33import os
44import io
55from 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
77
88
99class Lighthouse :
@@ -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,7 +36,40 @@ 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 )
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 )
4073 except Exception as e :
4174 raise e
4275
@@ -66,3 +99,30 @@ def getUploads(publicKey: str, pageNo: int = 1) -> List[t.DealData]:
6699 return getUploads .get_uploads (publicKey , pageNo )
67100 except Exception as e :
68101 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