Add range support

Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
Bryan Frimin
2026-07-02 17:56:03 +02:00
committed by Sacha Al Himdani
parent 8bbbca0d6d
commit 7495d1d5a0
4 changed files with 141 additions and 12 deletions

View File

@@ -31,17 +31,21 @@ import (
)
type FileObject struct {
Body io.ReadCloser
ContentType string
ContentLength int64
ETag string
LastModified time.Time
NotModified bool
Body io.ReadCloser
ContentType string
ContentLength int64
ContentRange string
ETag string
LastModified time.Time
NotModified bool
PartialContent bool
RangeNotSatisfiable bool
}
type FileConditions struct {
IfNoneMatch string
IfModifiedSince time.Time
Range string
}
func (s *Service) GetFileBase64(
@@ -111,11 +115,21 @@ func (s *Service) OpenFile(
input.IfModifiedSince = &conds.IfModifiedSince
}
if conds.Range != "" {
input.Range = &conds.Range
}
result, err := s.s3Client.GetObject(ctx, input)
if err != nil {
if respErr, ok := errors.AsType[*smithyhttp.ResponseError](err); ok {
if respErr.HTTPStatusCode() == http.StatusNotModified {
switch respErr.HTTPStatusCode() {
case http.StatusNotModified:
return &FileObject{NotModified: true}, nil
case http.StatusRequestedRangeNotSatisfiable:
return &FileObject{
RangeNotSatisfiable: true,
ContentLength: file.FileSize,
}, nil
}
}
@@ -136,6 +150,19 @@ func (s *Service) OpenFile(
obj.LastModified = *result.LastModified
}
// A Range request that S3 honors comes back as 206 Partial Content with a
// Content-Range header and a ContentLength scoped to the returned slice. An
// If-Range mismatch (or no Range) yields a normal 200 with the full object,
// so we only override the length/status when Content-Range is present.
if result.ContentRange != nil {
obj.ContentRange = *result.ContentRange
obj.PartialContent = true
}
if result.ContentLength != nil {
obj.ContentLength = *result.ContentLength
}
return obj, nil
}

View File

@@ -91,6 +91,87 @@ func TestOpenFile_StreamsBody(t *testing.T) {
assert.Equal(t, content, string(body))
}
func TestOpenFile_RangeRequestReturnsPartialContent(t *testing.T) {
t.Parallel()
const (
etag = `"abc123"`
content = "hello world"
)
svc := newTestS3Service(
t,
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "bytes=0-4", r.Header.Get("Range"))
w.Header().Set("ETag", etag)
w.Header().Set("Content-Range", "bytes 0-4/11")
w.Header().Set("Content-Length", "5")
w.WriteHeader(http.StatusPartialContent)
_, _ = io.WriteString(w, content[:5])
},
)
file := &coredata.File{
BucketName: "uploads",
FileKey: "tenant/file",
MimeType: "text/plain",
FileSize: int64(len(content)),
}
obj, err := svc.OpenFile(
context.Background(),
file,
filemanager.FileConditions{Range: "bytes=0-4"},
)
require.NoError(t, err)
require.NotNil(t, obj)
defer func() { _ = obj.Body.Close() }()
assert.True(t, obj.PartialContent)
assert.False(t, obj.NotModified)
assert.Equal(t, "bytes 0-4/11", obj.ContentRange)
assert.Equal(t, int64(5), obj.ContentLength)
body, err := io.ReadAll(obj.Body)
require.NoError(t, err)
assert.Equal(t, content[:5], string(body))
}
func TestOpenFile_RangeNotSatisfiable(t *testing.T) {
t.Parallel()
const content = "hello world"
svc := newTestS3Service(
t,
func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Range", "bytes */11")
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
},
)
file := &coredata.File{
BucketName: "uploads",
FileKey: "tenant/file",
MimeType: "text/plain",
FileSize: int64(len(content)),
}
obj, err := svc.OpenFile(
context.Background(),
file,
filemanager.FileConditions{Range: "bytes=999-1000"},
)
require.NoError(t, err)
require.NotNil(t, obj)
assert.True(t, obj.RangeNotSatisfiable)
assert.False(t, obj.PartialContent)
assert.Nil(t, obj.Body)
assert.Equal(t, int64(len(content)), obj.ContentLength)
}
func TestOpenFile_NotModifiedByETag(t *testing.T) {
t.Parallel()