Change blobref.SeekerFromStreamingFetcher signature to not return an error.

Change-Id: I77f693e3b3d0d116e08bca3d3f4cb45ef2a00b27
This commit is contained in:
Brad Fitzpatrick 2012-12-25 10:27:35 -08:00
parent 826bb388b4
commit e3247edafb
6 changed files with 17 additions and 48 deletions

View File

@ -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)

View File

@ -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

View File

@ -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/") {

View File

@ -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

View File

@ -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)

View File

@ -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
}