func ImageData(storage store.ImageStore, gc groupcache.Context) ([]byte, error) { c, ok := gc.(*CacheContext) if !ok { return nil, errors.New("invalid context") } var reader io.ReadCloser var err error defer func() { if reader != nil { reader.Close() } }() resp, err := storage.Head(c.Bucket, c.ImageId) if err != nil { // Don't break on an error log.Println(err) } reader, err = c.ReadModified(storage) if err != nil { reader, err = c.ReadOriginal(storage) if err != nil { return nil, err } } else { log.Println("Retrieved resized image from S3") return readImage(reader) } var buf io.Reader if c.Width != 0 { if resp != nil && resp.Header.Get("Content-Type") == "image/gif" { buf, err = ResizeGif(reader, c) } else { buf, err = Resize(reader, c) } if err != nil { return nil, err } } result, err := readImage(buf) if err != nil { return nil, err } go func() { err = c.WriteModified(result, storage) if err != nil { log.Printf("s3 upload: %s", err.Error()) } }() log.Println("Retrieved original and stored resized image in S3") return result, err }
func (c *CacheContext) ReadModified(s store.ImageStore) (io.ReadCloser, error) { data, err := s.GetReader(c.Bucket, c.CacheKey()) if err != nil { log.Printf("s3 download: %s", err.Error()) return nil, err } return data, err }
func (c *CacheContext) ReadOriginal(s store.ImageStore) (io.ReadCloser, error) { r, err := s.GetReader(c.Bucket, c.ImageId) if err != nil { log.Printf("s3 download: %s", err.Error()) return nil, err } return r, err }
func (c *CacheContext) WriteModified(buf []byte, s store.ImageStore) error { return s.Put(c.Bucket, c.CacheKey(), buf, http.DetectContentType(buf)) }