@@ -116,7 +116,7 @@ async def worker(session):
116116 else :
117117 raise ValueError ("Invalid file. Expected a file path as string or a binary (not text) file pointer" )
118118
119- file_name = fp . name
119+ file_name = getattr ( fp , " name" , "file.jpg" )
120120
121121 fp .seek (0 , os .SEEK_END )
122122 file_size = fp .tell ()
@@ -148,53 +148,52 @@ async def worker(session):
148148 for session in pool :
149149 await session .start ()
150150
151- with fp :
152- fp .seek (part_size * file_part )
151+ fp .seek (part_size * file_part )
153152
154- while True :
155- chunk = fp .read (part_size )
153+ while True :
154+ chunk = fp .read (part_size )
156155
157- if not chunk :
158- if not is_big and not is_missing_part :
159- md5_sum = "" .join ([hex (i )[2 :].zfill (2 ) for i in md5_sum .digest ()])
160- break
156+ if not chunk :
157+ if not is_big and not is_missing_part :
158+ md5_sum = "" .join ([hex (i )[2 :].zfill (2 ) for i in md5_sum .digest ()])
159+ break
160+
161+ if is_big :
162+ rpc = raw .functions .upload .SaveBigFilePart (
163+ file_id = file_id ,
164+ file_part = file_part ,
165+ file_total_parts = file_total_parts ,
166+ bytes = chunk
167+ )
168+ else :
169+ rpc = raw .functions .upload .SaveFilePart (
170+ file_id = file_id ,
171+ file_part = file_part ,
172+ bytes = chunk
173+ )
174+
175+ await queue .put (rpc )
176+
177+ if is_missing_part :
178+ return
161179
162- if is_big :
163- rpc = raw .functions .upload .SaveBigFilePart (
164- file_id = file_id ,
165- file_part = file_part ,
166- file_total_parts = file_total_parts ,
167- bytes = chunk
168- )
169- else :
170- rpc = raw .functions .upload .SaveFilePart (
171- file_id = file_id ,
172- file_part = file_part ,
173- bytes = chunk
174- )
180+ if not is_big and not is_missing_part :
181+ md5_sum .update (chunk )
175182
176- await queue . put ( rpc )
183+ file_part += 1
177184
178- if is_missing_part :
179- return
185+ if progress :
186+ func = functools .partial (
187+ progress ,
188+ min (file_part * part_size , file_size ),
189+ file_size ,
190+ * progress_args
191+ )
180192
181- if not is_big and not is_missing_part :
182- md5_sum .update (chunk )
183-
184- file_part += 1
185-
186- if progress :
187- func = functools .partial (
188- progress ,
189- min (file_part * part_size , file_size ),
190- file_size ,
191- * progress_args
192- )
193-
194- if inspect .iscoroutinefunction (progress ):
195- await func ()
196- else :
197- await self .loop .run_in_executor (self .executor , func )
193+ if inspect .iscoroutinefunction (progress ):
194+ await func ()
195+ else :
196+ await self .loop .run_in_executor (self .executor , func )
198197 except StopTransmission :
199198 raise
200199 except Exception as e :
@@ -222,3 +221,6 @@ async def worker(session):
222221
223222 for session in pool :
224223 await session .stop ()
224+
225+ if isinstance (path , (str , PurePath )):
226+ fp .close ()
0 commit comments