From 7c0a4ff70312e26d8637373cc4f27be3893b3103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Fri, 22 May 2026 14:14:07 +0200 Subject: [PATCH] fix in download torrents --- pytorrent/services/rtorrent/files.py | 46 +++++++++------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/pytorrent/services/rtorrent/files.py b/pytorrent/services/rtorrent/files.py index 74099f3..4aa9563 100644 --- a/pytorrent/services/rtorrent/files.py +++ b/pytorrent/services/rtorrent/files.py @@ -176,25 +176,16 @@ def _media_info_sample_suffix(source_path: str) -> str: def _read_file_prefix(profile: dict, source_path: str, max_bytes: int) -> bytes: - # Note: Small previews use a bounded prefix read, so text and image preview actions never load an entire large file into RAM. + # Note: File info must read through rTorrent, not the pyTorrent process, because torrents may live on a remote host or under rTorrent-only permissions. limit = max(0, int(max_bytes or 0)) chunks: list[bytes] = [] collected = 0 - if int(profile.get("is_remote") or 0): - for chunk in iter_remote_file_chunks(profile, source_path, size=limit, chunk_size=_MEDIA_INFO_CHUNK_BYTES): - if collected >= limit: - break - data = bytes(chunk[: max(0, limit - collected)]) - chunks.append(data) - collected += len(data) - else: - with open(source_path, "rb") as src: - while collected < limit: - data = src.read(min(_MEDIA_INFO_CHUNK_BYTES, limit - collected)) - if not data: - break - chunks.append(data) - collected += len(data) + for chunk in iter_remote_file_chunks(profile, source_path, size=limit, chunk_size=_MEDIA_INFO_CHUNK_BYTES): + if collected >= limit: + break + data = bytes(chunk[: max(0, limit - collected)]) + chunks.append(data) + collected += len(data) return b"".join(chunks) @@ -340,21 +331,12 @@ def _media_info_temp_sample(profile: dict, source_path: str, max_bytes: int) -> written = 0 try: with os.fdopen(fd, "wb") as tmp: - if int(profile.get("is_remote") or 0): - for chunk in iter_remote_file_chunks(profile, source_path, size=max_bytes, chunk_size=_MEDIA_INFO_CHUNK_BYTES): - if written >= max_bytes: - break - data = bytes(chunk[: max(0, max_bytes - written)]) - tmp.write(data) - written += len(data) - else: - with open(source_path, "rb") as src: - while written < max_bytes: - data = src.read(min(_MEDIA_INFO_CHUNK_BYTES, max_bytes - written)) - if not data: - break - tmp.write(data) - written += len(data) + for chunk in iter_remote_file_chunks(profile, source_path, size=max_bytes, chunk_size=_MEDIA_INFO_CHUNK_BYTES): + if written >= max_bytes: + break + data = bytes(chunk[: max(0, max_bytes - written)]) + tmp.write(data) + written += len(data) return tmp_path, written except Exception: try: @@ -453,7 +435,7 @@ def torrent_file_media_info(profile: dict, torrent_hash: str, index: int, max_by name = str(selected.get("path") or remote_path) size = int(selected.get("size") or 0) - err = remote_file_readability_error(profile, remote_path) if int(profile.get("is_remote") or 0) else None + err = remote_file_readability_error(profile, remote_path) if err: raise RuntimeError(err)