feat: add automatic media normalization, R2 retention cleanup, and seamless playout loops
This commit is contained in:
1 parent
e15d89edfe
commit
06ab5b4655
16 files changed
+1103
-408
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"chat/src/auth"
|
||||
"chat/src/config"
|
||||
"chat/src/db"
|
||||
"chat/src/media"
|
||||
"chat/src/ws"
|
||||
)
|
||||
|
||||
@@ -944,4 +946,133 @@ func TestHandleAdminMediaUploads_InvalidFile(t *testing.T) {
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeMediaRecord_Success(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
os.Setenv("MEDIA_DIR", tmpDir)
|
||||
defer os.Unsetenv("MEDIA_DIR")
|
||||
|
||||
srcPath := filepath.Join(tmpDir, "source.mov")
|
||||
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "source.mov",
|
||||
OriginalName: "source.mov",
|
||||
Path: srcPath,
|
||||
SizeBytes: 10,
|
||||
DurationSeconds: 0,
|
||||
Status: "needs_normalization",
|
||||
Title: "source",
|
||||
})
|
||||
|
||||
origNormalize := media.NormalizeVideo
|
||||
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
|
||||
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
|
||||
if err := os.WriteFile(dst, []byte("normalized"), 0o644); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
return 120, 10, nil
|
||||
}
|
||||
|
||||
var record db.Media
|
||||
gormDB.First(&record, 1)
|
||||
normalizeMediaRecord(deps, record)
|
||||
|
||||
var updated db.Media
|
||||
gormDB.First(&updated, 1)
|
||||
if updated.Status != "ready" {
|
||||
t.Errorf("Status = %q, want ready", updated.Status)
|
||||
}
|
||||
if updated.DurationSeconds != 120 {
|
||||
t.Errorf("DurationSeconds = %d, want 120", updated.DurationSeconds)
|
||||
}
|
||||
if !strings.HasSuffix(updated.Path, ".normalized.mp4") {
|
||||
t.Errorf("Path = %q, want .normalized.mp4 suffix", updated.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeMediaRecord_Failure(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
os.Setenv("MEDIA_DIR", tmpDir)
|
||||
defer os.Unsetenv("MEDIA_DIR")
|
||||
|
||||
srcPath := filepath.Join(tmpDir, "source.mov")
|
||||
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "source.mov",
|
||||
OriginalName: "source.mov",
|
||||
Path: srcPath,
|
||||
SizeBytes: 10,
|
||||
DurationSeconds: 0,
|
||||
Status: "needs_normalization",
|
||||
Title: "source",
|
||||
})
|
||||
|
||||
origNormalize := media.NormalizeVideo
|
||||
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
|
||||
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
|
||||
return 0, 0, fmt.Errorf("ffmpeg failed")
|
||||
}
|
||||
|
||||
var record db.Media
|
||||
gormDB.First(&record, 1)
|
||||
normalizeMediaRecord(deps, record)
|
||||
|
||||
var updated db.Media
|
||||
gormDB.First(&updated, 1)
|
||||
if updated.Status != "normalization_failed" {
|
||||
t.Errorf("Status = %q, want normalization_failed", updated.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleAdminMediaByID_Normalize(t *testing.T) {
|
||||
deps, gormDB, _ := setupTestDeps(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
os.Setenv("MEDIA_DIR", tmpDir)
|
||||
defer os.Unsetenv("MEDIA_DIR")
|
||||
|
||||
srcPath := filepath.Join(tmpDir, "source.mov")
|
||||
if err := os.WriteFile(srcPath, []byte("fake video"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
gormDB.Create(&db.Media{
|
||||
Filename: "source.mov",
|
||||
OriginalName: "source.mov",
|
||||
Path: srcPath,
|
||||
SizeBytes: 10,
|
||||
DurationSeconds: 0,
|
||||
Status: "needs_normalization",
|
||||
Title: "source",
|
||||
})
|
||||
|
||||
origNormalize := media.NormalizeVideo
|
||||
t.Cleanup(func() { media.NormalizeVideo = origNormalize })
|
||||
media.NormalizeVideo = func(src, dst string) (int, int64, error) {
|
||||
if err := os.WriteFile(dst, []byte("normalized"), 0o644); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
return 90, 9, nil
|
||||
}
|
||||
|
||||
handler := HandleAdminMediaByID(deps)
|
||||
req := httptest.NewRequest("POST", "/api/admin/media/1/normalize", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler(w, req)
|
||||
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Errorf("status = %d, want 202", w.Code)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user