Skip to content

Commit fd37ea3

Browse files
committed
[Added] ➕ download Functions
1 parent f9b78fa commit fd37ea3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import requests
2+
import warnings
3+
import io
4+
from .axios import Axios
5+
from .config import Config
6+
7+
8+
# 10MB chunks by default
9+
def download_file_into_writable(cid: str, writable_object: io.BufferedWriter, chunk_size=1024*1024*10):
10+
url = f"{Config.lighthouse_gateway}/{cid}"
11+
with requests.get(url, stream=True) as r:
12+
r.raise_for_status()
13+
for chunk in r.iter_content(chunk_size=chunk_size):
14+
if chunk: # filter out keep-alive new chunks
15+
writable_object.write(chunk)
16+
return {"data": {"Hash": cid, "Size": writable_object.tell()}}
17+
18+
19+
def get_url_body(url):
20+
response = requests.get(url)
21+
response.raise_for_status() # Raises stored HTTPError, if one occurred.
22+
return response.content, response.headers
23+
24+
25+
def get_file(cid: str) -> (bytes, str):
26+
try:
27+
url = f"{Config.lighthouse_gateway}/{cid}"
28+
29+
(body, headers) = get_url_body(url)
30+
31+
# show a warning if the file is greater then 2GB
32+
if (int(headers['Content-Length']) > 1024*1024*1024*2):
33+
warnings.warn(
34+
"This content of the file is grater then 2GB, use `downloadBlob` instead", UserWarning)
35+
return (body, headers['Content-Type'])
36+
except requests.HTTPError as error:
37+
raise Exception(error.response.text)
38+
39+
40+
def getTaggedCid(tag: str, token: str):
41+
42+
_axios = Axios(
43+
f"{Config.lighthouse_api}/api/user/get_tag_details?tag={tag}")
44+
data = _axios.get({
45+
"Authorization": f"Bearer {token}"
46+
})
47+
return data
48+
49+
# return {"data": {"Hash": cid, "Size": writable_object.tell()}}

0 commit comments

Comments
 (0)