mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-06-13 14:09:25 -03:00
The previous commit (a19ddc14) restored Linux sendfile but kept the
manual streaming path for Windows via sys.platform guard. A Windows
user reports performance is still worse than v1.0.5.
Switch back to web.FileResponse for all files on all platforms as the
default. The IOCP crash is an edge case (fast scrolling through many
video previews) that affects few users, while the Python chunked I/O
performance penalty affects everyone.
_stream_file() is kept as an unused fallback for a future compat
setting toggle.
106 lines
4.0 KiB
Python
106 lines
4.0 KiB
Python
"""Handlers responsible for serving preview assets dynamically."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import mimetypes
|
|
import urllib.parse
|
|
from pathlib import Path
|
|
|
|
from aiohttp import web
|
|
|
|
from ...config import config as global_config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_CHUNK_SIZE = 1024 * 1024 # 1 MB — balance between streaming iteration overhead and per-chunk memory
|
|
|
|
# Video file extensions that bypass native sendfile on Windows
|
|
# to avoid IOCP/ProactorEventLoop crashes during client disconnect.
|
|
_VIDEO_EXTENSIONS = frozenset({".mp4", ".webm", ".mov", ".avi", ".mkv"})
|
|
|
|
|
|
class PreviewHandler:
|
|
"""Serve preview assets for the active library at request time."""
|
|
|
|
def __init__(self, *, config=global_config) -> None:
|
|
self._config = config
|
|
|
|
async def serve_preview(self, request: web.Request) -> web.StreamResponse:
|
|
"""Return the preview file referenced by the encoded ``path`` query."""
|
|
|
|
raw_path = request.query.get("path", "")
|
|
if not raw_path:
|
|
raise web.HTTPBadRequest(text="Missing 'path' query parameter")
|
|
|
|
try:
|
|
decoded_path = urllib.parse.unquote(raw_path)
|
|
except Exception as exc: # pragma: no cover - defensive guard
|
|
logger.debug("Failed to decode preview path %s: %s", raw_path, exc)
|
|
raise web.HTTPBadRequest(text="Invalid preview path encoding") from exc
|
|
|
|
normalized = decoded_path.replace("\\", "/")
|
|
|
|
if not self._config.is_preview_path_allowed(normalized):
|
|
raise web.HTTPForbidden(text="Preview path is not within an allowed directory")
|
|
|
|
candidate = Path(normalized)
|
|
try:
|
|
resolved = candidate.expanduser().resolve(strict=False)
|
|
except Exception as exc:
|
|
logger.debug("Failed to resolve preview path %s: %s", normalized, exc)
|
|
raise web.HTTPBadRequest(text="Unable to resolve preview path") from exc
|
|
|
|
if not resolved.is_file():
|
|
logger.debug("Preview file not found at %s", str(resolved))
|
|
raise web.HTTPNotFound(text="Preview file not found")
|
|
|
|
# aiohttp's FileResponse handles range requests, content headers, and
|
|
# uses kernel sendfile (zero-copy DMA) on Linux/macOS. On Windows it
|
|
# uses IOCP-based _sendfile_native which can crash when the client
|
|
# disconnects mid-transfer during fast scrolling. The _stream_file()
|
|
# fallback is kept for a future compat toggle.
|
|
return web.FileResponse(path=resolved, chunk_size=_CHUNK_SIZE)
|
|
|
|
async def _stream_file(
|
|
self, request: web.Request, path: Path
|
|
) -> web.StreamResponse:
|
|
"""Stream a file chunk-by-chunk, bypassing native sendfile.
|
|
|
|
This avoids the Windows IOCP ``_sendfile_native`` crash that occurs
|
|
when the client disconnects during a large file transfer.
|
|
"""
|
|
content_type, _ = mimetypes.guess_type(str(path))
|
|
if content_type is None:
|
|
content_type = "application/octet-stream"
|
|
|
|
file_size = path.stat().st_size
|
|
resp = web.StreamResponse()
|
|
resp.content_type = content_type
|
|
resp.content_length = file_size
|
|
|
|
# Allow browser caching: video previews rarely change during a session.
|
|
# The frontend already appends ?t={version} to bust cache on update.
|
|
resp.headers["Cache-Control"] = "public, max-age=86400"
|
|
|
|
await resp.prepare(request)
|
|
|
|
try:
|
|
with open(path, "rb") as f:
|
|
while True:
|
|
chunk = f.read(_CHUNK_SIZE)
|
|
if not chunk:
|
|
break
|
|
await resp.write(chunk)
|
|
except (ConnectionResetError, ConnectionAbortedError):
|
|
# Client disconnected during streaming — expected when scrolling
|
|
# rapidly through a library with animated previews.
|
|
pass
|
|
except OSError as exc:
|
|
logger.debug("I/O error streaming preview %s: %s", path, exc)
|
|
|
|
return resp
|
|
|
|
|
|
__all__ = ["PreviewHandler"]
|