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 := context.Cause(ctx); err != nil { return info, fmt.Errorf("%s: %w", label(expected), err) } info, body, readErr := store.Read(ctx, key) sizeKnown := info.SizeKnown || info.Size > 0 size := expected if size == nil && sizeKnown { size = &info.Size } if body != nil { defer func() { if closeErr := body.Close(); closeErr != nil { err = errors.Join(err, fmt.Errorf("%s: close response: %w", label(size), closeErr)) } }() } if readErr != nil { return info, fmt.Errorf("%s: %w", label(size), readErr) } if body == nil { return info, fmt.Errorf("%s: response body is missing", label(size)) } 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} tracked := &transferReader{reader: source} if size == nil { _, copyErr := io.Copy(destination, tracked) if tracked.err != nil && !errors.Is(tracked.err, io.EOF) { copyErr = errors.Join(copyErr, tracked.err) } if err := errors.Join(copyErr, context.Cause(ctx)); err != nil { return info, fmt.Errorf("download object %q: copy body: %w", key, err) } return info, nil } count, copyErr := io.CopyN(destination, tracked, *size) if tracked.err != nil && !errors.Is(tracked.err, io.EOF) { copyErr = errors.Join(copyErr, tracked.err) } copyErr = errors.Join(copyErr, context.Cause(ctx)) if copyErr != nil { if errors.Is(copyErr, io.EOF) && context.Cause(ctx) == nil { 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 var n int var probeErr error for n == 0 && probeErr == nil { n, probeErr = source.Read(probe[:]) } if n > 0 { oversized := fmt.Errorf("download object %q: expected %d bytes, response is oversized", key, *size) if probeErr != nil && !errors.Is(probeErr, io.EOF) { return info, errors.Join(oversized, probeErr, context.Cause(ctx)) } return info, errors.Join(oversized, context.Cause(ctx)) } if probeErr != nil && !errors.Is(probeErr, io.EOF) { return info, fmt.Errorf("download object %q: expected %d bytes, probe response: %w", key, *size, errors.Join(probeErr, context.Cause(ctx))) } if err := context.Cause(ctx); err != nil { return info, fmt.Errorf("download object %q: expected %d bytes: %w", key, *size, err) } return info, nil } type transferReader struct { reader io.Reader err error } func (r *transferReader) Read(p []byte) (int, error) { n, err := r.reader.Read(p) if err != nil { r.err = err } return n, err }