summaryrefslogtreecommitdiff
path: root/internal/storage/storage.go
diff options
context:
space:
mode:
authorLibravatar CDN <cardinal@codeword.info>2024-07-31 20:44:18 +0800
committerLibravatar GitHub <noreply@github.com>2024-07-31 13:44:18 +0100
commit43519324b39de697e3403691fb286de03bf0d4d1 (patch)
tree5a179a34b7bff4e261b7cffaf700a96cbd18246c /internal/storage/storage.go
parentFix no rows in result set error in emoji list command (#3152) (diff)
downloadgotosocial-43519324b39de697e3403691fb286de03bf0d4d1.tar.xz
[feature] Object store custom URL (S3) (#3046)
* tweaks * boobs * fix variable name + typo --------- Co-authored-by: tobi <tobi.smethurst@protonmail.com>
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
}