Files: a seek costs nothing; the backend reopens only when a read wants somewhere else

ServeContent seeks to the end for the size before it seeks to the range, which was
throwing away the stream Check had just opened.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Mitchell-Johnson
2026-10-11 16:41:54 -07:00
co-authored by Claude Fable 5.1
parent a9bcb88212
commit d7f56ee0b3
+6 -6
View File
@@ -26,7 +26,7 @@ func NewSeeker(ctx context.Context, fsys FS, path string, size int64) *Seeker {
}
// Check opens the file now, from the start, so a file that cannot be read is known before
// any headers go out. A later seek elsewhere simply reopens.
// any headers go out. Seeking costs nothing; a Read from somewhere else reopens there.
func (s *Seeker) Check() error {
if s.cur != nil {
return nil
@@ -54,11 +54,7 @@ func (s *Seeker) Seek(offset int64, whence int) (int64, error) {
if pos < 0 {
return 0, errors.New("seek before start")
}
if s.cur != nil && pos != s.at {
s.cur.Close()
s.cur = nil
}
s.pos = pos
s.pos = pos // the stream, if open, stays where it is until a Read wants somewhere else
return pos, nil
}
@@ -66,6 +62,10 @@ func (s *Seeker) Read(p []byte) (int, error) {
if s.pos >= s.size {
return 0, io.EOF
}
if s.cur != nil && s.pos != s.at {
s.cur.Close()
s.cur = nil
}
if s.cur == nil {
f, err := s.fsys.OpenAt(s.ctx, s.path, s.pos)
if err != nil {