Skip to content

Commit c2cc518

Browse files
committed
[feature]🧰 Upload with Tag
1 parent 28c8972 commit c2cc518

File tree

6 files changed

+87
-33
lines changed

6 files changed

+87
-33
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"Programming Language :: Python :: 3.8",
2525
"Programming Language :: Python :: 3.9",
2626
"Programming Language :: Python :: 3.10",
27+
"Programming Language :: Python :: 3.11",
2728
],
2829
keywords="lighthouse storage sdk python filecoin ipfs web3 perpetual",
2930
long_description=(

src/lighthouseweb3/__init__.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ def __init__(self, token: str = ""):
1414
"No token provided: Please provide a token or set the LIGHTHOUSE_TOKEN environment variable"
1515
)
1616

17-
def upload(self, source: str) -> t.Upload:
17+
def upload(self, source: str, tag: str = '') -> t.Upload:
1818
"""
1919
Upload a file or directory to the Lighthouse.
2020
2121
:param source: str, path to file or directory
2222
:return: t.Upload, the upload result
2323
"""
2424
try:
25-
return d.upload(source, self.token)
25+
return d.upload(source, self.token, tag)
2626
except Exception as e:
2727
raise e
2828

29-
def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
29+
def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = '') -> t.Upload:
3030
"""
3131
Upload Blob a file or directory to the Lighthouse.
3232
@@ -36,17 +36,20 @@ def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
3636
if not (hasattr(source, 'read') and hasattr(source, 'close')):
3737
raise TypeError("source must have 'read' and 'close' methods")
3838
try:
39-
return d.uploadBlob(source, filename, self.token)
39+
return d.uploadBlob(source, filename, self.token, tag)
4040
except Exception as e:
4141
raise e
4242

4343
@staticmethod
4444
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10) -> t.Upload:
4545
"""
46-
Download Blob a file or directory to the Lighthouse.
46+
Download a Blob (file or directory) from the Lighthouse.
4747
48-
:param source: str, path to file or directory
49-
:return: t.Upload, the upload result
48+
:param dist: BufferedWriter, destination to write the downloaded data
49+
:param cid: str, Content Identifier for the data to be downloaded
50+
:param chunk_size: int, size of chunks in which the file will be downloaded (default: 10MB)
51+
:param useCidAsTag: bool, flag to use CID as a tag (default: False)
52+
:return: t.Upload, the download result
5053
"""
5154
if not (hasattr(dist, 'read') and hasattr(dist, 'close')):
5255
raise TypeError("source must have 'read' and 'close' methods")
@@ -85,13 +88,25 @@ def getUploads(publicKey: str, pageNo: int = 1) -> List[t.DealData]:
8588
@staticmethod
8689
def download(cid: str) -> bytes:
8790
"""
88-
Get uploads from the Lighthouse.
91+
Download content from the Lighthouse using its Content Identifier (CID).
8992
90-
:param publicKey: str, public key
91-
:param pageNo: int, page number (default: 1)
92-
:return: List[t.DealData], list of deal data
93+
:param cid: str, Content Identifier for the data to be downloaded
94+
:param useCidAsTag: bool, flag to use CID as a tag (default: False)
95+
:return: bytes, the downloaded content
9396
"""
9497
try:
9598
return _download.get_file(cid)
9699
except Exception as e:
97100
raise e
101+
102+
def getTagged(self, tag: str) -> t.Upload:
103+
"""
104+
Retrieve an upload from the Lighthouse using its tag.
105+
106+
:param tag: str, tag associated with the file or directory
107+
:return: t.Upload, the upload result
108+
"""
109+
try:
110+
return _download.getTaggedCid(tag, self.token)
111+
except Exception as e:
112+
raise e

src/lighthouseweb3/functions/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ class Config:
66

77
# lighthouse_api = "http://13.234.35.183:5050" # "https://api.lighthouse.storage"
88
lighthouse_api = 'https://api.lighthouse.storage'
9+
lighthouse_api_test = "http://34.131.52.103"
910
lighthouse_node = "https://node.lighthouse.storage"
1011
lighthouse_bls_node = "https://encryption.lighthouse.storage"
12+
lighthouse_gateway = "https://gateway.lighthouse.storage/ipfs"

src/lighthouseweb3/functions/upload.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from . import types as t
99

1010

11-
def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.Upload:
11+
def upload(source: str | BufferedReader | NamedBufferedReader, token: str, tag: str = "") -> t.Upload:
1212
"""
1313
Deploy a file or directory to the lighthouse network
1414
@params {source}: str, path to file or directory
@@ -40,15 +40,24 @@ def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.
4040
file_dict["files"] = [source]
4141
file_dict["is_dir"] = False
4242
file_dict["path"] = source
43-
return {"data": axios.post_files(file_dict, headers)}
43+
hashData = axios.post_files(file_dict, headers)
4444
else:
45-
return {"data": axios.post_blob(source, source.name, headers)}
45+
hashData = axios.post_blob(source, source.name, headers)
46+
47+
if len(tag):
48+
_axios = Axios(Config.lighthouse_api_test + "/api/user/create_tag")
49+
data = _axios.post({
50+
"tag": tag,
51+
"cid": hashData.get("Hash")
52+
}, {
53+
"Authorization": f"Bearer {token}", })
54+
return {"data": hashData}
4655
except Exception as e:
4756
print(e)
4857
raise e
4958

5059

51-
def uploadBlob(source: BufferedReader, filename: str, token: str) -> t.Upload:
60+
def uploadBlob(source: BufferedReader, filename: str, token: str, tag: str = "") -> t.Upload:
5261
"""
5362
Upload a Buffer or readable Object
5463
@params {source}: str, path to file or directory
@@ -65,7 +74,16 @@ def uploadBlob(source: BufferedReader, filename: str, token: str) -> t.Upload:
6574
# create http object
6675
axios = Axios(Config.lighthouse_node + "/api/v0/add")
6776
# create list of files to upload
68-
return {"data": axios.post_blob(source, filename, headers)}
77+
78+
hashData = axios.post_blob(source, filename, headers)
79+
if len(tag):
80+
_axios = Axios(Config.lighthouse_api_test + "/api/user/create_tag")
81+
data = _axios.post({
82+
"tag": tag,
83+
"cid": hashData.get("Hash")
84+
}, {
85+
"Authorization": f"Bearer {token}", })
86+
return {"data": hashData}
6987
except Exception as e:
7088
print(e)
7189
raise e

tests/test_download.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,14 @@ def test_download_file(self):
3333
self.assertIsInstance(res, bytes, "type doesn't match")
3434
self.assertEqual(res, b'tests/testdir/', "data doesn't match")
3535

36-
# def test_download_blob(self):
37-
# """test Upload function"""
38-
# l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
39-
# res = l.uploadBlob(
40-
# io.BytesIO(b"tests/testdir/"), f"{generate_random_string(16)}.txt")
41-
# self.assertNotEqual(res.get("data"), None, "data is None")
42-
# self.assertIsInstance(res.get("data"), dict, "data is a dict")
43-
# self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
44-
4536
def test_download_blob_file(self):
4637
"""test download_blob function"""
4738
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
4839
with open("./image.png", "wb") as file:
4940
res = l.downloadBlob(
5041
file, "QmPT11PFFQQD3mT6BdwfSHQGHRdF8ngmRmcvxtSBiddWEa", chunk_size=1024*100)
51-
self.assertEqual(file.tell(), 123939, "File Size dont match")
52-
self.assertEqual(res, True, "data doesn't match")
42+
self.assertEqual(res.get("data").get("Size"),
43+
123939, "File Size dont match")
5344

5445

5546
if __name__ == "__main__":

tests/test_upload.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@ def test_env(self):
2525
os.environ.get("LIGHTHOUSE_TOKEN"), None, "token is not None"
2626
)
2727

28-
def test_Upload_file(self):
28+
def test_upload_file(self):
2929
"""test Upload function"""
3030
l = Lighthouse() # will use env var
3131
res = l.upload("tests/testdir/testfile.txt")
3232
self.assertNotEqual(res.get("data"), None, "data is None")
3333
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
3434

35-
def test_Upload_dir(self):
35+
def test_upload_dir(self):
3636
"""test Upload function"""
3737
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
38-
res = l.upload("tests/testdir/")
38+
res = l.upload("tests/")
3939
self.assertNotEqual(res.get("data"), None, "data is None")
4040
self.assertIsInstance(res.get("data"), dict, "data is a dict")
41-
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
41+
self.assertIsInstance(res.get("data").get(
42+
"Hash"), str, "Instance is not of type String")
43+
self.assertIsInstance(res.get("data").get(
44+
"Size"), str, "Instance is not of type String")
4245

43-
def test_Upload_Blob(self):
46+
def test_upload_Blob(self):
4447
"""test Upload function"""
4548
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
4649
res = l.uploadBlob(
@@ -49,7 +52,7 @@ def test_Upload_Blob(self):
4952
self.assertIsInstance(res.get("data"), dict, "data is a dict")
5053
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
5154

52-
def test_Upload_File(self):
55+
def test_upload_File(self):
5356
"""test Upload function"""
5457
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
5558
with open("./.gitignore", "rb") as file:
@@ -59,6 +62,30 @@ def test_Upload_File(self):
5962
self.assertNotEqual(res.get("data").get(
6063
"Hash"), None, "data is None")
6164

65+
def test_upload_with_tag(self):
66+
"""test Upload with tag function"""
67+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
68+
tag = generate_random_string(8)
69+
res = l.uploadBlob(
70+
io.BytesIO(b"tests/testdir/"+generate_random_string(80).encode("utf-8")), f"{generate_random_string(16)}.txt", tag)
71+
self.assertNotEqual(res.get("data"), None, "data is None")
72+
self.assertIsInstance(res.get("data"), dict, "data is a dict")
73+
self.assertIsInstance(res.get("data").get(
74+
"Hash"), str, "Hash is not of type string")
75+
76+
tagData = l.getTagged(tag)
77+
self.assertEqual(tag, tagData.get("data").get("tag"), "Tag dont match")
78+
self.assertEqual(res.get("data").get("Hash"), tagData.get(
79+
"data").get("cid"), "Tag dont match")
80+
81+
# overWrite tag
82+
res = l.uploadBlob(
83+
io.BytesIO(b"tests/testdir/"+generate_random_string(80).encode("utf-8")), f"{generate_random_string(16)}.txt", tag)
84+
tagData = l.getTagged(tag)
85+
self.assertEqual(tag, tagData.get("data").get("tag"), "Tag dont match")
86+
self.assertEqual(res.get("data").get("Hash"), tagData.get(
87+
"data").get("cid"), "Tag dont match")
88+
6289

6390
if __name__ == "__main__":
6491
unittest.main()

0 commit comments

Comments
 (0)