mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-16 23:11:27 +00:00
Serve search thumbnails directly from Jellyfin Trickplay
This commit is contained in:
+24
-8
@@ -5,14 +5,14 @@ import threading
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import Depends, FastAPI, Header, HTTPException, Query
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.responses import FileResponse, Response
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from .caption_jobs import CaptionJobManager
|
||||
from .config import settings
|
||||
from .database import connect, init_db
|
||||
from .frame_source import FrameSource
|
||||
from .index_jobs import IndexJobManager
|
||||
from .paths import resolve_thumb_path
|
||||
from .search_service import SearchService
|
||||
|
||||
WEB_INDEX_PATH = Path(__file__).parent / "web" / "index.html"
|
||||
@@ -25,6 +25,7 @@ app = FastAPI(
|
||||
_service: SearchService | None = None
|
||||
_index_jobs: IndexJobManager | None = None
|
||||
_caption_jobs: CaptionJobManager | None = None
|
||||
_frame_source: FrameSource | None = None
|
||||
_job_start_lock = threading.Lock()
|
||||
|
||||
|
||||
@@ -67,6 +68,13 @@ def get_caption_jobs() -> CaptionJobManager:
|
||||
return _caption_jobs
|
||||
|
||||
|
||||
def get_frame_source() -> FrameSource:
|
||||
global _frame_source
|
||||
if _frame_source is None:
|
||||
_frame_source = FrameSource()
|
||||
return _frame_source
|
||||
|
||||
|
||||
def require_api_key(
|
||||
authorization: str | None = Header(default=None),
|
||||
x_scene_search_key: str | None = Header(default=None),
|
||||
@@ -197,12 +205,14 @@ def caption_status() -> dict:
|
||||
"/api/frames/{frame_id}/thumbnail",
|
||||
dependencies=[Depends(require_api_key)],
|
||||
)
|
||||
def frame_thumbnail(frame_id: str) -> FileResponse:
|
||||
def frame_thumbnail(frame_id: str) -> Response:
|
||||
init_db(settings.db_path)
|
||||
with connect(settings.db_path) as con:
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT f.thumb_path
|
||||
SELECT f.frame_number, f.thumb_path,
|
||||
m.source_kind, m.jellyfin_item_id,
|
||||
m.jellyfin_media_source_id, m.trickplay_width
|
||||
FROM frames f
|
||||
JOIN media m ON m.id = f.media_id
|
||||
WHERE f.id = ? AND m.source_kind = 'jellyfin'
|
||||
@@ -213,11 +223,17 @@ def frame_thumbnail(frame_id: str) -> FileResponse:
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Frame not found")
|
||||
|
||||
path = resolve_thumb_path(str(row["thumb_path"]))
|
||||
if not path.is_file():
|
||||
raise HTTPException(status_code=404, detail="Thumbnail file not found")
|
||||
try:
|
||||
content = get_frame_source().jpeg_bytes(row)
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Thumbnail file not found: {exc}",
|
||||
) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||
|
||||
return FileResponse(path, media_type="image/jpeg")
|
||||
return Response(content=content, media_type="image/jpeg")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user