2025-10-15 19:44:41 +03:00
|
|
|
package mediaStorage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-10-19 19:43:33 +03:00
|
|
|
"fmt"
|
2025-10-15 19:44:41 +03:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"io"
|
|
|
|
|
"net/url"
|
2025-10-19 19:43:33 +03:00
|
|
|
"strings"
|
2025-10-15 19:44:41 +03:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Service struct {
|
2025-10-19 19:43:33 +03:00
|
|
|
client *minio.Client
|
|
|
|
|
endpoint string
|
|
|
|
|
secureMode bool
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 19:43:33 +03:00
|
|
|
func newService(client *minio.Client, endpoint string, secureMode bool) *Service {
|
2025-10-15 19:44:41 +03:00
|
|
|
return &Service{
|
2025-10-19 19:43:33 +03:00
|
|
|
client: client,
|
|
|
|
|
endpoint: endpoint,
|
|
|
|
|
secureMode: secureMode,
|
2025-10-15 19:44:41 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-19 19:43:33 +03:00
|
|
|
func (s *Service) GetPublicLink(ctx context.Context, bucket, object string) (string, string, error) {
|
|
|
|
|
stat, err := s.client.StatObject(ctx, bucket, object, minio.StatObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
|
"error": err,
|
|
|
|
|
"key": bucket + "/" + object,
|
|
|
|
|
}).Error("Media storage | Failed to get public link")
|
|
|
|
|
return "", "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var scheme string
|
|
|
|
|
if s.secureMode {
|
|
|
|
|
scheme = "https"
|
|
|
|
|
} else {
|
|
|
|
|
scheme = "http"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
link := fmt.Sprintf("%s://%s/%s/%s", scheme, strings.TrimRight(s.endpoint, "/"), bucket, object)
|
|
|
|
|
log.WithFields(log.Fields{"link": link}).Debug("Media storage | Get public link")
|
|
|
|
|
|
|
|
|
|
return link, stat.ETag, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetPresignedLink(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (string, error) {
|
2025-10-18 15:30:59 +03:00
|
|
|
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
|
|
|
|
|
}
|