Philipp Hörist pushed to branch master at gajim / gajim


Commits:
302759d2 by Philipp Hörist at 2025-11-15T17:45:47+01:00
fix: HTTPDownload: Correctly close file on errors

Fixes #12536

- - - - -
e0d8bcc2 by Philipp Hörist at 2025-11-15T17:47:11+01:00
cq: HTTP: Remove print statements

- - - - -


1 changed file:

- gajim/common/multiprocess/http.py


Changes:

=====================================
gajim/common/multiprocess/http.py
=====================================
@@ -13,6 +13,7 @@
 import threading
 from collections.abc import Iterable
 from dataclasses import dataclass
+from functools import partial
 from io import BytesIO
 from pathlib import Path
 
@@ -189,38 +190,38 @@ def http_download(
             decryptor = NonDecryptor()
 
     if output is None:
-        file = BytesIO()
+        file_method = BytesIO
     else:
-        file = output.open(mode="wb")
+        file_method = partial(output.open, mode="wb")
 
     queue.put(TransferState(id=ft_id, state=FTState.IN_PROGRESS, progress=0))
 
-    for data in resp.iter_bytes(chunk_size=chunk_size):
-        if event.is_set():
-            file.close()
-            raise CancelledError
-
-        received_length += len(data)
-        if received_length > content_length:
-            raise OverflowError(f"{received_length} > {content_length}")
+    with file_method() as file:
+        for data in resp.iter_bytes(chunk_size=chunk_size):
+            if event.is_set():
+                raise CancelledError
 
-        progress = round(received_length / content_length, 2)
-        hash_obj.update(data)
-        file.write(decryptor.decrypt(data))
-        if with_progress:
-            queue.put(
-                TransferState(id=ft_id, state=FTState.IN_PROGRESS, 
progress=progress)
-            )
+            received_length += len(data)
+            if received_length > content_length:
+                raise OverflowError(f"{received_length} > {content_length}")
 
-    file.write(decryptor.finalize())
-    if with_progress:
-        queue.put(TransferState(id=ft_id, state=FTState.IN_PROGRESS, 
progress=1))
+            progress = round(received_length / content_length, 2)
+            hash_obj.update(data)
+            file.write(decryptor.decrypt(data))
+            if with_progress:
+                queue.put(
+                    TransferState(
+                        id=ft_id, state=FTState.IN_PROGRESS, progress=progress
+                    )
+                )
 
-    content = b""
-    if isinstance(file, BytesIO):
-        content = file.getvalue()
+        file.write(decryptor.finalize())
+        if with_progress:
+            queue.put(TransferState(id=ft_id, state=FTState.IN_PROGRESS, 
progress=1))
 
-    file.close()
+        content = b""
+        if isinstance(file, BytesIO):
+            content = file.getvalue()
 
     digest = hash_obj.hexdigest()
     if hash_value is not None and digest != hash_value:
@@ -271,7 +272,6 @@ def http_upload(
         file = io.BytesIO(input_)
     else:
         content_size = input_.stat().st_size
-        print("content-size", content_size)
         file = input_.open(mode="rb")
 
     match encryption_data:
@@ -281,8 +281,6 @@ def http_upload(
         case _:
             encryptor = NonEncryptor()
 
-    print("content-size-after", content_size)
-
     default_headers = {
         "User-Agent": USER_AGENT,
         "Content-Type": content_type,



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/compare/454211787d9182acf72f9e7b5f7405705bd27ad0...e0d8bcc2988c2a61d480bcca694f214723554984

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/compare/454211787d9182acf72f9e7b5f7405705bd27ad0...e0d8bcc2988c2a61d480bcca694f214723554984
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to