# Media Table Integration **Task**: AZ-175_media_table_integration **Name**: Integrate Media table: create record on upload, store file, track status **Description**: When a file is uploaded to the detections API, create a Media record in the DB, store the file at the proper path, and update MediaStatus throughout processing. **Complexity**: 2 points **Dependencies**: Annotations service needs Media CRUD endpoints **Component**: Media Management **Jira**: AZ-175 **Parent**: AZ-172 ## Problem Currently, uploaded files are written to temp files, processed, and deleted. No `Media` record is created in the database. File persistence and status tracking are missing. ## Current State - `/detect`: writes upload to `tempfile.NamedTemporaryFile`, processes, deletes via `os.unlink` - `/detect/{media_id}`: accepts a media_id parameter but doesn't create or manage Media records - No XxHash64 ID generation in the detections module - No file storage to persistent paths ## Target State ### On Upload 1. Receive file bytes from HTTP upload 2. Compute XxHash64 of file content using the sampling algorithm 3. Determine MediaType from file extension (Video or Image) 4. Store file at proper path (from DirectorySettings: VideosDir or ImagesDir) 5. Create Media record via annotations service: `POST /api/media` - Id: XxHash64 hex string - Name: original filename - Path: storage path - MediaType: Video|Image - MediaStatus: New (1) - UserId: from JWT ### During Processing 6. Update MediaStatus to AIProcessing (2) via `PUT /api/media/{id}/status` 7. Run detection (stream-based per AZ-173) 8. Update MediaStatus to AIProcessed (3) on success, or Error (6) on failure ## XxHash64 Sampling Algorithm ``` For files >= 3072 bytes: Input = file_size_as_8_bytes + first_1024_bytes + middle_1024_bytes + last_1024_bytes Output = XxHash64(input) as hex string For files < 3072 bytes: Input = file_size_as_8_bytes + entire_file_content Output = XxHash64(input) as hex string ``` Virtual hashes (in-memory only) prefixed with "V". ## Acceptance Criteria - [ ] XxHash64 ID computed correctly using the sampling algorithm - [ ] Media record created in DB on upload with correct fields - [ ] File stored at proper persistent path (not temp) - [ ] MediaStatus transitions: New → AIProcessing → AIProcessed (or Error) - [ ] UserId correctly extracted from JWT and associated with Media record ## File Changes | File | Action | Description | |------|--------|-------------| | `src/main.py` | Modified | Upload handling, Media API calls, status updates | | `src/media_hash.py` | New | XxHash64 sampling hash utility | | `requirements.txt` | Modified | Add `xxhash` library if not present |