Files
Showy/docs-vault/Media System.md
T

105 lines
3.1 KiB
Markdown

---
title: Media System
tags:
- backend
- media
- images
---
# Media System
## Overview
Showy downloads images from TMDB and stores them locally. Each image gets a UUID filename and a low-res preview. The [[Models#Media|Media model]] is polymorphic — it can associate with entries, seasons, episodes, collections, or people.
## Pipeline
```mermaid
graph LR
TMDB[TMDB Image API] --> DL[mediaDownloaderService]
DL --> |UUID filename| FS[Local Filesystem]
DL --> OPT[mediaOptimizerService]
OPT --> |_preview suffix| FS
DL --> DB[Media.create]
DB --> MySQL[(MySQL)]
```
### Step by Step
1. **Download** — `mediaDownloaderService` fetches image from TMDB, saves with UUID filename
2. **Optimize** — `mediaOptimizerService` generates a `_preview` version using `sharp`
- JPEG for photos (posters, backdrops, stills, profiles)
- PNG for logos (transparency)
3. **Record** — `mediaService` creates a `Media` DB record with polymorphic association
4. **Serve** — Express serves files via `/api/v1/media/:category/:type/:size/:fileName`
## Storage Structure
```
{userDataPath}/images/
├── entries/
│ ├── posters/ {uuid}.jpg + {uuid}_preview.jpg
│ ├── backdrops/ {uuid}.jpg + {uuid}_preview.jpg
│ └── logos/ {uuid}.png + {uuid}_preview.png
├── collections/
│ ├── posters/
│ └── backdrops/
├── seasons/
│ └── posters/
├── episodes/
│ └── stills/
└── peoples/
└── profiles/
```
`{userDataPath}` defaults to `C:\Users\{user}\Documents\Showy Data` — configurable in [[Env|settings.json]].
## Media Model Fields
See [[Models#Media]] for full schema.
Key fields:
- `mediaable` — entity type: `'entry'`, `'people'`, `'collection'`, `'season'`, `'episode'`
- `mediaableId` — polymorphic FK
- `type` — image type: `'poster'`, `'backdrop'`, `'logo'`, `'still'`, `'profile'`
- `path` — UUID filename on disk
- `isSelected` — the "active" image displayed in UI
- `isCustom` — user-uploaded (vs TMDB-downloaded)
## Serving URLs
API pattern: `GET /api/v1/media/:category/:type/:size/:fileName`
| Parameter | Values |
|---|---|
| `category` | `entries`, `collections`, `seasons`, `episodes`, `peoples` |
| `type` | `posters`, `backdrops`, `logos`, `stills`, `profiles` |
| `size` | `preview`, `full` |
| `fileName` | UUID filename with extension |
Example: `/api/v1/media/entries/posters/preview/abc123_preview.jpg`
## User Uploads
Users can upload custom media via:
- `POST /entries/:id/media` (Multer, supports poster/backdrop/logo)
- `POST /people/:id/media` (profile pictures)
- `PUT /collections/:id` (multipart with poster/backdrop)
Upload config: `mutlerStorageLocations.js` — UUID filenames, stored in same directory structure.
## Frontend URL Resolution
See [[Media Utilities]] for how the frontend resolves which image to display:
1. Local selected media → 2. TMDB fallback → 3. Default placeholder
## Maintenance
See [[Settings Page#Cleanup]] for orphan file/record detection and removal.
- **Orphan files** = files on disk with no DB record
- **Orphan media records** = DB records pointing to missing files