18 from typing
import Union
19 from pathlib
import Path
23 from .network_manager_base
import FileData
24 from .network_manager
import networkManager
25 from .network_response
import NetworkRequestError
28 MAX_CHUNK_SIZE = 128 * 1024 * 1024
31 def _loadChunk(filePath: Path, start: int, chunkSize: int) -> bytes:
32 with filePath.open(
"rb")
as file:
34 return file.read(chunkSize)
40 A class which splits a file into chunks and uploades it
41 chunk by chunk. This class should be used for uploading
42 files larger than 2 GiB, since Python does not support
43 uploading files with a larger size.
45 Maximum chunk size is 128 MiB.
50 size of chunks into which the file will be split
51 maximum value is 128 MiB, while the minimum value is 1
52 filePath : Union[Path, str]
53 path to the file which will be uploaded
55 size of the file which will be uploaded
58 def __init__(self, chunkSize: int, filePath: Union[Path, str]) ->
None:
59 if chunkSize <= 0
or chunkSize > MAX_CHUNK_SIZE:
60 raise ValueError(f
">> [Coretex] Invalid \"chunkSize\" value \"{chunkSize}\". Value must be in range 0-{MAX_CHUNK_SIZE}")
62 if isinstance(filePath, str):
63 filePath = Path(filePath)
67 self.
fileSizefileSize = filePath.lstat().st_size
69 def __start(self) -> str:
74 response = networkManager.post(
"upload/start", parameters)
75 if response.hasFailed():
76 raise NetworkRequestError(response, f
"Failed to start chunked upload for \"{self.filePath}\"")
78 uploadId = response.getJson(dict).get(
"id")
80 if not isinstance(uploadId, str):
81 raise ValueError(f
">> [Coretex] Invalid API response, invalid value \"{uploadId}\" for field \"id\"")
85 def __uploadChunk(self, uploadId: str, start: int, end: int) ->
None:
94 FileData.createFromBytes(
"file", chunk, self.
filePathfilePath.name)
97 response = networkManager.formData(
"upload/chunk", parameters, files)
98 if response.hasFailed():
99 raise NetworkRequestError(response, f
"Failed to upload file chunk with byte range \"{start}-{end}\"")
101 logging.getLogger(
"coretexpylib").debug(f
">> [Coretex] Uploaded chunk with range \"{start}-{end}\"")
105 Uploads the file to Coretex.ai
109 str -> ID of the uploaded file
113 NetworkRequestError, ValueError -> if some kind of error happened during
114 the upload of the provided file
118 >>> from coretex.networking import ChunkUploadSession, NetworkRequestError
120 >>> chunkSize = 16 * 1024 * 1024 # chunk size: 16 MiB
121 >>> uploadSession = ChunkUploadSession(chunkSize, path/fo/file.ext)
124 uploadId = uploadSession.run()
126 except NetworkRequestError, ValueError:
127 print("Failed to upload file")
129 logging.getLogger(
"coretexpylib").debug(f
">> [Coretex] Starting upload for \"{self.filePath}\"")
131 uploadId = self.
__start__start()
137 for i
in range(chunkCount):
146 def fileChunkUpload(path: Path, chunkSize: int = MAX_CHUNK_SIZE) -> str:
148 Uploads file in chunks to Coretex.ai server.
149 Should be used when uploading large files.
154 File which will be uploaded in chunks
156 Size of the chunks into which file will be split
157 before uploading. Maximum value is 128 MiBs
161 str -> id of the file which was uploaded
164 if not path.is_file():
165 raise ValueError(f
"{path} is not a file")
167 if chunkSize > MAX_CHUNK_SIZE:
168 chunkSize = MAX_CHUNK_SIZE
171 return uploadSession.run()
None __uploadChunk(self, str uploadId, int start, int end)