1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/transport/gcs/gcs.go
Ewan Higgs 4944dfab30
Support Last-Modified response header and support If-Modified-Since request header. (#1147)
* Always return Last-Modified and support If-Modified-Since.

* IMGPROXY_USE_LAST_MODIFIED config setting.

IMGPROXY_USE_LAST_MODIFIED  (default false) when enabled will return the
Last-Modified time of the upstream image and also allow the support of
the If-Modified-Since request header (returning a 304 if the image
hasn't been modified).

If-Modified-Since allows If-None-Match to take precedence.

* Fixes based on DarthSim's feedback.

1. Don't worry about nil maps.
2. Fix a test now that we use the config.LastModifiedEnabled (and move
   it's location it he test file to a more sane place).
3. Update GCS transport code based on the refactoring of DarthSim.

In this iteration, we pull the Updated time from the GCS object attributes
and format them as a string. We then parse it in the notmodified module.
Seems a bit silly to do it this way. If we agree on the approach here,
then AWS and Azure can follow.

* Support azure, fs, s3, and swift.

* Grab the headers for If-Modified-Since and Last-Modified before parsing them.

* Add tests for last-modified for fs.

* Support Last-Modified being passed when streaming an upstream file.

* Tests for Last-Modified for GCS and Azure

* Support s3 and swift tests. Sadly fakes3 doesn't support Last-Modified

* Test against forked gofakes3
2023-05-03 21:21:46 +06:00

187 lines
4.3 KiB
Go

package gcs
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"google.golang.org/api/option"
raw "google.golang.org/api/storage/v1"
htransport "google.golang.org/api/transport/http"
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/httprange"
defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
"github.com/imgproxy/imgproxy/v3/transport/notmodified"
)
// For tests
var noAuth bool = false
type transport struct {
client *storage.Client
}
func buildHTTPClient(opts ...option.ClientOption) (*http.Client, error) {
trans, err := defaultTransport.New(false)
if err != nil {
return nil, err
}
htrans, err := htransport.NewTransport(context.Background(), trans, opts...)
if err != nil {
return nil, errors.Wrap(err, "error creating GCS transport")
}
return &http.Client{Transport: htrans}, nil
}
func New() (http.RoundTripper, error) {
var client *storage.Client
opts := []option.ClientOption{
option.WithScopes(raw.DevstorageReadOnlyScope),
option.WithUserAgent(config.UserAgent),
}
if len(config.GCSKey) > 0 {
opts = append(opts, option.WithCredentialsJSON([]byte(config.GCSKey)))
}
if len(config.GCSEndpoint) > 0 {
opts = append(opts, option.WithEndpoint(config.GCSEndpoint))
}
if noAuth {
opts = append(opts, option.WithoutAuthentication())
}
httpClient, err := buildHTTPClient(opts...)
if err != nil {
return nil, err
}
opts = append(opts, option.WithHTTPClient(httpClient))
client, err = storage.NewClient(context.Background(), opts...)
if err != nil {
return nil, fmt.Errorf("Can't create GCS client: %s", err)
}
return transport{client}, nil
}
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
bkt := t.client.Bucket(req.URL.Host)
obj := bkt.Object(strings.TrimPrefix(req.URL.Path, "/"))
if g, err := strconv.ParseInt(req.URL.RawQuery, 10, 64); err == nil && g > 0 {
obj = obj.Generation(g)
}
var (
reader *storage.Reader
statusCode int
size int64
)
header := make(http.Header)
if r := req.Header.Get("Range"); len(r) != 0 {
start, end, err := httprange.Parse(r)
if err != nil {
return httprange.InvalidHTTPRangeResponse(req), nil
}
if end != 0 {
length := end - start + 1
if end < 0 {
length = -1
}
reader, err = obj.NewRangeReader(req.Context(), start, length)
if err != nil {
return nil, err
}
if end < 0 || end >= reader.Attrs.Size {
end = reader.Attrs.Size - 1
}
size = end - reader.Attrs.StartOffset + 1
statusCode = http.StatusPartialContent
header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", reader.Attrs.StartOffset, end, reader.Attrs.Size))
}
}
// We haven't initialize reader yet, this means that we need non-ranged reader
if reader == nil {
if config.ETagEnabled || config.LastModifiedEnabled {
attrs, err := obj.Attrs(req.Context())
if err != nil {
return handleError(req, err)
}
if config.ETagEnabled {
header.Set("ETag", attrs.Etag)
}
if config.LastModifiedEnabled {
header.Set("Last-Modified", attrs.Updated.Format(http.TimeFormat))
}
}
if resp := notmodified.Response(req, header); resp != nil {
return resp, nil
}
var err error
reader, err = obj.NewReader(req.Context())
if err != nil {
return handleError(req, err)
}
statusCode = 200
size = reader.Attrs.Size
}
header.Set("Accept-Ranges", "bytes")
header.Set("Content-Length", strconv.Itoa(int(size)))
header.Set("Content-Type", reader.Attrs.ContentType)
header.Set("Cache-Control", reader.Attrs.CacheControl)
return &http.Response{
StatusCode: statusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
ContentLength: reader.Attrs.Size,
Body: reader,
Close: true,
Request: req,
}, 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
}