2025-10-15 19:44:41 +03:00
|
|
|
package mediaStorage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"io"
|
|
|
|
|
"net/url"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
2025-10-18 15:30:59 +03:00
|
|
|
client *minio.Client
|
|
|
|
|
domain string
|
|
|
|
|
endpoint string
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-18 16:38:21 +03:00
|
|
|
func newService(client *minio.Client) *Service {
|
2025-10-15 19:44:41 +03:00
|
|
|
return &Service{
|
|
|
|
|
client: client,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 21:34:32 +03:00
|
|
|
func (s *Service) CheckBucketExists(bucketName string) (bool, error) {
|
2025-10-15 19:44:41 +03:00
|
|
|
ctx := context.Background()
|
2025-10-15 21:34:32 +03:00
|
|
|
exists, err := s.client.BucketExists(ctx, bucketName)
|
2025-10-15 19:44:41 +03:00
|
|
|
if err != nil {
|
2025-10-15 21:34:32 +03:00
|
|
|
log.WithError(err).Fatal("Media storage | Failed to check bucket existence")
|
|
|
|
|
return exists, err
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
2025-10-15 21:34:32 +03:00
|
|
|
log.Infof("Media storage | Bucket %s exists", bucketName)
|
|
|
|
|
return exists, nil
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error {
|
|
|
|
|
_, err := s.client.PutObject(ctx, bucket, object, reader, size, minio.PutObjectOptions{ContentType: "image/jpeg"})
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-18 15:30:59 +03:00
|
|
|
func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) {
|
|
|
|
|
presigned, err := s.client.PresignedGetObject(ctx, bucket, object, expires, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-18 16:38:21 +03:00
|
|
|
return presigned.String(), nil
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Delete(ctx context.Context, bucket, object string) error {
|
|
|
|
|
return s.client.RemoveObject(ctx, bucket, object, minio.RemoveObjectOptions{})
|
|
|
|
|
}
|
2025-10-17 23:47:48 +03:00
|
|
|
|
|
|
|
|
func (s *Service) GetObjectEtag(ctx context.Context, bucketName, object string) (string, error) {
|
|
|
|
|
info, err := s.client.StatObject(ctx, bucketName, object, minio.StatObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return info.ETag, nil
|
|
|
|
|
}
|