# AGENTS.md ## Project Overview MediaLore is a **self-hosted media library browser** for images and videos. It provides a web UI for browsing, searching, tagging, and viewing media with thumbnail previews and doom-scroll/TikTok-style consumption. **All access requires authentication** — users must log in before accessing any media or UI pages. **Monorepo** with two independent apps: - `backend/` — Python FastAPI server - `frontend/` — React SPA (TypeScript, Vite) ## Tech Stack | Layer | Technology | |-------------|---------------------------------------------------------| | Backend | Python 3.12+, FastAPI, Uvicorn | | Database | SQLite via SQLAlchemy (async aiosqlite), WAL mode | | Migrations | Alembic | | Auth | JWT (python-jose) + bcrypt password hashing | | Thumbnails | Pillow (images), ffmpeg (videos) — generated on-demand | | File Watch | watchdog — live filesystem monitoring | | Frontend | React 19, TypeScript 6, Vite 8, React Router 7, TanStack Query v5 | | Infra | Docker Compose (backend: Python 3.12-slim, frontend: Nginx Alpine) | ## Directory Structure ``` medialore-web-app/ ├── docker-compose.yml # Orchestration (backend, frontend, volumes) ├── .env.example # Environment variable template ├── .gitea/ │ └── workflows/ │ └── container-publish.yml # CI: build & push Docker images ├── backend/ │ ├── Dockerfile │ ├── pyproject.toml # Python package config (hatchling build) │ ├── alembic.ini │ ├── alembic/ # DB migrations │ │ ├── env.py │ │ └── versions/ │ └── app/ │ ├── main.py # FastAPI app, CORS, lifespan, router registration │ ├── config.py # Pydantic Settings (DATABASE_URL, MEDIA_ROOT, etc.) │ ├── database.py # SQLAlchemy async engine, session, WAL pragma │ ├── models.py # ORM models: Library, MediaItem, Tag, User, media_item_tags │ ├── schemas.py # Pydantic request/response schemas (incl. auth) │ ├── auth.py # bcrypt hashing, JWT create/decode, auth dependencies │ ├── routers/ │ │ ├── auth.py # Login, token validation, user CRUD (admin) │ │ ├── libraries.py # Library CRUD, browse, doom-scroll, scan-status, rescan │ │ ├── media.py # Media item get, file serve, thumbnail, tag assignment │ │ ├── tags.py # Tag CRUD, grouped by category │ │ └── search.py # Fuzzy filename + tag search │ └── services/ │ ├── scanner.py # Dir walk, SHA-256 hashing, moved file detection │ ├── watcher.py # watchdog event handler (create/move/delete) │ └── thumbnails.py # Pillow + ffmpeg thumbnail generation └── frontend/ ├── Dockerfile # Multi-stage: Node build + Nginx serve ├── nginx.conf # SPA fallback, /api proxy to backend ├── package.json ├── tsconfig.json # Root references ├── tsconfig.app.json # App TS config ├── tsconfig.node.json # Vite config TS config ├── vite.config.ts # Vite + React plugin, dev proxy /api -> localhost:8000 ├── eslint.config.js # Flat config: JS + TS + React hooks + refresh ├── index.html ├── public/ │ ├── favicon.svg │ └── icons.svg └── src/ ├── main.tsx # React entry point ├── App.tsx # Router, QueryClient, sidebar, theme, responsive layout ├── index.css # CSS custom properties for light/dark themes ├── api/ │ └── client.ts # Typed fetch wrapper; all endpoint functions ├── auth/ │ ├── AuthTypes.ts # AuthUser, AuthState interfaces, AuthContext │ ├── AuthContext.tsx # AuthProvider component (login/logout/token mgmt) │ └── useAuth.ts # useAuth hook ├── pages/ │ ├── LoginPage.tsx │ ├── SearchPage.tsx │ ├── BrowserPage.tsx │ ├── SettingsPage.tsx ├── components/ │ ├── FileBrowser/FileBrowser.tsx │ ├── MediaViewer/MediaViewer.tsx │ ├── DoomScrollViewer/DoomScrollViewer.tsx │ └── TagPanel/TagPanel.tsx └── assets/ ├── hero.png └── vite.svg ``` ## Common Commands ### Backend ```bash # Install (from backend/) pip install -e . # Run dev server cd backend && uvicorn app.main:app --reload --port 8000 # Generate a new Alembic migration cd backend && alembic revision --autogenerate -m "description" # Apply migrations cd backend && alembic upgrade head ``` ### Frontend ```bash # Install dependencies (from frontend/) npm install # Dev server (proxies /api to localhost:8000) npm run dev # Production build npm run build # Lint npm run lint # Preview production build npm run preview ``` ### Docker ```bash docker compose up -d ``` ## Environment Variables | Variable | Default | Purpose | |-------------------------|-------------------------------------|------------------------------| | `DATABASE_URL` | `sqlite+aiosqlite:////data/medialore.db` | SQLite connection string | | `MEDIA_ROOT` | `/media` | Root path for media libraries | | `THUMBNAIL_DIR` | `/data/thumbnails` | Cached thumbnail storage | | `SECRET_KEY` | auto-generated | JWT signing key | | `ADMIN_USERNAME` | `admin` | Initial admin user | | `ADMIN_PASSWORD` | (required) | Initial admin password | | `ACCESS_TOKEN_EXPIRE_DAYS` | `30` | JWT token lifetime in days | Pydantic Settings reads from `.env` at startup (via `model_config = {"env_file": ".env"}`). For Docker, these are set in `docker-compose.yml`. ## Database SQLite with **WAL mode** and a 10-second busy timeout (see `app/database.py:14-18`). Tables are auto-created at startup via `Base.metadata.create_all` in the FastAPI lifespan. ### Schema - **libraries**: `id`, `name`, `path` (unique) - **media_items**: `id`, `library_id` (FK), `rel_path`, `filename`, `file_hash` (SHA-256), `media_type` ("image" or "video"), `size_bytes`, `missing`, `created_at`, `updated_at` — unique on `(library_id, rel_path)` - **tags**: `id`, `name`, `category` — unique on `(name, category)` - **media_item_tags**: `media_item_id` (FK), `tag_id` (FK), composite PK - **users**: `id`, `username` (unique), `password_hash`, `is_admin`, `created_at` **Migrations**: Use Alembic. The initial migration is at `backend/alembic/versions/0001_initial_schema.py` and the users table migration at `0002_add_users_table.py`. ## API Conventions All endpoints are prefixed with `/api`. All responses are JSON except `/api/media/:id/file` (binary stream) and `/api/media/:id/thumbnail` (JPEG). **All endpoints require authentication** except `POST /api/auth/login`. The `Authorization: Bearer ` header must be included. File and thumbnail endpoints also accept `?token=` as a query parameter (for ``/`