fix in download torrents

This commit is contained in:
Mateusz Gruszczyński
2026-05-22 14:14:07 +02:00
parent 00a3831386
commit 7c0a4ff703

View File

@@ -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: 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)) limit = max(0, int(max_bytes or 0))
chunks: list[bytes] = [] chunks: list[bytes] = []
collected = 0 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): for chunk in iter_remote_file_chunks(profile, source_path, size=limit, chunk_size=_MEDIA_INFO_CHUNK_BYTES):
if collected >= limit: if collected >= limit:
break break
data = bytes(chunk[: max(0, limit - collected)]) data = bytes(chunk[: max(0, limit - collected)])
chunks.append(data) chunks.append(data)
collected += len(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)
return b"".join(chunks) return b"".join(chunks)
@@ -340,21 +331,12 @@ def _media_info_temp_sample(profile: dict, source_path: str, max_bytes: int) ->
written = 0 written = 0
try: try:
with os.fdopen(fd, "wb") as tmp: 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): for chunk in iter_remote_file_chunks(profile, source_path, size=max_bytes, chunk_size=_MEDIA_INFO_CHUNK_BYTES):
if written >= max_bytes: if written >= max_bytes:
break break
data = bytes(chunk[: max(0, max_bytes - written)]) data = bytes(chunk[: max(0, max_bytes - written)])
tmp.write(data) tmp.write(data)
written += len(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)
return tmp_path, written return tmp_path, written
except Exception: except Exception:
try: 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) name = str(selected.get("path") or remote_path)
size = int(selected.get("size") or 0) 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: if err:
raise RuntimeError(err) raise RuntimeError(err)