diff options
Diffstat (limited to 'shrine/utils/storage/storage.go')
| -rw-r--r-- | shrine/utils/storage/storage.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/shrine/utils/storage/storage.go b/shrine/utils/storage/storage.go new file mode 100644 index 0000000..32b3db5 --- /dev/null +++ b/shrine/utils/storage/storage.go @@ -0,0 +1,50 @@ +package storage + +import ( + "context" + "io" + "shrine/config" + "shrine/utils/logger" + "strings" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +var Client *minio.Client + +func init() { + if config.Storage.AccessKey == "" || config.Storage.SecretKey == "" { + logger.Infof("Storage", "MinIO credentials not configured, storage disabled") + return + } + + var err error + Client, err = minio.New(config.Storage.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(config.Storage.AccessKey, config.Storage.SecretKey, ""), + Secure: config.Storage.UseSSL, + }) + if err != nil { + logger.Fatalf("Storage", "Failed to initialize MinIO client: %v", err) + } + + logger.Successf("Storage", "MinIO client initialized for %s", config.Storage.Endpoint) +} + +func Upload(path string, reader io.Reader, size int64, contentType string) error { + _, err := Client.PutObject(context.Background(), config.Storage.Bucket, path, reader, size, minio.PutObjectOptions{ + ContentType: contentType, + }) + return err +} + +func Delete(path string) error { + return Client.RemoveObject(context.Background(), config.Storage.Bucket, path, minio.RemoveObjectOptions{}) +} + +func ResolveCDN(path string) string { + if path == "" { + return "" + } + return strings.TrimRight(config.Storage.CDN, "/") + "/" + config.Storage.Bucket + "/" + path +}
\ No newline at end of file |
