MCPcopy
hub / github.com/perkeep/perkeep / ServeHTTP

Method ServeHTTP

pkg/server/image.go:339–428  ·  view source on GitHub ↗
(rw http.ResponseWriter, req *http.Request, file blob.Ref)

Source from the content-addressed store, hash-verified

337var singleResize singleflight.Group
338
339func (ih *ImageHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request, file blob.Ref) {
340 ctx := req.Context()
341 if !httputil.IsGet(req) {
342 http.Error(rw, "Invalid method", http.StatusBadRequest)
343 return
344 }
345 mw, mh := ih.MaxWidth, ih.MaxHeight
346 if mw == 0 || mh == 0 || mw > search.MaxImageSize || mh > search.MaxImageSize {
347 http.Error(rw, "bogus dimensions", http.StatusBadRequest)
348 return
349 }
350
351 key := cacheKey(file.String(), mw, mh)
352 etag := blob.RefFromString(key).String()[5:]
353 inm := req.Header.Get("If-None-Match")
354 if inm != "" {
355 if strings.Trim(inm, `"`) == etag {
356 thumbCacheHeader304.Add(1)
357 rw.WriteHeader(http.StatusNotModified)
358 return
359 }
360 } else {
361 if !disableThumbCache && req.Header.Get("If-Modified-Since") != "" {
362 thumbCacheHeader304.Add(1)
363 rw.WriteHeader(http.StatusNotModified)
364 return
365 }
366 }
367
368 var imageData []byte
369 format := ""
370 cacheHit := false
371 if ih.ThumbMeta != nil && !disableThumbCache {
372 var buf bytes.Buffer
373 format = ih.scaledCached(ctx, &buf, file)
374 if format != "" {
375 cacheHit = true
376 imageData = buf.Bytes()
377 }
378 }
379
380 if !cacheHit {
381 thumbCacheMiss.Add(1)
382 imi, err := singleResize.Do(key, func() (interface{}, error) {
383 return ih.scaleImage(ctx, file)
384 })
385 if err != nil {
386 http.Error(rw, err.Error(), 500)
387 return
388 }
389 im := imi.(*formatAndImage)
390 imageData = im.image
391 format = im.format
392 if ih.ThumbMeta != nil {
393 err := ih.cacheScaled(ctx, imageData, key)
394 if err != nil {
395 log.Printf("image resize: %v", err)
396 }

Callers 3

serveThumbnailMethod · 0.95
serveScaledImageMethod · 0.95
handleResourceMethod · 0.95

Calls 15

scaledCachedMethod · 0.95
scaleImageMethod · 0.95
cacheScaledMethod · 0.95
IsGetFunction · 0.92
RefFromStringFunction · 0.92
cacheKeyFunction · 0.85
imageContentTypeOfFormatFunction · 0.85
ContextMethod · 0.80
PrintfMethod · 0.80
ContainsMethod · 0.80
GetMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected