mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-06 18:03:29 +02:00
Respond with 404 if local file wasn't found
This commit is contained in:
@@ -4,8 +4,11 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/imgproxy/imgproxy/v3/config"
|
"github.com/imgproxy/imgproxy/v3/config"
|
||||||
)
|
)
|
||||||
@@ -19,9 +22,25 @@ func New() transport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||||
f, err := t.fs.Open(req.URL.Path)
|
header := make(http.Header)
|
||||||
|
|
||||||
|
f, err := t.fs.Open(req.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
Proto: "HTTP/1.0",
|
||||||
|
ProtoMajor: 1,
|
||||||
|
ProtoMinor: 0,
|
||||||
|
Header: header,
|
||||||
|
ContentLength: 0,
|
||||||
|
Body: io.NopCloser(strings.NewReader(
|
||||||
|
fmt.Sprintf("%s doesn't exist", req.URL.Path),
|
||||||
|
)),
|
||||||
|
Close: false,
|
||||||
|
Request: req,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,11 +50,21 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
return nil, fmt.Errorf("%s is a directory", req.URL.Path)
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
Proto: "HTTP/1.0",
|
||||||
|
ProtoMajor: 1,
|
||||||
|
ProtoMinor: 0,
|
||||||
|
Header: header,
|
||||||
|
ContentLength: 0,
|
||||||
|
Body: io.NopCloser(strings.NewReader(
|
||||||
|
fmt.Sprintf("%s is directory", req.URL.Path),
|
||||||
|
)),
|
||||||
|
Close: false,
|
||||||
|
Request: req,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
header := make(http.Header)
|
|
||||||
|
|
||||||
if config.ETagEnabled {
|
if config.ETagEnabled {
|
||||||
etag := BuildEtag(req.URL.Path, fi)
|
etag := BuildEtag(req.URL.Path, fi)
|
||||||
header.Set("ETag", etag)
|
header.Set("ETag", etag)
|
||||||
|
Reference in New Issue
Block a user