initial commit
This commit is contained in:
0
backend/app/services/__init__.py
Normal file
0
backend/app/services/__init__.py
Normal file
91
backend/app/services/scanner.py
Normal file
91
backend/app/services/scanner.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.models import MediaItem
|
||||
|
||||
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".avif", ".heic"}
|
||||
VIDEO_EXTENSIONS = {".mp4", ".mkv", ".mov", ".avi", ".webm", ".m4v", ".flv", ".wmv", ".ts"}
|
||||
|
||||
|
||||
def classify(path: Path) -> str | None:
|
||||
ext = path.suffix.lower()
|
||||
if ext in IMAGE_EXTENSIONS:
|
||||
return "image"
|
||||
if ext in VIDEO_EXTENSIONS:
|
||||
return "video"
|
||||
return None
|
||||
|
||||
|
||||
def hash_file(path: Path) -> str:
|
||||
h = hashlib.sha256()
|
||||
with path.open("rb") as f:
|
||||
for chunk in iter(lambda: f.read(65536), b""):
|
||||
h.update(chunk)
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
async def scan_library(library_id: int, library_path: str, db: AsyncSession) -> None:
|
||||
root = Path(library_path)
|
||||
existing = await db.execute(
|
||||
select(MediaItem).where(MediaItem.library_id == library_id)
|
||||
)
|
||||
db_items = {item.rel_path: item for item in existing.scalars().all()}
|
||||
|
||||
seen_paths: set[str] = set()
|
||||
|
||||
for file_path in root.rglob("*"):
|
||||
if not file_path.is_file():
|
||||
continue
|
||||
media_type = classify(file_path)
|
||||
if not media_type:
|
||||
continue
|
||||
|
||||
rel = str(file_path.relative_to(root))
|
||||
seen_paths.add(rel)
|
||||
|
||||
if rel in db_items:
|
||||
item = db_items[rel]
|
||||
if item.missing:
|
||||
item.missing = False
|
||||
item.updated_at = datetime.utcnow()
|
||||
else:
|
||||
file_hash = hash_file(file_path)
|
||||
# Check if this hash matches an orphaned (missing) item — file was moved while offline
|
||||
moved = await _find_by_hash(library_id, file_hash, db)
|
||||
if moved:
|
||||
moved.rel_path = rel
|
||||
moved.filename = file_path.name
|
||||
moved.missing = False
|
||||
moved.updated_at = datetime.utcnow()
|
||||
else:
|
||||
item = MediaItem(
|
||||
library_id=library_id,
|
||||
rel_path=rel,
|
||||
filename=file_path.name,
|
||||
file_hash=file_hash,
|
||||
media_type=media_type,
|
||||
size_bytes=file_path.stat().st_size,
|
||||
missing=False,
|
||||
)
|
||||
db.add(item)
|
||||
|
||||
# Mark items no longer on disk as missing
|
||||
for rel_path, item in db_items.items():
|
||||
if rel_path not in seen_paths and not item.missing:
|
||||
item.missing = True
|
||||
item.updated_at = datetime.utcnow()
|
||||
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def _find_by_hash(library_id: int, file_hash: str, db: AsyncSession) -> MediaItem | None:
|
||||
result = await db.execute(
|
||||
select(MediaItem).where(
|
||||
MediaItem.library_id == library_id,
|
||||
MediaItem.file_hash == file_hash,
|
||||
MediaItem.missing == True, # noqa: E712
|
||||
)
|
||||
)
|
||||
return result.scalars().first()
|
||||
53
backend/app/services/thumbnails.py
Normal file
53
backend/app/services/thumbnails.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
from app.config import THUMBNAIL_DIR
|
||||
|
||||
THUMB_SIZE = (400, 400)
|
||||
|
||||
|
||||
def thumbnail_path(media_id: int) -> Path:
|
||||
return THUMBNAIL_DIR / f"{media_id}.jpg"
|
||||
|
||||
|
||||
def generate_image_thumbnail(src: Path, dest: Path) -> None:
|
||||
with Image.open(src) as img:
|
||||
img.thumbnail(THUMB_SIZE)
|
||||
img = img.convert("RGB")
|
||||
img.save(dest, "JPEG", quality=85)
|
||||
|
||||
|
||||
def generate_video_thumbnail(src: Path, dest: Path) -> None:
|
||||
# Extract frame at 10% of duration
|
||||
subprocess.run(
|
||||
[
|
||||
"ffprobe", "-v", "error", "-show_entries", "format=duration",
|
||||
"-of", "default=noprint_wrappers=1:nokey=1", str(src),
|
||||
],
|
||||
capture_output=True, text=True, check=False,
|
||||
)
|
||||
subprocess.run(
|
||||
[
|
||||
"ffmpeg", "-y", "-ss", "00:00:05", "-i", str(src),
|
||||
"-vframes", "1", "-vf", f"scale={THUMB_SIZE[0]}:-1",
|
||||
str(dest),
|
||||
],
|
||||
capture_output=True, check=False,
|
||||
)
|
||||
|
||||
|
||||
def get_or_create_thumbnail(media_id: int, abs_path: str, media_type: str) -> Path | None:
|
||||
dest = thumbnail_path(media_id)
|
||||
if dest.exists():
|
||||
return dest
|
||||
src = Path(abs_path)
|
||||
if not src.exists():
|
||||
return None
|
||||
try:
|
||||
if media_type == "image":
|
||||
generate_image_thumbnail(src, dest)
|
||||
else:
|
||||
generate_video_thumbnail(src, dest)
|
||||
return dest if dest.exists() else None
|
||||
except Exception:
|
||||
return None
|
||||
140
backend/app/services/watcher.py
Normal file
140
backend/app/services/watcher.py
Normal file
@@ -0,0 +1,140 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler, FileMovedEvent, FileCreatedEvent, FileDeletedEvent, DirMovedEvent
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.models import Library, MediaItem
|
||||
from app.services.scanner import classify, hash_file
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
_observers: dict[int, Observer] = {}
|
||||
|
||||
|
||||
class LibraryEventHandler(FileSystemEventHandler):
|
||||
def __init__(self, library_id: int, library_path: str):
|
||||
self.library_id = library_id
|
||||
self.root = Path(library_path)
|
||||
|
||||
def _rel(self, abs_path: str) -> str:
|
||||
return str(Path(abs_path).relative_to(self.root))
|
||||
|
||||
def on_moved(self, event):
|
||||
asyncio.run(self._handle_move(event))
|
||||
|
||||
def on_created(self, event):
|
||||
if not event.is_directory:
|
||||
asyncio.run(self._handle_create(event.src_path))
|
||||
|
||||
def on_deleted(self, event):
|
||||
if not event.is_directory:
|
||||
asyncio.run(self._handle_delete(event.src_path))
|
||||
|
||||
async def _handle_move(self, event):
|
||||
async with SessionLocal() as db:
|
||||
if isinstance(event, DirMovedEvent):
|
||||
old_prefix = self._rel(event.src_path)
|
||||
new_prefix = self._rel(event.dest_path)
|
||||
result = await db.execute(
|
||||
select(MediaItem).where(
|
||||
MediaItem.library_id == self.library_id,
|
||||
MediaItem.rel_path.startswith(old_prefix + "/"),
|
||||
)
|
||||
)
|
||||
for item in result.scalars().all():
|
||||
item.rel_path = new_prefix + item.rel_path[len(old_prefix):]
|
||||
item.updated_at = datetime.utcnow()
|
||||
else:
|
||||
src_rel = self._rel(event.src_path)
|
||||
dest_rel = self._rel(event.dest_path)
|
||||
result = await db.execute(
|
||||
select(MediaItem).where(
|
||||
MediaItem.library_id == self.library_id,
|
||||
MediaItem.rel_path == src_rel,
|
||||
)
|
||||
)
|
||||
item = result.scalars().first()
|
||||
if item:
|
||||
item.rel_path = dest_rel
|
||||
item.filename = Path(event.dest_path).name
|
||||
item.missing = False
|
||||
item.updated_at = datetime.utcnow()
|
||||
await db.commit()
|
||||
|
||||
async def _handle_create(self, abs_path: str):
|
||||
path = Path(abs_path)
|
||||
media_type = classify(path)
|
||||
if not media_type:
|
||||
return
|
||||
rel = self._rel(abs_path)
|
||||
async with SessionLocal() as db:
|
||||
existing = await db.execute(
|
||||
select(MediaItem).where(
|
||||
MediaItem.library_id == self.library_id,
|
||||
MediaItem.rel_path == rel,
|
||||
)
|
||||
)
|
||||
if existing.scalars().first():
|
||||
return
|
||||
file_hash = hash_file(path)
|
||||
item = MediaItem(
|
||||
library_id=self.library_id,
|
||||
rel_path=rel,
|
||||
filename=path.name,
|
||||
file_hash=file_hash,
|
||||
media_type=media_type,
|
||||
size_bytes=path.stat().st_size,
|
||||
)
|
||||
db.add(item)
|
||||
await db.commit()
|
||||
|
||||
async def _handle_delete(self, abs_path: str):
|
||||
rel = self._rel(abs_path)
|
||||
async with SessionLocal() as db:
|
||||
result = await db.execute(
|
||||
select(MediaItem).where(
|
||||
MediaItem.library_id == self.library_id,
|
||||
MediaItem.rel_path == rel,
|
||||
)
|
||||
)
|
||||
item = result.scalars().first()
|
||||
if item:
|
||||
item.missing = True
|
||||
item.updated_at = datetime.utcnow()
|
||||
await db.commit()
|
||||
|
||||
|
||||
def start_watcher(library_id: int, library_path: str):
|
||||
if library_id in _observers:
|
||||
return
|
||||
handler = LibraryEventHandler(library_id, library_path)
|
||||
observer = Observer()
|
||||
observer.schedule(handler, library_path, recursive=True)
|
||||
observer.start()
|
||||
_observers[library_id] = observer
|
||||
log.info("Started watcher for library %d at %s", library_id, library_path)
|
||||
|
||||
|
||||
def stop_watcher(library_id: int):
|
||||
observer = _observers.pop(library_id, None)
|
||||
if observer:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
log.info("Stopped watcher for library %d", library_id)
|
||||
|
||||
|
||||
async def start_all():
|
||||
async with SessionLocal() as db:
|
||||
result = await db.execute(select(Library))
|
||||
libraries = result.scalars().all()
|
||||
for lib in libraries:
|
||||
start_watcher(lib.id, lib.path)
|
||||
|
||||
|
||||
async def stop_all():
|
||||
for library_id in list(_observers.keys()):
|
||||
stop_watcher(library_id)
|
||||
Reference in New Issue
Block a user