Skip to content

Commit 4807f21

Browse files
committed
feat: UploadBlob
1 parent 013ca96 commit 4807f21

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import io
45
from typing import List
56
from .functions import upload as d, types as t, deal_status, get_uploads as getUploads
67

@@ -25,6 +26,18 @@ def upload(self, source: str) -> t.Upload:
2526
except Exception as e:
2627
raise e
2728

29+
def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
30+
"""
31+
Upload Blob a file or directory to the Lighthouse.
32+
33+
:param source: str, path to file or directory
34+
:return: t.Upload, the upload result
35+
"""
36+
try:
37+
return d.uploadBlob(source, filename, self.token)
38+
except Exception as e:
39+
raise e
40+
2841
@staticmethod
2942
def getDealStatus(cid: str) -> List[t.DealData]:
3043
"""

src/lighthouseweb3/functions/axios.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ def post_files(
6161
raise e
6262

6363
def post_blob(
64-
self, file: BufferedReader, headers: Dict[str, str] = None, **kwargs
64+
self, file: BufferedReader, filename: str, headers: Dict[str, str] = None, **kwargs
6565
) -> dict | Exception:
6666
try:
6767
self.parse_url_query(kwargs.get("query", None))
6868
files = [(
6969
"file",
7070
(
71-
utils.extract_file_name(file.name),
71+
utils.extract_file_name(filename),
7272
file.read(),
7373
"application/octet-stream",
7474
),

src/lighthouseweb3/functions/upload.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,30 @@ def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.
4242
file_dict["path"] = source
4343
return {"data": axios.post_files(file_dict, headers)}
4444
else:
45-
return {"data": axios.post_blob(source, headers)}
45+
return {"data": axios.post_blob(source, source.name, headers)}
46+
except Exception as e:
47+
print(e)
48+
raise e
49+
50+
51+
def uploadBlob(source: BufferedReader, filename: str, token: str) -> t.Upload:
52+
"""
53+
Upload a Buffer or readable Object
54+
@params {source}: str, path to file or directory
55+
@params {token}: str, lighthouse api token
56+
"""
57+
# create headers
58+
headers = {
59+
"Authorization": f"Bearer {token}",
60+
# "Content-Type": "multipart/form-data",
61+
"Encryption": "false",
62+
"Mime-Type": "application/octet-stream",
63+
}
64+
try:
65+
# create http object
66+
axios = Axios(Config.lighthouse_node + "/api/v0/add")
67+
# create list of files to upload
68+
return {"data": axios.post_blob(source, filename, headers)}
4669
except Exception as e:
4770
print(e)
4871
raise e

tests/test_upload.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from src.lighthouseweb3 import Lighthouse
66
from src.lighthouseweb3.functions.utils import NamedBufferedReader
77
from .setup import parse_env
8+
import string
9+
import secrets
10+
11+
12+
def generate_random_string(length: int) -> str:
13+
characters = string.ascii_letters + string.digits
14+
return ''.join(secrets.choice(characters) for _ in range(length))
815

916

1017
class TestUpload(unittest.TestCase):
@@ -36,8 +43,8 @@ def test_Upload_dir(self):
3643
def test_Upload_Blob(self):
3744
"""test Upload function"""
3845
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
39-
res = l.upload(NamedBufferedReader(
40-
io.BytesIO(b"tests/testdir/"), "hwh.txt"))
46+
res = l.uploadBlob(
47+
io.BytesIO(b"tests/testdir/"), f"{generate_random_string(16)}.txt")
4148
self.assertNotEqual(res.get("data"), None, "data is None")
4249
self.assertIsInstance(res.get("data"), dict, "data is a dict")
4350
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")

0 commit comments

Comments
 (0)