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
@@ -976,9 +976,9 @@ func HandleAdminMedia(deps *Dependencies) http.HandlerFunc {
|
||||
}
|
||||
|
||||
normResult := media.ValidateNormalization(targetPath)
|
||||
status := "ready"
|
||||
status := media.StatusReady
|
||||
if !normResult.Valid {
|
||||
status = "needs_normalization"
|
||||
status = media.StatusNeedsNormalization
|
||||
}
|
||||
|
||||
filename := filepath.Base(targetPath)
|
||||
@@ -998,6 +998,10 @@ func HandleAdminMedia(deps *Dependencies) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if !normResult.Valid {
|
||||
go normalizeMediaRecord(deps, mediaRecord)
|
||||
}
|
||||
|
||||
files, err := media.ListFiles(deps.DB)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to list media files: %v", err), http.StatusInternalServerError)
|
||||
@@ -1307,9 +1311,9 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
|
||||
}
|
||||
|
||||
normResult := media.ValidateNormalization(targetPath)
|
||||
status := "ready"
|
||||
status := media.StatusReady
|
||||
if !normResult.Valid {
|
||||
status = "needs_normalization"
|
||||
status = media.StatusNeedsNormalization
|
||||
}
|
||||
|
||||
filename := filepath.Base(targetPath)
|
||||
@@ -1329,6 +1333,10 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if !normResult.Valid {
|
||||
go normalizeMediaRecord(deps, mediaRecord)
|
||||
}
|
||||
|
||||
_ = os.RemoveAll(media.UploadSessionDir(mediaDir, uploadID))
|
||||
|
||||
files, err := media.ListFiles(deps.DB)
|
||||
@@ -1348,6 +1356,11 @@ func HandleAdminMediaUploads(deps *Dependencies) http.HandlerFunc {
|
||||
func HandleAdminMediaByID(deps *Dependencies) http.HandlerFunc {
|
||||
mediaDir := config.MediaDir()
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/normalize") {
|
||||
handleNormalizeMedia(deps, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
mediaID, err := media.GetMediaIDFromPath(r.URL.Path, "/api/admin/media/")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -1424,6 +1437,75 @@ func HandleAdminMediaByID(deps *Dependencies) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeMediaRecord runs ffmpeg normalization for a media record and updates
|
||||
// its status, path, duration, and size. Safe to run asynchronously.
|
||||
func normalizeMediaRecord(deps *Dependencies, record db.Media) {
|
||||
src := record.Path
|
||||
dst := strings.TrimSuffix(src, filepath.Ext(src)) + ".normalized.mp4"
|
||||
|
||||
if err := deps.DB.Model(&record).Update("status", media.StatusNormalizing).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
duration, size, err := media.NormalizeVideo(src, dst)
|
||||
if err != nil {
|
||||
_ = deps.DB.Model(&record).Updates(map[string]any{
|
||||
"status": media.StatusNormalizationFailed,
|
||||
"tags": `"normalization":false`,
|
||||
}).Error
|
||||
return
|
||||
}
|
||||
|
||||
_ = deps.DB.Model(&record).Updates(map[string]any{
|
||||
"path": dst,
|
||||
"filename": filepath.Base(dst),
|
||||
"size_bytes": size,
|
||||
"duration_seconds": duration,
|
||||
"status": media.StatusReady,
|
||||
"tags": `"normalization":true`,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// handleNormalizeMedia handles POST /api/admin/media/{id}/normalize.
|
||||
func handleNormalizeMedia(deps *Dependencies, w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
idPart := strings.TrimSuffix(r.URL.Path, "/normalize")
|
||||
idPart = strings.TrimPrefix(idPart, "/api/admin/media/")
|
||||
idPart = strings.Trim(idPart, "/")
|
||||
mediaID, err := strconv.ParseUint(idPart, 10, 64)
|
||||
if err != nil || mediaID == 0 {
|
||||
http.Error(w, "invalid media id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var record db.Media
|
||||
if err := deps.DB.First(&record, mediaID).Error; err != nil {
|
||||
http.Error(w, "media item not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if record.Status == media.StatusNormalizing {
|
||||
http.Error(w, "normalization already in progress", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
record.Status = media.StatusNormalizing
|
||||
if err := deps.DB.Model(&record).Update("status", media.StatusNormalizing).Error; err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to update media status: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
go normalizeMediaRecord(deps, record)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(mediaEntryFromRecord(record))
|
||||
}
|
||||
|
||||
// HandleAdminChatLog lists chat log entries.
|
||||
func HandleAdminChatLog(deps *Dependencies) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -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