18
18
19
19
20
20
def _generate_lock_id ():
21
+ """Generate a random 12-character lock ID."""
21
22
return "" .join (choice (ascii_letters ) for i in range (12 )) # nosec
22
23
23
24
24
25
class FileUploadService (BaseService ):
25
26
def _create_new_file (self , filename : str , parent_object_id : str , persno : str , check_access : bool = True ) -> str :
26
- """Creates a new ( empty) file attached to the parent object and returns the cdb_object_id ."""
27
+ """Create a new empty file attached to the parent object."""
27
28
response_json = self .request (
28
29
endpoint = "/file_upload/create" ,
29
30
method = "POST" ,
@@ -37,6 +38,7 @@ def _create_new_file(self, filename: str, parent_object_id: str, persno: str, ch
37
38
def _get_presigned_write_urls (
38
39
self , file_object_id : str , filesize : int , lock_id : str , persno : str , check_access : bool = True
39
40
) -> PresignedWriteUrls :
41
+ """Request presigned URLs for uploading file chunks."""
40
42
response_json = self .request (
41
43
endpoint = f"/file_upload/{ file_object_id } /generate_presigned_url" ,
42
44
method = "POST" ,
@@ -50,7 +52,7 @@ def _get_presigned_write_urls(
50
52
def _upload_from_stream (
51
53
self , presigned_urls : PresignedWriteUrls , stream : BinaryIO
52
54
) -> tuple [PresignedWriteUrls , str ]:
53
- """Upload file stream in chunks using presigned URLs and return updated context + sha256 hash."""
55
+ """Upload file stream in chunks and return updated presigned URLs and sha256 hash."""
54
56
etags : list [str ] = []
55
57
sha256 = hashlib .sha256 ()
56
58
for url in presigned_urls .urls :
@@ -70,6 +72,7 @@ def _upload_from_stream(
70
72
71
73
@staticmethod
72
74
def _get_stream_size (stream : BinaryIO ) -> int :
75
+ """Get the size of a seekable stream."""
73
76
if not stream .seekable ():
74
77
raise ValueError ("Stream is not seekable; size cannot be determined." )
75
78
current_pos = stream .tell ()
@@ -89,6 +92,7 @@ def _complete_upload(
89
92
sha256 : str | None = None ,
90
93
delete_derived_files : bool = True ,
91
94
) -> None :
95
+ """Mark the upload as complete and finalize the file."""
92
96
self .request (
93
97
endpoint = f"/file_upload/{ file_object_id } /complete" ,
94
98
method = "POST" ,
@@ -106,6 +110,7 @@ def _complete_upload(
106
110
def _abort_upload (
107
111
self , file_object_id : str , lock_id : str , persno : str , presigned_write_urls : PresignedWriteUrls
108
112
) -> None :
113
+ """Abort an ongoing file upload."""
109
114
self .request (
110
115
endpoint = f"/file_upload/{ file_object_id } /abort" ,
111
116
method = "POST" ,
0 commit comments