oficina / slides Public

Slides: a clip that cannot be rewound keeps its sniffed head

sniffedVideo handed back a seekable store reader as it was, after
reading its first kilobyte to sniff it, and left the rewind to the
clip cache. When that Seek failed, the cache copied from where the
sniff stopped: a registered clip failed its hash check and a staged
one came back short. sniffedVideo now rewinds it itself, and when the
rewind fails answers the head in front of the rest.

Test: TestASniffedClipKeepsItsHeadWhenItCannotRewind, with a
ReadSeeker whose Seek errors, read directly and through the cache.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2c4c2b7eb3fcdc4b4355b0b693fb98b0d00fd188 parent 79b5d64
2 files changed, +44 −2
  • internal/slides/clipcache_test.go +39 −0
  • internal/slides/clips.go +5 −2
diff --git a/internal/slides/clipcache_test.go b/internal/slides/clipcache_test.go
index 27400ba..c0672b0 100644
--- a/internal/slides/clipcache_test.go
+++ b/internal/slides/clipcache_test.go
@@ -132,3 +132,42 @@ func TestTheClipCacheEvictsWithoutBreakingWhatIsBeingServed(t *testing.T) {
}
newClipCache("", 100).close()
}
+
+// noSeek is a store reader that says it seeks and cannot: every Seek
+// fails.
+type noSeek struct{ *bytes.Reader }
+
+func (noSeek) Seek(int64, int) (int64, error) { return 0, errors.New("no seeking here") }
+func (noSeek) Close() error { return nil }
+
+// TestASniffedClipKeepsItsHeadWhenItCannotRewind: the head read to
+// sniff a clip is part of its bytes. When the reader cannot be rewound
+// it is put back in front of the rest, so a registered clip still
+// matches its ref and a staged one comes back whole.
+func TestASniffedClipKeepsItsHeadWhenItCannotRewind(t *testing.T) {
+ v := append([]byte{0, 0, 0, 0x18, 'f', 't', 'y', 'p', 'i', 's', 'o', 'm', 0, 0, 0, 0, 'i', 's', 'o', 'm', 'm', 'p', '4', '1'},
+ bytes.Repeat([]byte("v"), 5000)...)
+ load := func() (string, io.Reader, func(), error) { return sniffedVideo(noSeek{bytes.NewReader(v)}) }
+ kind, r, done, err := load()
+ if err != nil || kind != "video/mp4" {
+ t.Fatalf("sniffed %q, %v; want video/mp4", kind, err)
+ }
+ got, _ := io.ReadAll(r)
+ done()
+ if !bytes.Equal(got, v) {
+ t.Errorf("read %d bytes back, want the whole %d", len(got), len(v))
+ }
+ c := newClipCache("", 1<<20)
+ defer c.close()
+ for _, key := range []string{refOf(v), "staged-abc"} {
+ body, _, release, err := c.get(key, key == refOf(v), load)
+ if err != nil {
+ t.Fatalf("get %s: %v", key[:6], err)
+ }
+ got, _ := io.ReadAll(body)
+ release()
+ if !bytes.Equal(got, v) {
+ t.Errorf("%s: the cache holds %d bytes, want the whole %d", key[:6], len(got), len(v))
+ }
+ }
+}
diff --git a/internal/slides/clips.go b/internal/slides/clips.go
index aca57be..650b6d4 100644
--- a/internal/slides/clips.go
+++ b/internal/slides/clips.go
@@ -313,7 +313,8 @@ func videoLoad(ctx context.Context, store blobs.Store, object string) func() (st
}
// sniffedVideo reads rc's head, and answers its kind and rc again,
-// whole: rc itself when it seeks, else the head and the rest.
+// whole: rc itself, rewound, when it seeks, else (it cannot, or the
+// rewind fails) the head and the rest.
func sniffedVideo(rc io.ReadCloser) (string, io.Reader, func(), error) {
head := make([]byte, 1024)
n, err := io.ReadFull(rc, head)
@@ -328,7 +329,9 @@ func sniffedVideo(rc io.ReadCloser) (string, io.Reader, func(), error) {
}
release := func() { rc.Close() }
if rs, ok := rc.(io.ReadSeeker); ok {
- return kind, rs, release, nil
+ if _, err := rs.Seek(0, io.SeekStart); err == nil {
+ return kind, rs, release, nil
+ }
}
return kind, io.MultiReader(bytes.NewReader(head[:n]), rc), release, nil
}