1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00

GCS transport should return 404 if bucket/object is not found

This commit is contained in:
DarthSim 2022-07-27 17:06:25 +06:00
parent badf8303a2
commit 6cc887dfc4
2 changed files with 22 additions and 2 deletions

View File

@ -21,6 +21,7 @@
### Fix ### Fix
- Fix trimming of CMYK images. - Fix trimming of CMYK images.
- Respond with 404 when the source image can not be found in OpenStack Object Storage. - Respond with 404 when the source image can not be found in OpenStack Object Storage.
- Respond with 404 when file wasn't found in the GCS storage.
## [3.6.0] - 2022-06-13 ## [3.6.0] - 2022-06-13
### Add ### Add

View File

@ -3,6 +3,7 @@ package gcs
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -62,7 +63,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
if config.ETagEnabled { if config.ETagEnabled {
attrs, err := obj.Attrs(req.Context()) attrs, err := obj.Attrs(req.Context())
if err != nil { if err != nil {
return nil, err return handleError(req, err)
} }
header.Set("ETag", attrs.Etag) header.Set("ETag", attrs.Etag)
@ -83,7 +84,7 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
reader, err := obj.NewReader(req.Context()) reader, err := obj.NewReader(req.Context())
if err != nil { if err != nil {
return nil, err return handleError(req, err)
} }
header.Set("Cache-Control", reader.Attrs.CacheControl) header.Set("Cache-Control", reader.Attrs.CacheControl)
@ -101,3 +102,21 @@ func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
Request: req, Request: req,
}, nil }, nil
} }
func handleError(req *http.Request, err error) (*http.Response, error) {
if err != storage.ErrBucketNotExist && err != storage.ErrObjectNotExist {
return nil, err
}
return &http.Response{
StatusCode: http.StatusNotFound,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: make(http.Header),
ContentLength: int64(len(err.Error())),
Body: io.NopCloser(strings.NewReader(err.Error())),
Close: false,
Request: req,
}, nil
}