From e3247edafb38b31cf0ed17378c0a6c447da6113a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 25 Dec 2012 10:27:35 -0800 Subject: [PATCH] Change blobref.SeekerFromStreamingFetcher signature to not return an error. Change-Id: I77f693e3b3d0d116e08bca3d3f4cb45ef2a00b27 --- cmd/camget/camget.go | 4 ++-- pkg/blobref/fetcher.go | 17 ++++++----------- pkg/index/receive.go | 8 ++------ pkg/server/download.go | 10 ++-------- pkg/server/filetree.go | 11 ++--------- pkg/server/image.go | 15 +++------------ 6 files changed, 17 insertions(+), 48 deletions(-) diff --git a/cmd/camget/camget.go b/cmd/camget/camget.go index 15858ddb7..0aca02e5f 100644 --- a/cmd/camget/camget.go +++ b/cmd/camget/camget.go @@ -106,7 +106,7 @@ func main() { var rc io.ReadCloser var err error if *flagContents { - seekFetcher, _ := blobref.SeekerFromStreamingFetcher(cl) + seekFetcher := blobref.SeekerFromStreamingFetcher(cl) rc, err = schema.NewFileReader(seekFetcher, br) } else { rc, err = fetch(cl, br) @@ -213,7 +213,7 @@ func smartFetch(cl *client.Client, targ string, br *blobref.BlobRef) error { return fmt.Errorf("file type: %v", err) } defer f.Close() - seekFetcher, _ := blobref.SeekerFromStreamingFetcher(cl) + seekFetcher := blobref.SeekerFromStreamingFetcher(cl) fr, err := schema.NewFileReader(seekFetcher, br) if err != nil { return fmt.Errorf("NewFileReader: %v", err) diff --git a/pkg/blobref/fetcher.go b/pkg/blobref/fetcher.go index 1a838e4fa..adc733fb4 100644 --- a/pkg/blobref/fetcher.go +++ b/pkg/blobref/fetcher.go @@ -187,19 +187,14 @@ func (s *MemoryStore) FetchStreaming(b *BlobRef) (file io.ReadCloser, size int64 // SeekerFromStreamingFetcher returns the most efficient implementation of a seeking fetcher // from a provided streaming fetcher. -func SeekerFromStreamingFetcher(f StreamingFetcher) (SeekFetcher, error) { - // TODO(bradfitz): this never returns errors now, so update signature and fix callers. - seeker, ok := f.(SeekFetcher) - if ok { - return seeker, nil +func SeekerFromStreamingFetcher(f StreamingFetcher) SeekFetcher { + if sk, ok := f.(SeekFetcher); ok { + return sk } - tester, ok := f.(SeekTester) - if ok { - if tester.IsFetcherASeeker() { - return &fetcherToSeekerWrapper{f}, nil - } + if tester, ok := f.(SeekTester); ok && tester.IsFetcherASeeker() { + return &fetcherToSeekerWrapper{f} } - return bufferingSeekFetcherWrapper{f}, nil + return bufferingSeekFetcherWrapper{f} } // bufferingSeekFetcherWrapper is a SeekFetcher that implements diff --git a/pkg/index/receive.go b/pkg/index/receive.go index 0a69f6f8c..64f3a9766 100644 --- a/pkg/index/receive.go +++ b/pkg/index/receive.go @@ -110,12 +110,7 @@ func (ix *Index) populateMutation(br *blobref.BlobRef, sniffer *BlobSniffer, bm // ss: the parsed file schema blob // bm: keys to populate func (ix *Index) populateFile(blobRef *blobref.BlobRef, ss *schema.Superset, bm BatchMutation) error { - seekFetcher, err := blobref.SeekerFromStreamingFetcher(ix.BlobSource) - if err != nil { - return err - } - - sha1 := sha1.New() + seekFetcher := blobref.SeekerFromStreamingFetcher(ix.BlobSource) fr, err := ss.NewFileReader(seekFetcher) if err != nil { // TODO(bradfitz): propagate up a transient failure @@ -127,6 +122,7 @@ func (ix *Index) populateFile(blobRef *blobref.BlobRef, ss *schema.Superset, bm } mime, reader := magic.MimeTypeFromReader(fr) + sha1 := sha1.New() var copyDest io.Writer = sha1 var withCopyErr func(error) // or nil if strings.HasPrefix(mime, "image/") { diff --git a/pkg/server/download.go b/pkg/server/download.go index 8e8071254..a5f43a77c 100644 --- a/pkg/server/download.go +++ b/pkg/server/download.go @@ -34,7 +34,7 @@ type DownloadHandler struct { ForceMime string // optional } -func (dh *DownloadHandler) storageSeekFetcher() (blobref.SeekFetcher, error) { +func (dh *DownloadHandler) storageSeekFetcher() blobref.SeekFetcher { return blobref.SeekerFromStreamingFetcher(dh.Fetcher) // TODO: pass dh.Cache? } @@ -44,13 +44,7 @@ func (dh *DownloadHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request, return } - fetchSeeker, err := dh.storageSeekFetcher() - if err != nil { - http.Error(rw, err.Error(), 500) - return - } - - fr, err := schema.NewFileReader(fetchSeeker, file) + fr, err := schema.NewFileReader(dh.storageSeekFetcher(), file) if err != nil { http.Error(rw, "Can't serve file: "+err.Error(), 500) return diff --git a/pkg/server/filetree.go b/pkg/server/filetree.go index d190ae070..bb178e4a2 100644 --- a/pkg/server/filetree.go +++ b/pkg/server/filetree.go @@ -30,7 +30,7 @@ type FileTreeHandler struct { file *blobref.BlobRef } -func (fth *FileTreeHandler) storageSeekFetcher() (blobref.SeekFetcher, error) { +func (fth *FileTreeHandler) storageSeekFetcher() blobref.SeekFetcher { return blobref.SeekerFromStreamingFetcher(fth.Fetcher) // TODO: pass ih.Cache? } @@ -42,14 +42,7 @@ func (fth *FileTreeHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) ret := make(map[string]interface{}) defer httputil.ReturnJSON(rw, ret) - fetchSeeker, err := fth.storageSeekFetcher() - if err != nil { - http.Error(rw, "No storageSeekFetcher", 500) - log.Printf("getting fetcher: %v\n", err) - return - } - - de, err := schema.NewDirectoryEntryFromBlobRef(fetchSeeker, fth.file) + de, err := schema.NewDirectoryEntryFromBlobRef(fth.storageSeekFetcher(), fth.file) dir, err := de.Directory() if err != nil { http.Error(rw, "Error reading directory", 500) diff --git a/pkg/server/image.go b/pkg/server/image.go index abd4ca0a7..9c5c2db92 100644 --- a/pkg/server/image.go +++ b/pkg/server/image.go @@ -46,7 +46,7 @@ type ImageHandler struct { sc ScaledImage // optional cache for scaled images } -func (ih *ImageHandler) storageSeekFetcher() (blobref.SeekFetcher, error) { +func (ih *ImageHandler) storageSeekFetcher() blobref.SeekFetcher { return blobref.SeekerFromStreamingFetcher(ih.Fetcher) // TODO: pass ih.Cache? } @@ -97,11 +97,7 @@ func (ih *ImageHandler) cacheScaled(tr io.Reader, name string) error { } func (ih *ImageHandler) cached(br *blobref.BlobRef) (fr *schema.FileReader, err error) { - fetchSeeker, err := blobref.SeekerFromStreamingFetcher(ih.Cache) - if err != nil { - return nil, err - } - + fetchSeeker := blobref.SeekerFromStreamingFetcher(ih.Cache) fr, err = schema.NewFileReader(fetchSeeker, br) if err != nil { return nil, err @@ -151,12 +147,7 @@ func (ih *ImageHandler) scaledCached(buf *bytes.Buffer, file *blobref.BlobRef) ( func (ih *ImageHandler) scaleImage(buf *bytes.Buffer, file *blobref.BlobRef) (format string, err error) { mw, mh := ih.MaxWidth, ih.MaxHeight - fetchSeeker, err := ih.storageSeekFetcher() - if err != nil { - return format, err - } - - fr, err := schema.NewFileReader(fetchSeeker, file) + fr, err := schema.NewFileReader(ih.storageSeekFetcher(), file) if err != nil { return format, err }