File size: 1,607 Bytes
6b53875 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import os, json
# ENV (optional):
# - IPFS_API_MULTIADDR (default: /ip4/127.0.0.1/tcp/5001/http)
# - PINATA_JWT (if present, uses Pinata pinning as fallback)
def pin_bytes_to_ipfs(data: bytes) -> dict:
"""
Try local IPFS daemon first; fallback to Pinata if configured.
Returns: {"ok":True,"cid":"..."} or {"ok":False,"error":"..."}
"""
# 1) Local daemon via ipfshttpclient
try:
import ipfshttpclient
addr = os.environ.get("IPFS_API_MULTIADDR", "/ip4/127.0.0.1/tcp/5001/http")
with ipfshttpclient.connect(addr) as client:
res = client.add_bytes(data)
return {"ok": True, "cid": res}
except Exception as e:
err_local = str(e)
# 2) Pinata fallback
token = os.environ.get("PINATA_JWT", "").strip()
if token:
import requests
try:
r = requests.post(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
headers={"Authorization": f"Bearer {token}"},
files={"file": ("bundle.json", data, "application/json")},
timeout=30
)
if r.status_code in (200, 202):
cid = r.json().get("IpfsHash") or r.json().get("ipfsHash")
if cid:
return {"ok": True, "cid": cid}
return {"ok": False, "error": f"Pinata error: {r.status_code} {r.text}"}
except Exception as e:
return {"ok": False, "error": f"Pinata exception: {e}"}
return {"ok": False, "error": f"IPFS local failed: {err_local}; no Pinata fallback configured"}
|