2022-12-19 01:09:59 +02:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2022-02-26 01:48:16 +02:00
|
|
|
from urllib.parse import quote_plus
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import requests_unixsocket
|
2023-11-10 01:31:41 +02:00
|
|
|
import lzma
|
2022-02-26 01:48:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class IPFSController:
|
2022-12-19 01:09:59 +02:00
|
|
|
def __init__(self, nodeApiAddress, clusterApiAddress, nixCache, db):
|
|
|
|
self.__nodeAddr = f'http+unix://{quote_plus(nodeApiAddress.get("unix"))}'
|
|
|
|
self.__clusterAddr = f'http+unix://{quote_plus(clusterApiAddress.get("unix"))}'
|
2022-02-26 01:48:16 +02:00
|
|
|
self.__nix = nixCache
|
|
|
|
self.__db = db
|
|
|
|
|
2022-12-19 00:05:29 +02:00
|
|
|
def ipfs_fetch_task(self, callback, nar, hint=None):
|
2022-02-26 01:48:16 +02:00
|
|
|
print(f"Downloading NAR: {nar}")
|
2022-12-19 00:05:29 +02:00
|
|
|
code, _, content = self.__nix.try_all("get", nar, hint)
|
2022-02-26 01:48:16 +02:00
|
|
|
if code == 200:
|
2023-11-10 01:31:41 +02:00
|
|
|
if nar.endswith(".nar.xz"):
|
|
|
|
print(f"Attempt decompression of {nar}")
|
|
|
|
decompressed = lzma.decompress(content)
|
|
|
|
print(f"Size diff: {len(content)} -> {len(decompressed)}")
|
|
|
|
content = decompressed
|
|
|
|
|
2022-03-02 00:59:31 +02:00
|
|
|
upload = {"file": ("FILE", content, "application/octet-stream")}
|
2022-02-26 01:48:16 +02:00
|
|
|
try:
|
2022-03-02 00:59:31 +02:00
|
|
|
rIpfs = requests_unixsocket.post(
|
2023-11-10 01:36:26 +02:00
|
|
|
f"{self.__nodeAddr}/api/v0/add?pin=false&quieter=true&chunker=buzhash&trickle=true", files=upload
|
2022-03-02 00:59:31 +02:00
|
|
|
)
|
2022-02-26 01:48:16 +02:00
|
|
|
hash = rIpfs.json()["Hash"]
|
|
|
|
print(f"Mapped: {nar} -> /ipfs/{hash}")
|
|
|
|
self.__db.set_path(nar, hash)
|
2022-12-19 01:09:59 +02:00
|
|
|
expireAt = datetime.now(timezone.utc) + timedelta(hours=24)
|
|
|
|
rClusterPin = requests_unixsocket.post(
|
|
|
|
f"{self.__clusterAddr}/pins/ipfs/{hash}?expire-at={quote_plus(expireAt.isoformat())}&mode=recursive&name=reflex-{quote_plus(nar)}&replication-max=2&replication-min=1", files=upload
|
|
|
|
)
|
|
|
|
if rClusterPin.status_code != 200:
|
|
|
|
print(f"Warning: failed to pin {hash} on IPFS cluster")
|
2022-12-19 00:05:29 +02:00
|
|
|
callback()
|
2022-02-26 01:48:16 +02:00
|
|
|
return (nar, 200, hash)
|
|
|
|
except requests.ConnectionError as e:
|
|
|
|
print(e)
|
2022-12-19 00:05:29 +02:00
|
|
|
callback()
|
2022-02-26 01:48:16 +02:00
|
|
|
return (nar, 502, False)
|
|
|
|
else:
|
2022-12-19 00:05:29 +02:00
|
|
|
callback()
|
2022-02-26 01:48:16 +02:00
|
|
|
return (nar, code, False)
|