2021-04-26 17:52:50 +06:00
|
|
|
package fs
|
2019-02-04 20:04:19 +06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-04-26 17:52:50 +06:00
|
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v2/config"
|
2019-02-04 20:04:19 +06:00
|
|
|
)
|
|
|
|
|
2021-04-26 17:52:50 +06:00
|
|
|
type transport struct {
|
2019-02-04 20:04:19 +06:00
|
|
|
fs http.Dir
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:52:50 +06:00
|
|
|
func New() transport {
|
|
|
|
return transport{fs: http.Dir(config.LocalFileSystemRoot)}
|
2019-02-04 20:04:19 +06:00
|
|
|
}
|
|
|
|
|
2021-04-26 17:52:50 +06:00
|
|
|
func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
2019-02-04 20:04:19 +06:00
|
|
|
f, err := t.fs.Open(req.URL.Path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return nil, fmt.Errorf("%s is a directory", req.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.0",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
Header: make(http.Header),
|
|
|
|
ContentLength: fi.Size(),
|
|
|
|
Body: f,
|
|
|
|
Close: true,
|
|
|
|
Request: req,
|
|
|
|
}, nil
|
|
|
|
}
|