diff --git a/pkg/internal/fs/downloader.go b/pkg/internal/fs/downloader.go index 56d9813..569b0d3 100644 --- a/pkg/internal/fs/downloader.go +++ b/pkg/internal/fs/downloader.go @@ -10,7 +10,6 @@ import ( "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "github.com/spf13/viper" ) @@ -31,11 +30,7 @@ func DownloadFileToLocal(meta models.Attachment, dst int) (string, error) { var destConfigured models.S3Destination _ = jsoniter.Unmarshal(rawDest, &destConfigured) - client, err := minio.New(destConfigured.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(destConfigured.SecretID, destConfigured.SecretKey, ""), - Secure: destConfigured.EnableSSL, - BucketLookup: minio.BucketLookupType(destConfigured.BucketLookup), - }) + client, err := destConfigured.GetClient() client.SetAppInfo("HyperNet.Paperclip", pkg.AppVersion) if err != nil { return "", fmt.Errorf("unable to configure s3 client: %v", err) diff --git a/pkg/internal/fs/recycler.go b/pkg/internal/fs/recycler.go index 24bd6a5..767a355 100644 --- a/pkg/internal/fs/recycler.go +++ b/pkg/internal/fs/recycler.go @@ -7,14 +7,12 @@ import ( "path/filepath" "time" - pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "github.com/samber/lo" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "github.com/rs/zerolog/log" "github.com/spf13/viper" ) @@ -121,12 +119,7 @@ func DeleteFileFromLocal(config models.LocalDestination, uuid string) error { } func DeleteFileFromS3(config models.S3Destination, uuid string) error { - client, err := minio.New(config.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(config.SecretID, config.SecretKey, ""), - Secure: config.EnableSSL, - BucketLookup: minio.BucketLookupType(config.BucketLookup), - }) - client.SetAppInfo("HyperNet.Paperclip", pkg.AppVersion) + client, err := config.GetClient() if err != nil { return fmt.Errorf("unable to configure s3 client: %v", err) } diff --git a/pkg/internal/models/destination.go b/pkg/internal/models/destination.go index 62056ff..bea9887 100644 --- a/pkg/internal/models/destination.go +++ b/pkg/internal/models/destination.go @@ -1,5 +1,11 @@ package models +import ( + pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + const ( DestinationTypeLocal = "local" DestinationTypeS3 = "s3" @@ -33,3 +39,15 @@ type S3Destination struct { EnableSigned bool `json:"enable_signed"` BucketLookup int `json:"bucket_lookup"` } + +func (v S3Destination) GetClient() (*minio.Client, error) { + client, err := minio.New(v.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(v.SecretID, v.SecretKey, ""), + Secure: v.EnableSSL, + BucketLookup: minio.BucketLookupType(v.BucketLookup), + }) + if err == nil { + client.SetAppInfo("HyperNet.Paperclip", pkg.AppVersion) + } + return client, err +} diff --git a/pkg/internal/services/opener.go b/pkg/internal/services/opener.go index ab11304..c0b703e 100644 --- a/pkg/internal/services/opener.go +++ b/pkg/internal/services/opener.go @@ -9,7 +9,6 @@ import ( "path/filepath" "time" - pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" localCache "git.solsynth.dev/hypernet/paperclip/pkg/internal/cache" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" @@ -18,7 +17,6 @@ import ( "github.com/eko/gocache/lib/v4/store" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "github.com/samber/lo" ) @@ -118,12 +116,7 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str _ = jsoniter.Unmarshal(rawDest, &destConfigured) if destConfigured.EnableSigned { var client *minio.Client - client, err = minio.New(destConfigured.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(destConfigured.SecretID, destConfigured.SecretKey, ""), - Secure: destConfigured.EnableSSL, - BucketLookup: minio.BucketLookupType(destConfigured.BucketLookup), - }) - client.SetAppInfo("HyperNet.Paperclip", pkg.AppVersion) + client, err = destConfigured.GetClient() if err != nil { return } diff --git a/pkg/internal/services/uploader.go b/pkg/internal/services/uploader.go index 25a5991..39b68e6 100644 --- a/pkg/internal/services/uploader.go +++ b/pkg/internal/services/uploader.go @@ -8,14 +8,12 @@ import ( "os" "path/filepath" - pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" "git.solsynth.dev/hypernet/paperclip/pkg/internal/models" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "github.com/spf13/viper" ) @@ -92,12 +90,7 @@ func ReUploadFile(meta models.Attachment, dst int, doNotUpdate ...bool) error { var destConfigured models.S3Destination _ = jsoniter.Unmarshal(rawDest, &destConfigured) - client, err := minio.New(destConfigured.Endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(destConfigured.SecretID, destConfigured.SecretKey, ""), - Secure: destConfigured.EnableSSL, - BucketLookup: minio.BucketLookupType(destConfigured.BucketLookup), - }) - client.SetAppInfo("HyperNet.Paperclip", pkg.AppVersion) + client, err := destConfigured.GetClient() if err != nil { return fmt.Errorf("unable to configure s3 client: %v", err) }