feat(api): upload queue with quota, tus intake, processing and import (M5.5)
ci / api / api (push) Successful in 46s
ci / contract / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 1m3s
ci / mobile / mobile (Metro-бандл Android) (push) Successful in 1m22s
ci / ts / ts (packages + web) (push) Successful in 1m38s

- Migration 00008: upload_batches, upload_files, upload_quota.
- Quota reserved under a row lock when a batch is created; every status
  change goes through one transition that frees space in the same
  transaction. SHA-256 duplicates are rejected before any upload.
- tus 1.0.0 core (HEAD/PATCH) for the author only; offset is the real
  staging file size, data beyond the declared size is refused (ADR-015).
- Processing worker: ffprobe checks, SHA-256 verification, exact and
  Chromaprint duplicates against library and queue, metadata from tags
  or the dropped folder path, possible_duplicate on metadata match, and
  the target path from write_template reusing existing folder spelling.
- Import worker: edited tags written with ffmpeg stream copy, file
  written by storage-agent, point scan of the new file, imported status;
  idempotent and resumed after restart. Stale uploads expire after 24 h,
  orphan staging files are removed at startup.
- API: quota, precheck, batches, my uploads, withdraw, admin list,
  metadata edit, bulk review, retry and queue settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mrleo1nidandClaude Opus 5.5 committed 2026-10-20 17:23:05 +03:00
1 parent f66be2ba0a
commit ca106e8eb1
21 files changed
+5552 -198

No files matched your search

+43
View File
@@ -271,6 +271,49 @@ func (sc *Scanner) run(ctx context.Context, src Source, root string, full bool)
return sc.store.SetLastScan(ctx, src.ID, sc.now())
}
// ScanFile — точечный скан одного файла (scan_path): после импорта из очереди трек сразу появляется в каталоге.
// Возвращает id записи media_files. Хуки AfterScan (обложки, поиск, отпечатки) вызывает сам.
func (sc *Scanner) ScanFile(ctx context.Context, src Source, root, rel string) (uuid.UUID, error) {
rows, err := sc.store.Patterns(ctx, src.ID)
if err != nil {
return uuid.Nil, err
}
var exprs []string
for _, r := range rows {
if r.IsEnabled {
exprs = append(exprs, r.Regex)
}
}
patterns, err := meta.CompilePatterns(exprs)
if err != nil {
return uuid.Nil, fmt.Errorf("library: patterns: %w", err)
}
info, err := os.Stat(filepath.Join(root, filepath.FromSlash(rel)))
if err != nil {
return uuid.Nil, fmt.Errorf("library: scan file: %w", err)
}
j := job{walked: walked{rel: rel, size: info.Size(), mtime: info.ModTime().UTC().Truncate(time.Microsecond)}}
if f, ok, err := sc.store.FileByPath(ctx, src.ID, rel); err != nil {
return uuid.Nil, err
} else if ok {
j.fileID, j.oldTrack = f.ID, f.TrackID
}
if err := sc.process(ctx, src, root, patterns, []job{j}); err != nil {
return uuid.Nil, err
}
f, ok, err := sc.store.FileByPath(ctx, src.ID, rel)
if err != nil {
return uuid.Nil, err
}
if !ok {
return uuid.Nil, fmt.Errorf("library: scanned file %s not saved", rel)
}
if sc.AfterScan != nil {
go sc.AfterScan(context.WithoutCancel(ctx))
}
return f.ID, nil
}
// process читает файлы параллельно, а каталог обновляет один писатель —
// так дубли и новые исполнители не создаются дважды в гонке.
func (sc *Scanner) process(ctx context.Context, src Source, root string, patterns []meta.Pattern, jobs []job) error {
+16
View File
@@ -244,6 +244,22 @@ func (st *Store) Files(ctx context.Context, sourceID uuid.UUID) (map[string]File
return out, rows.Err()
}
// FileByPath — файл источника по пути (точечный скан).
func (st *Store) FileByPath(ctx context.Context, sourceID uuid.UUID, rel string) (FileRow, bool, error) {
var f FileRow
err := st.pool.QueryRow(ctx, `
SELECT id, rel_path, size_bytes, mtime, content_hash, track_id, missing_since IS NOT NULL
FROM media_files WHERE source_id = $1 AND rel_path = $2`, sourceID, rel).
Scan(&f.ID, &f.RelPath, &f.Size, &f.Mtime, &f.ContentHash, &f.TrackID, &f.Missing)
if errors.Is(err, pgx.ErrNoRows) {
return FileRow{}, false, nil
}
if err != nil {
return FileRow{}, false, fmt.Errorf("library: file by path: %w", err)
}
return f, true, nil
}
// SampleFiles — случайная выборка путей источника (для предпросмотра шаблонов).
func (st *Store) SampleFiles(ctx context.Context, sourceID uuid.UUID, n int) ([]string, error) {
rows, err := st.pool.Query(ctx, `