aboutsummaryrefslogtreecommitdiff
path: root/gn3/file_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/file_utils.py')
-rw-r--r--gn3/file_utils.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/gn3/file_utils.py b/gn3/file_utils.py
index c1c9d53..4dcf296 100644
--- a/gn3/file_utils.py
+++ b/gn3/file_utils.py
@@ -5,12 +5,14 @@ import os
import random
import string
import tarfile
+
from functools import partial
from typing import Dict
from typing import List
-
from werkzeug.utils import secure_filename
+import ipfshttpclient
+
def get_hash_of_files(files: List[str]) -> str:
"""Given a list of valid of FILES, return their hash as a string"""
@@ -75,3 +77,19 @@ contents to TARGET_DIR/<dir-hash>.
except Exception:
return {"status": 128, "error": "gzip failed to unpack file"}
return {"status": 0, "token": token}
+
+
+def cache_ipfs_file(ipfs_file: str,
+ cache_dir: str,
+ ipfs_addr: str = "/ip4/127.0.0.1/tcp/5001") -> str:
+ """Check if a file exists in cache; if it doesn't, cache it. Return the
+ cached file location
+
+ """
+ file_loc = os.path.join(cache_dir, ipfs_file.split("ipfs/")[-1])
+ if not os.path.exists(file_loc):
+ client = ipfshttpclient.connect(ipfs_addr)
+ client.get(ipfs_file,
+ target=os.path.join(cache_dir,
+ ipfs_file.split("ipfs/")[-1]))
+ return file_loc