2022-04-06 13:00:19 +02:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-24 12:50:08 +02:00
|
|
|
"errors"
|
2022-04-06 13:00:19 +02:00
|
|
|
"fmt"
|
2022-06-24 12:50:08 +02:00
|
|
|
"io"
|
2022-04-06 13:00:19 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/swift/v2"
|
2022-04-06 13:35:44 +02:00
|
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
2023-04-13 22:05:25 +02:00
|
|
|
defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
|
2023-04-10 19:01:44 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/transport/notmodified"
|
2022-04-06 13:00:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type transport struct {
|
|
|
|
con *swift.Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (http.RoundTripper, error) {
|
2023-04-13 22:05:25 +02:00
|
|
|
trans, err := defaultTransport.New(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:00:19 +02:00
|
|
|
c := &swift.Connection{
|
|
|
|
UserName: config.SwiftUsername,
|
|
|
|
ApiKey: config.SwiftAPIKey,
|
|
|
|
AuthUrl: config.SwiftAuthURL,
|
|
|
|
AuthVersion: config.SwiftAuthVersion,
|
|
|
|
Domain: config.SwiftDomain, // v3 auth only
|
|
|
|
Tenant: config.SwiftTenant, // v2 auth only
|
|
|
|
Timeout: time.Duration(config.SwiftTimeoutSeconds) * time.Second,
|
|
|
|
ConnectTimeout: time.Duration(config.SwiftConnectTimeoutSeconds) * time.Second,
|
2023-04-13 22:05:25 +02:00
|
|
|
Transport: trans,
|
2022-04-06 13:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-04-13 22:05:25 +02:00
|
|
|
err = c.Authenticate(ctx)
|
2022-04-06 13:00:19 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("swift authentication error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport{con: c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
|
|
|
// Users should have converted the object storage URL in the format of swift://{container}/{object}
|
|
|
|
container := req.URL.Host
|
|
|
|
objectName := strings.TrimPrefix(req.URL.Path, "/")
|
|
|
|
|
2022-09-07 13:09:43 +02:00
|
|
|
reqHeaders := make(swift.Headers)
|
|
|
|
if r := req.Header.Get("Range"); len(r) > 0 {
|
|
|
|
reqHeaders["Range"] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
object, objectHeaders, err := t.con.ObjectOpen(req.Context(), container, objectName, false, reqHeaders)
|
2022-04-06 13:00:19 +02:00
|
|
|
|
2022-06-24 12:50:08 +02:00
|
|
|
header := make(http.Header)
|
|
|
|
|
2022-04-06 13:00:19 +02:00
|
|
|
if err != nil {
|
2022-06-24 12:50:08 +02:00
|
|
|
if errors.Is(err, swift.ObjectNotFound) || errors.Is(err, swift.ContainerNotFound) {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusNotFound,
|
|
|
|
Proto: "HTTP/1.0",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
Header: header,
|
|
|
|
Body: io.NopCloser(strings.NewReader(err.Error())),
|
|
|
|
Close: false,
|
|
|
|
Request: req,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:00:19 +02:00
|
|
|
return nil, fmt.Errorf("error opening object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ETagEnabled {
|
2022-04-06 13:35:44 +02:00
|
|
|
if etag, ok := objectHeaders["Etag"]; ok {
|
2022-04-06 13:00:19 +02:00
|
|
|
header.Set("ETag", etag)
|
2023-04-10 19:01:44 +02:00
|
|
|
}
|
2023-05-03 17:21:46 +02:00
|
|
|
}
|
2022-04-06 13:00:19 +02:00
|
|
|
|
2023-05-03 17:21:46 +02:00
|
|
|
if config.LastModifiedEnabled {
|
|
|
|
if lastModified, ok := objectHeaders["Last-Modified"]; ok {
|
|
|
|
header.Set("Last-Modified", lastModified)
|
2022-04-06 13:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:21:46 +02:00
|
|
|
if resp := notmodified.Response(req, header); resp != nil {
|
|
|
|
object.Close()
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:35:44 +02:00
|
|
|
for k, v := range objectHeaders {
|
|
|
|
header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:00:19 +02:00
|
|
|
return &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.0",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
Header: header,
|
2023-05-01 19:50:42 +02:00
|
|
|
Body: object,
|
2022-04-06 13:00:19 +02:00
|
|
|
Close: true,
|
|
|
|
Request: req,
|
|
|
|
}, nil
|
|
|
|
}
|