Merge pull request #9657 from azr/azr/ctt-hlp-read-seeker
content: add a BlobReadSeeker to allow multipart blob uploads
This commit is contained in:
		@@ -17,6 +17,7 @@
 | 
				
			|||||||
package content
 | 
					package content
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
@@ -52,6 +53,31 @@ func NewReader(ra ReaderAt) io.Reader {
 | 
				
			|||||||
	return io.NewSectionReader(ra, 0, ra.Size())
 | 
						return io.NewSectionReader(ra, 0, ra.Size())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type nopCloserBytesReader struct {
 | 
				
			||||||
 | 
						*bytes.Reader
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (*nopCloserBytesReader) Close() error { return nil }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type nopCloserSectionReader struct {
 | 
				
			||||||
 | 
						*io.SectionReader
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (*nopCloserSectionReader) Close() error { return nil }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// BlobReadSeeker returns a read seeker for the blob from the provider.
 | 
				
			||||||
 | 
					func BlobReadSeeker(ctx context.Context, provider Provider, desc ocispec.Descriptor) (io.ReadSeekCloser, error) {
 | 
				
			||||||
 | 
						if int64(len(desc.Data)) == desc.Size && digest.FromBytes(desc.Data) == desc.Digest {
 | 
				
			||||||
 | 
							return &nopCloserBytesReader{bytes.NewReader(desc.Data)}, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ra, err := provider.ReaderAt(ctx, desc)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return &nopCloserSectionReader{io.NewSectionReader(ra, 0, ra.Size())}, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ReadBlob retrieves the entire contents of the blob from the provider.
 | 
					// ReadBlob retrieves the entire contents of the blob from the provider.
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// Avoid using this for large blobs, such as layers.
 | 
					// Avoid using this for large blobs, such as layers.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user