3
3
import os
4
4
import io
5
5
from typing import List
6
- from .functions import upload as d , types as t , deal_status , get_uploads as getUploads
6
+ from .functions import upload as d , types as t , deal_status , get_uploads as getUploads , download as _download
7
7
8
8
9
9
class Lighthouse :
@@ -14,19 +14,19 @@ def __init__(self, token: str = ""):
14
14
"No token provided: Please provide a token or set the LIGHTHOUSE_TOKEN environment variable"
15
15
)
16
16
17
- def upload (self , source : str ) -> t .Upload :
17
+ def upload (self , source : str , tag : str = '' ) -> t .Upload :
18
18
"""
19
19
Upload a file or directory to the Lighthouse.
20
20
21
21
:param source: str, path to file or directory
22
22
:return: t.Upload, the upload result
23
23
"""
24
24
try :
25
- return d .upload (source , self .token )
25
+ return d .upload (source , self .token , tag )
26
26
except Exception as e :
27
27
raise e
28
28
29
- def uploadBlob (self , source : io .BufferedReader , filename : str ) -> t .Upload :
29
+ def uploadBlob (self , source : io .BufferedReader , filename : str , tag : str = '' ) -> t .Upload :
30
30
"""
31
31
Upload Blob a file or directory to the Lighthouse.
32
32
@@ -36,7 +36,40 @@ def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
36
36
if not (hasattr (source , 'read' ) and hasattr (source , 'close' )):
37
37
raise TypeError ("source must have 'read' and 'close' methods" )
38
38
try :
39
- return d .uploadBlob (source , filename , self .token )
39
+ return d .uploadBlob (source , filename , self .token , tag )
40
+ except Exception as e :
41
+ raise e
42
+
43
+ @staticmethod
44
+ def downloadBlob (dist : io .BufferedWriter , cid : str , chunk_size = 1024 * 1024 * 10 ) -> t .Upload :
45
+ """
46
+ Download a Blob (file or directory) from the Lighthouse.
47
+
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
53
+ """
54
+ if not (hasattr (dist , 'read' ) and hasattr (dist , 'close' )):
55
+ raise TypeError ("source must have 'read' and 'close' methods" )
56
+ try :
57
+ return _download .download_file_into_writable (cid , dist , chunk_size )
58
+ except Exception as e :
59
+ raise e
60
+
61
+ @staticmethod
62
+ def downloadBlob (dist : io .BufferedWriter , cid : str , chunk_size = 1024 * 1024 * 10 ) -> t .Upload :
63
+ """
64
+ Download Blob a file or directory to the Lighthouse.
65
+
66
+ :param source: str, path to file or directory
67
+ :return: t.Upload, the upload result
68
+ """
69
+ if not (hasattr (dist , 'read' ) and hasattr (dist , 'close' )):
70
+ raise TypeError ("source must have 'read' and 'close' methods" )
71
+ try :
72
+ return _download .download_file_into_writable (cid , dist , chunk_size )
40
73
except Exception as e :
41
74
raise e
42
75
@@ -66,3 +99,30 @@ def getUploads(publicKey: str, pageNo: int = 1) -> List[t.DealData]:
66
99
return getUploads .get_uploads (publicKey , pageNo )
67
100
except Exception as e :
68
101
raise e
102
+
103
+ @staticmethod
104
+ def download (cid : str ) -> bytes :
105
+ """
106
+ Download content from the Lighthouse using its Content Identifier (CID).
107
+
108
+ :param cid: str, Content Identifier for the data to be downloaded
109
+ :param useCidAsTag: bool, flag to use CID as a tag (default: False)
110
+ :return: bytes, the downloaded content
111
+ """
112
+ try :
113
+ return _download .get_file (cid )
114
+ except Exception as e :
115
+ raise e
116
+
117
+ def getTagged (self , tag : str ) -> t .Upload :
118
+ """
119
+ Retrieve an upload from the Lighthouse using its tag.
120
+
121
+ :param tag: str, tag associated with the file or directory
122
+ :return: t.Upload, the upload result
123
+ """
124
+ try :
125
+ return _download .getTaggedCid (tag , self .token )
126
+ except Exception as e :
127
+ raise e
128
+
0 commit comments