Stream object responses with known size bounds
This commit is contained in:
78
internal/adapters/storage/stream.go
Normal file
78
internal/adapters/storage/stream.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// StreamObject copies one response into a caller-owned destination and closes
|
||||
// its body. A non-nil expected size is authoritative; otherwise the response's
|
||||
// known size bounds the transfer. Unknown response sizes stream to EOF.
|
||||
func StreamObject(ctx context.Context, store ObjectStore, key string, destination io.Writer, expected *int64) (info ObjectInfo, err error) {
|
||||
label := func(size *int64) string {
|
||||
if size != nil {
|
||||
return fmt.Sprintf("download object %q (expected %d bytes)", key, *size)
|
||||
}
|
||||
return fmt.Sprintf("download object %q", key)
|
||||
}
|
||||
if store == nil || destination == nil {
|
||||
return info, fmt.Errorf("download object %q: store and destination are required", key)
|
||||
}
|
||||
if expected != nil && *expected < 0 {
|
||||
return info, fmt.Errorf("download object %q: invalid expected size %d", key, *expected)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return info, fmt.Errorf("%s: %w", label(expected), err)
|
||||
}
|
||||
info, body, err := store.Read(ctx, key)
|
||||
if err != nil {
|
||||
return info, fmt.Errorf("%s: %w", label(expected), err)
|
||||
}
|
||||
if body == nil {
|
||||
return info, fmt.Errorf("%s: response body is missing", label(expected))
|
||||
}
|
||||
sizeKnown := info.SizeKnown || info.Size > 0
|
||||
size := expected
|
||||
if size == nil && sizeKnown {
|
||||
size = &info.Size
|
||||
}
|
||||
defer func() {
|
||||
if closeErr := body.Close(); closeErr != nil {
|
||||
err = errors.Join(err, fmt.Errorf("%s: close response: %w", label(size), closeErr))
|
||||
}
|
||||
}()
|
||||
if expected != nil && sizeKnown && info.Size != *expected {
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes, response reports %d", key, *expected, info.Size)
|
||||
}
|
||||
source := contextReader{ctx: ctx, reader: body}
|
||||
if size == nil {
|
||||
if _, err := io.Copy(destination, source); err != nil {
|
||||
return info, fmt.Errorf("download object %q: copy body: %w", key, err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return info, fmt.Errorf("download object %q: %w", key, err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
count, copyErr := io.CopyN(destination, source, *size)
|
||||
if copyErr != nil {
|
||||
if errors.Is(copyErr, io.EOF) {
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes, received %d: %w", key, *size, count, io.ErrUnexpectedEOF)
|
||||
}
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes, copied %d: %w", key, *size, count, copyErr)
|
||||
}
|
||||
var probe [1]byte
|
||||
n, probeErr := io.ReadFull(source, probe[:])
|
||||
if n > 0 {
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes, response is oversized", key, *size)
|
||||
}
|
||||
if probeErr != nil && !errors.Is(probeErr, io.EOF) {
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes, probe response: %w", key, *size, probeErr)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return info, fmt.Errorf("download object %q: expected %d bytes: %w", key, *size, err)
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
Reference in New Issue
Block a user