summaryrefslogtreecommitdiff
path: root/internal/storage/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage/storage.go')
-rw-r--r--internal/storage/storage.go38
1 files changed, 32 insertions, 6 deletions
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index 5d5baf283..f3cb814f1 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -79,6 +79,7 @@ type Driver struct {
Proxy bool
Bucket string
PresignedCache *ttl.Cache[string, PresignedURL]
+ RedirectURL string
}
// Get returns the byte value for key in storage.
@@ -163,12 +164,27 @@ func (d *Driver) URL(ctx context.Context, key string) *PresignedURL {
return &e.Value
}
- u, err := s3.Client().PresignedGetObject(ctx, d.Bucket, key, urlCacheTTL, url.Values{
- "response-content-type": []string{mime.TypeByExtension(path.Ext(key))},
- })
- if err != nil {
- // If URL request fails, fallback is to fetch the file. So ignore the error here
- return nil
+ var (
+ u *url.URL
+ err error
+ )
+
+ if d.RedirectURL != "" {
+ u, err = url.Parse(d.RedirectURL + "/" + key)
+ if err != nil {
+ // If URL parsing fails, fallback is to
+ // fetch the file. So ignore the error here
+ return nil
+ }
+ } else {
+ u, err = s3.Client().PresignedGetObject(ctx, d.Bucket, key, urlCacheTTL, url.Values{
+ "response-content-type": []string{mime.TypeByExtension(path.Ext(key))},
+ })
+ if err != nil {
+ // If URL request fails, fallback is to
+ // fetch the file. So ignore the error here
+ return nil
+ }
}
psu := PresignedURL{
@@ -204,6 +220,14 @@ func (d *Driver) ProbeCSPUri(ctx context.Context) (string, error) {
return "", nil
}
+ // If an S3 redirect URL is set, just
+ // return this URL without probing; we
+ // likely don't have write access on it
+ // anyway since it's probs a CDN bucket.
+ if d.RedirectURL != "" {
+ return d.RedirectURL + "/", nil
+ }
+
const cspKey = "gotosocial-csp-probe"
// Create an empty file in S3 storage.
@@ -273,6 +297,7 @@ func NewS3Storage() (*Driver, error) {
secret := config.GetStorageS3SecretKey()
secure := config.GetStorageS3UseSSL()
bucket := config.GetStorageS3BucketName()
+ redirectURL := config.GetStorageS3RedirectURL()
// Open the s3 storage implementation
s3, err := s3.Open(endpoint, bucket, &s3.Config{
@@ -300,5 +325,6 @@ func NewS3Storage() (*Driver, error) {
Bucket: config.GetStorageS3BucketName(),
Storage: s3,
PresignedCache: presignedCache,
+ RedirectURL: redirectURL,
}, nil
}