1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-06-17 22:37:33 +02:00

Implement fallback images in a similar fashion to watermarks. (#374)

* 220. Implement fallback images in a similar fashion to watermarks.

* 220. Fix error formatting.

* 220. Fixes based on @darthsim's feedback.
This commit is contained in:
Ewan Higgs
2020-04-08 16:32:45 +02:00
committed by GitHub
parent cf6f481d83
commit 21b990d895
3 changed files with 53 additions and 11 deletions

View File

@ -16,6 +16,7 @@ var (
processingSem chan struct{}
headerVaryValue string
fallback *imageData
)
func initProcessingHandler() error {
@ -45,6 +46,10 @@ func initProcessingHandler() error {
headerVaryValue = strings.Join(vary, ", ")
if err := loadFallback(); err != nil {
return err
}
return nil
}
@ -153,7 +158,12 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
if prometheusEnabled {
incrementPrometheusErrorsTotal("download")
}
panic(err)
if fallback != nil {
logError("Could not load image. Using fallback image: %s", err.Error())
ctx = context.WithValue(ctx, imageDataCtxKey, fallback)
} else {
panic(err)
}
}
checkTimeout(ctx)
@ -186,3 +196,11 @@ func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
respondWithImage(ctx, reqID, r, rw, imageData)
}
func loadFallback() (err error) {
fallback, err = getFallbackImageData()
if err != nil {
logError("Could not load fallback data. Fallback images will not be available: %s", err.Error())
}
return err
}