fix in download torrents
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user