ScaledCached reads the scaled version of the image in file, if it is in cache and writes it to buf. On successful read and population of buf, the returned format is non-empty. Almost all errors are not interesting. Real errors will be logged.
(ctx context.Context, buf *bytes.Buffer, file blob.Ref)
| 178 | // On successful read and population of buf, the returned format is non-empty. |
| 179 | // Almost all errors are not interesting. Real errors will be logged. |
| 180 | func (ih *ImageHandler) scaledCached(ctx context.Context, buf *bytes.Buffer, file blob.Ref) (format string) { |
| 181 | key := cacheKey(file.String(), ih.MaxWidth, ih.MaxHeight) |
| 182 | br, err := ih.ThumbMeta.Get(key) |
| 183 | if err == errCacheMiss { |
| 184 | return |
| 185 | } |
| 186 | if err != nil { |
| 187 | log.Printf("Warning: thumbnail cachekey(%q)->meta lookup error: %v", key, err) |
| 188 | return |
| 189 | } |
| 190 | fr, err := ih.cached(ctx, br) |
| 191 | if err != nil { |
| 192 | if imageDebug { |
| 193 | log.Printf("Could not get cached image %v: %v\n", br, err) |
| 194 | } |
| 195 | return |
| 196 | } |
| 197 | defer fr.Close() |
| 198 | _, err = io.Copy(buf, fr) |
| 199 | if err != nil { |
| 200 | return |
| 201 | } |
| 202 | mime := magic.MIMEType(buf.Bytes()) |
| 203 | if format = strings.TrimPrefix(mime, "image/"); format == mime { |
| 204 | log.Printf("Warning: unescaped MIME type %q of %v file for thumbnail %q", mime, br, key) |
| 205 | return |
| 206 | } |
| 207 | return format |
| 208 | } |
| 209 | |
| 210 | // Gate the number of concurrent image resizes to limit RAM & CPU use. |
| 211 |