The viewer hands a video or audio file to the browser untouched — container, codec and all — and the browser plays what it can; when it can't, the viewer says so and offers the download. What the server adds is the part a player needs: /api/files/read now goes through http.ServeContent for a real file of known size, so Range requests (seeking) and HEAD work, and a download can resume. The root backend's helper takes an offset for that, and a Seeker opens the backend once, at the range asked for, instead of streaming from the top. Audio files are a preview kind too: their cover art is the thumbnail. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
63 lines
2.2 KiB
Go
63 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"hamlab/internal/files"
|
|
)
|
|
|
|
// A file read answers Range requests — what a <video> needs to seek — and HEAD, through
|
|
// the same handler that streams everything else.
|
|
func TestFilesReadRanges(t *testing.T) {
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "clip.mp4")
|
|
data := make([]byte, 10000)
|
|
for i := range data {
|
|
data[i] = byte(i % 251)
|
|
}
|
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &Server{fsLocal: files.Local{}}
|
|
get := func(method, extra string, hdr map[string]string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, "/api/files/read?path="+url.QueryEscape(p)+extra, nil)
|
|
for k, v := range hdr {
|
|
r.Header.Set(k, v)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.filesRead(w, r)
|
|
return w
|
|
}
|
|
w := get("GET", "", nil)
|
|
if w.Code != 200 || w.Body.Len() != 10000 || w.Header().Get("Content-Type") != "video/mp4" || w.Header().Get("Accept-Ranges") != "bytes" {
|
|
t.Fatalf("whole: %d %d %q %q", w.Code, w.Body.Len(), w.Header().Get("Content-Type"), w.Header().Get("Accept-Ranges"))
|
|
}
|
|
w = get("GET", "", map[string]string{"Range": "bytes=100-199"})
|
|
if w.Code != 206 || w.Header().Get("Content-Range") != "bytes 100-199/10000" || w.Body.Len() != 100 || w.Body.Bytes()[0] != data[100] {
|
|
t.Fatalf("range: %d %q %d", w.Code, w.Header().Get("Content-Range"), w.Body.Len())
|
|
}
|
|
w = get("GET", "", map[string]string{"Range": "bytes=9990-"})
|
|
if w.Code != 206 || w.Body.Len() != 10 || w.Body.Bytes()[9] != data[9999] {
|
|
t.Fatalf("tail range: %d %d", w.Code, w.Body.Len())
|
|
}
|
|
w = get("HEAD", "", nil)
|
|
if w.Code != 200 || w.Body.Len() != 0 || w.Header().Get("Content-Length") != "10000" {
|
|
t.Fatalf("head: %d %d %q", w.Code, w.Body.Len(), w.Header().Get("Content-Length"))
|
|
}
|
|
if w := get("GET", "&max=50", nil); w.Code != 413 {
|
|
t.Fatalf("the editor's cap: %d", w.Code)
|
|
}
|
|
if w := get("GET", "", map[string]string{"Range": "bytes=20000-"}); w.Code != 416 {
|
|
t.Fatalf("a range past the end: %d", w.Code)
|
|
}
|
|
if err := os.Chmod(p, 0); err == nil && os.Geteuid() != 0 {
|
|
if w := get("GET", "", nil); w.Code != 403 {
|
|
t.Fatalf("unreadable: %d", w.Code)
|
|
}
|
|
}
|
|
}
|