mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
Merge pull request #1035 from rommapp/misc/use-async-stream-zip
misc: Use async interface for stream-zip
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import os
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import AsyncIterator
|
||||
from datetime import datetime
|
||||
from shutil import rmtree
|
||||
from stat import S_IFREG
|
||||
from typing import Annotated
|
||||
from urllib.parse import quote
|
||||
|
||||
from anyio import open_file
|
||||
from anyio import Path, open_file
|
||||
from config import (
|
||||
DISABLE_DOWNLOAD_ENDPOINT_AUTH,
|
||||
LIBRARY_BASE_PATH,
|
||||
@@ -30,7 +29,7 @@ from handler.filesystem import fs_resource_handler, fs_rom_handler
|
||||
from handler.filesystem.base_handler import CoverSize
|
||||
from handler.metadata import meta_igdb_handler, meta_moby_handler
|
||||
from logger.logger import log
|
||||
from stream_zip import ZIP_AUTO, stream_zip # type: ignore[import]
|
||||
from stream_zip import NO_COMPRESSION_32, ZIP_AUTO, AsyncMemberFile, async_stream_zip
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -185,7 +184,7 @@ def head_rom_content(request: Request, id: int, file_name: str):
|
||||
|
||||
|
||||
@protected_route(router.get, "/roms/{id}/content/{file_name}", ["roms.read"])
|
||||
def get_rom_content(
|
||||
async def get_rom_content(
|
||||
request: Request,
|
||||
id: int,
|
||||
file_name: str,
|
||||
@@ -222,39 +221,41 @@ def get_rom_content(
|
||||
)
|
||||
|
||||
# Builds a generator of tuples for each member file
|
||||
def local_files():
|
||||
def contents(filename: str) -> Iterator[bytes]:
|
||||
async def local_files() -> AsyncIterator[AsyncMemberFile]:
|
||||
async def contents(filename: str) -> AsyncIterator[bytes]:
|
||||
try:
|
||||
with open(f"{rom_path}/{filename}", "rb") as f:
|
||||
while chunk := f.read(65536):
|
||||
async with await open_file(f"{rom_path}/{filename}", "rb") as f:
|
||||
while chunk := await f.read(65536):
|
||||
yield chunk
|
||||
except FileNotFoundError:
|
||||
log.error(f"File {rom_path}/{filename} not found!")
|
||||
raise
|
||||
|
||||
m3u_file = [str.encode(f"{file}\n") for file in files_to_download]
|
||||
async def m3u_file() -> AsyncIterator[bytes]:
|
||||
for file in files_to_download:
|
||||
yield str.encode(f"{file}\n")
|
||||
|
||||
now = datetime.now()
|
||||
|
||||
return [
|
||||
(
|
||||
for f in files_to_download:
|
||||
file_size = (await Path(f"{rom_path}/{f}").stat()).st_size
|
||||
yield (
|
||||
f,
|
||||
now,
|
||||
S_IFREG | 0o600,
|
||||
ZIP_AUTO(os.path.getsize(f"{rom_path}/{f}"), level=0),
|
||||
ZIP_AUTO(file_size, level=0),
|
||||
contents(f),
|
||||
)
|
||||
for f in files_to_download
|
||||
] + [
|
||||
(
|
||||
f"{file_name}.m3u",
|
||||
now,
|
||||
S_IFREG | 0o600,
|
||||
ZIP_AUTO(sum([len(f) for f in m3u_file]), level=0),
|
||||
m3u_file,
|
||||
)
|
||||
]
|
||||
|
||||
zipped_chunks = stream_zip(local_files())
|
||||
yield (
|
||||
f"{file_name}.m3u",
|
||||
now,
|
||||
S_IFREG | 0o600,
|
||||
NO_COMPRESSION_32,
|
||||
m3u_file(),
|
||||
)
|
||||
|
||||
zipped_chunks = async_stream_zip(local_files())
|
||||
|
||||
# Streams the zip file to the client
|
||||
return CustomStreamingResponse(
|
||||
|
||||
12
poetry.lock
generated
12
poetry.lock
generated
@@ -2157,21 +2157,21 @@ starlette = ">=0.14.2"
|
||||
|
||||
[[package]]
|
||||
name = "stream-zip"
|
||||
version = "0.0.71"
|
||||
version = "0.0.81"
|
||||
description = "Python function to construct a ZIP archive with stream processing - without having to store the entire ZIP in memory or disk"
|
||||
optional = false
|
||||
python-versions = ">=3.6.7"
|
||||
files = [
|
||||
{file = "stream_zip-0.0.71-py3-none-any.whl", hash = "sha256:4afff6da680876b7982afedcc9c3478a575a519c74015a25a4248d3fe69a46a0"},
|
||||
{file = "stream_zip-0.0.71.tar.gz", hash = "sha256:fd85a01e8eadec4d1151b548c7da0eeecc37432f29df01ba232f672f958a2fc6"},
|
||||
{file = "stream_zip-0.0.81-py3-none-any.whl", hash = "sha256:29ffcad046e16219da16b57e5129bd4e2bc9a9acce222556de6ffe0a27a748f9"},
|
||||
{file = "stream_zip-0.0.81.tar.gz", hash = "sha256:ad5aa0dc8c21c014073ca27eb4b28fe8b5e2108ccab8b099462362c1ec973e0f"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
pycryptodome = ">=3.10.1"
|
||||
|
||||
[package.extras]
|
||||
ci = ["coverage (==6.2)", "pycryptodome (==3.10.1)", "pytest (==7.0.1)", "pytest-cov (==3.0.0)", "pyzipper (==0.3.6)", "stream-unzip (==0.0.86)"]
|
||||
dev = ["coverage (>=6.2)", "pytest (>=7.0.1)", "pytest-cov (>=3.0.0)", "pyzipper (>=0.3.6)", "stream-unzip (>=0.0.86)"]
|
||||
ci = ["coverage (==6.2)", "mypy (==0.971)", "pycryptodome (==3.10.1)", "pytest (==7.0.1)", "pytest-cov (==3.0.0)", "pyzipper (==0.3.6)", "stream-unzip (==0.0.86)", "types-contextvars (==2.4.7.3)"]
|
||||
dev = ["coverage (>=6.2)", "mypy (>=0.971)", "pytest (>=7.0.1)", "pytest-cov (>=3.0.0)", "pyzipper (>=0.3.6)", "stream-unzip (>=0.0.86)", "types-contextvars (>=2.4.7.3)"]
|
||||
|
||||
[[package]]
|
||||
name = "tornado"
|
||||
@@ -2695,4 +2695,4 @@ multidict = ">=4.0"
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.11"
|
||||
content-hash = "d4564c67bc8f59fcaa0d4e1e4a6cc02de669f6a6d99dec3e2b0f8147e1a6152e"
|
||||
content-hash = "f8d8c12320cb8e5b47b182c507b4c988185910e60f7fb162f9508685072f2214"
|
||||
|
||||
@@ -30,7 +30,7 @@ types-pyyaml = "^6.0.12.20240311"
|
||||
types-redis = "^4.6.0.20240311 "
|
||||
passlib = { extras = ["bcrypt"], version = "^1.7.4" }
|
||||
itsdangerous = "^2.1.2"
|
||||
stream-zip = "^0.0.71"
|
||||
stream-zip = "^0.0.81"
|
||||
rq-scheduler = "^0.13.1"
|
||||
starlette-csrf = "^3.0.0"
|
||||
httpx = "^0.27.0"
|
||||
|
||||
Reference in New Issue
Block a user