package fbhttp import ( "bytes" "encoding/binary" "encoding/json" "net/http/httptest" "os" "os/exec" "path/filepath" "testing" "github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/users" ) func TestMediaStatusIsAdminOnlyAndChecksExecutable(t *testing.T) { perm := users.Permissions{Admin: true} key := []byte("media-status-test") store := scopedUserStorage(t, t.TempDir(), perm, key) handler := handle(mediaStatusHandler, "", store, &settings.Server{}) request := func(admin bool) *httptest.ResponseRecorder { r := httptest.NewRequest("GET", "/api/media/status", nil) rec := httptest.NewRecorder() handler.ServeHTTP(rec, authorizeTestRequest(r, signToken(t, users.Permissions{Admin: admin}, key))) return rec } if rec := request(false); rec.Code != 403 { t.Fatalf("nonadmin: %d", rec.Code) } _, ffmpegErr := exec.LookPath("ffmpeg") rec := request(true) var status struct { Available bool `json:"available"` Version string `json:"version"` } if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &status) != nil { t.Fatalf("status: %d", rec.Code) } if ffmpegErr == nil && (!status.Available || status.Version == "") { t.Fatal("installed FFmpeg was not detected") } t.Setenv("PATH", t.TempDir()) rec = request(true) if json.Unmarshal(rec.Body.Bytes(), &status) != nil || status.Available { t.Fatal("missing FFmpeg reported as available") } } func TestMediaPermissionsAndConversion(t *testing.T) { root := t.TempDir() key := []byte("media-test") perm := users.Permissions{Download: true} store := scopedUserStorage(t, root, perm, key) handler := handle(mediaHandler, "", store, &settings.Server{}) request := func(target string) *httptest.ResponseRecorder { r := httptest.NewRequest("GET", target, nil) rec := httptest.NewRecorder() handler.ServeHTTP(rec, authorizeTestRequest(r, signToken(t, perm, key))) return rec } for target, status := range map[string]int{ "/api/media/play?path=/../private.wav": 400, "/api/media/play?path=/playlist.m3u": 415, "/api/media/play?path=https://example.com/test.mp3": 400, "/api/media/play?path=/missing.wav": 404, } { if rec := request(target); rec.Code != status { t.Fatalf("%s: want %d, got %d", target, status, rec.Code) } } wave := make([]byte, 44+9600) copy(wave, "RIFF") binary.LittleEndian.PutUint32(wave[4:], uint32(len(wave)-8)) copy(wave[8:], "WAVEfmt ") binary.LittleEndian.PutUint32(wave[16:], 16) binary.LittleEndian.PutUint16(wave[20:], 1) binary.LittleEndian.PutUint16(wave[22:], 1) binary.LittleEndian.PutUint32(wave[24:], 48000) binary.LittleEndian.PutUint32(wave[28:], 96000) binary.LittleEndian.PutUint16(wave[32:], 2) binary.LittleEndian.PutUint16(wave[34:], 16) copy(wave[36:], "data") binary.LittleEndian.PutUint32(wave[40:], 9600) os.WriteFile(filepath.Join(root, "audio.wav"), wave, 0600) user, _ := store.Users.Get("", false, uint(1)) user.Perm.Download = false store.Users.Save(user) if rec := request("/api/media/play?path=/audio.wav&audio=true"); rec.Code != 403 { t.Fatalf("download denied: %d", rec.Code) } user.Perm.Download = true store.Users.Save(user) if _, err := exec.LookPath("ffmpeg"); err != nil { if rec := request("/api/media/play?path=/audio.wav&audio=true"); rec.Code != 503 { t.Fatalf("missing FFmpeg: %d", rec.Code) } return } rec := request("/api/media/play?path=/audio.wav&audio=true") if rec.Code != 200 || rec.Body.Len() < 100 || rec.Header().Get("Content-Type") != "audio/mpeg" { t.Fatalf("audio transcode: %d, %d bytes, %s", rec.Code, rec.Body.Len(), rec.Body.String()) } original, _ := os.ReadFile(filepath.Join(root, "audio.wav")) if !bytes.Equal(original, wave) { t.Fatal("original changed") } video := filepath.Join(root, "video.avi") if output, err := exec.Command("ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "color=size=64x64:rate=10:color=blue", "-t", "0.2", "-c:v", "mpeg4", video).CombinedOutput(); err != nil { t.Fatalf("fixture: %v %s", err, output) } rec = request("/api/media/play?path=/video.avi") if rec.Code != 200 || !bytes.Contains(rec.Body.Bytes(), []byte("ftyp")) || rec.Header().Get("Content-Type") != "video/mp4" { t.Fatalf("video transcode: %d %s", rec.Code, rec.Body.String()) } }