diff options
author | BonfaceKilz | 2021-03-22 11:16:23 +0300 |
---|---|---|
committer | BonfaceKilz | 2021-03-23 01:14:37 +0300 |
commit | c4285db5edb4cdeea7e6b68bec177d0ce2c4115b (patch) | |
tree | bc2fcc32dde77a3e76a1027a8b2061b590f55a9f /gn3 | |
parent | 4dfb9d5d06d3544aea5c769b71537f51234ae74c (diff) | |
download | genenetwork3-c4285db5edb4cdeea7e6b68bec177d0ce2c4115b.tar.gz |
Add function to cache ipfs files
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/file_utils.py | 20 |
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 |