From 88b1324e4a4df4ea2684123186e815dee1159a97 Mon Sep 17 00:00:00 2001 From: Frederik Haarslev Date: Tue, 7 May 2024 13:48:04 +0200 Subject: [PATCH] Refactored chunking loop --- src/mediacatch_s2t/uploader.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/mediacatch_s2t/uploader.py b/src/mediacatch_s2t/uploader.py index 83866ac..db6cde7 100644 --- a/src/mediacatch_s2t/uploader.py +++ b/src/mediacatch_s2t/uploader.py @@ -1,5 +1,6 @@ import requests from concurrent.futures import ThreadPoolExecutor, as_completed +from io import BufferedReader from pathlib import Path from typing import Optional, Generator, Union @@ -104,19 +105,16 @@ def upload_file_chunks(self) -> None: except Exception as e: print(f"Chunk {part_number} failed to upload due to: {e}") - def _read_file_in_chunks(self, file) -> Generator[bytes, None, None]: + def _read_file_in_chunks(self, file: BufferedReader) -> Generator[bytes, None, None]: """Generator that reads the file in chunks. Args: - file (IO[bytes]): File object opened in binary read mode. + file (BufferedReader): File object opened in binary read mode. Yields: bytes: A chunk of the file. """ - while True: - chunk = file.read(self.CHUNK_SIZE) - if not chunk: - break + while chunk := file.read(self.CHUNK_SIZE): yield chunk def upload_chunk(self, part_number: int, chunk: bytes) -> None: