2024-01-16 21:10:29 +02:00
|
|
|
package minio
|
|
|
|
|
|
|
|
import (
|
2024-01-20 20:33:49 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2024-01-16 21:10:29 +02:00
|
|
|
"github.com/minio/minio-go/v7"
|
2024-01-20 20:33:49 +02:00
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bucket names
|
2024-01-20 21:01:32 +02:00
|
|
|
const (
|
|
|
|
recipeImgBucket string = "recipeimg"
|
2024-01-16 21:10:29 +02:00
|
|
|
)
|
|
|
|
|
2024-01-20 20:33:49 +02:00
|
|
|
const location string = "eu_ru"
|
|
|
|
|
2024-01-18 18:48:27 +02:00
|
|
|
type ObjStorage struct {
|
2024-01-16 21:10:29 +02:00
|
|
|
minio *minio.Client
|
|
|
|
}
|
|
|
|
|
2024-01-20 20:33:49 +02:00
|
|
|
func New(ctx context.Context, addr, user, password string) (*ObjStorage, error) {
|
2024-01-21 12:10:06 +02:00
|
|
|
const op = "media_storage.minio.New"
|
2024-01-20 20:33:49 +02:00
|
|
|
|
|
|
|
minioClient, err := minio.New(addr, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(user, password, ""),
|
|
|
|
Secure: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ObjStorage{minio: minioClient}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload file to bucket
|
2024-01-20 21:01:32 +02:00
|
|
|
func (o *ObjStorage) uploadFile(ctx context.Context, bucketName string, objectName string, fileBuffer io.Reader, contentType string, fileSize int64) error {
|
2024-01-21 12:10:06 +02:00
|
|
|
const op = "media_storage.minio.UploadFile"
|
2024-01-20 20:33:49 +02:00
|
|
|
// Upload the zip file with PutObject
|
2024-01-21 12:52:21 +02:00
|
|
|
_, err := o.minio.PutObject(ctx, bucketName, objectName, fileBuffer, fileSize, minio.PutObjectOptions{ContentType: contentType})
|
2024-01-20 20:33:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file from bucket
|
2024-01-20 21:01:32 +02:00
|
|
|
func (o *ObjStorage) getFile(ctx context.Context, bucketName string, objectName string) (*minio.Object, error) {
|
2024-01-21 12:10:06 +02:00
|
|
|
const op = "media_storage.minio.GetFile"
|
2024-01-20 20:33:49 +02:00
|
|
|
// Get object from minio
|
|
|
|
minio_obj, err := o.minio.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{Checksum: true})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
return minio_obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete file from bucket
|
2024-01-25 14:44:38 +02:00
|
|
|
//
|
|
|
|
//nolint:unused
|
2024-01-20 21:01:32 +02:00
|
|
|
func (o *ObjStorage) delFile(ctx context.Context, bucketName string, objectName string) error {
|
2024-01-21 12:10:06 +02:00
|
|
|
const op = "media_storage.minio.DelFile"
|
2024-01-20 20:33:49 +02:00
|
|
|
err := o.minio.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{ForceDelete: true})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
return nil
|
2024-01-16 21:10:29 +02:00
|
|
|
}
|
2024-01-20 21:01:32 +02:00
|
|
|
|
2024-01-21 12:52:21 +02:00
|
|
|
// checkBucketExists creates bucket if it don't exists.
|
|
|
|
func (o *ObjStorage) checkBucketExists(ctx context.Context, bucketName string) error {
|
|
|
|
const op = "media_storage.minio.checkBucketExists"
|
|
|
|
// Check exists
|
|
|
|
exists, err := o.minio.BucketExists(ctx, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
// Create bucket
|
|
|
|
err := o.minio.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 12:10:06 +02:00
|
|
|
// SaveRecipeImage saves image to bucket for recipes photos.
|
|
|
|
func (o *ObjStorage) SaveRecipeImage(ctx context.Context, imageFile io.Reader, filename string, contentType string, fileSize int64) error {
|
|
|
|
const op = "media_storage.minio.SaveRecipeImage"
|
2024-01-21 12:52:21 +02:00
|
|
|
err := o.checkBucketExists(ctx, recipeImgBucket)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
err = o.uploadFile(ctx, recipeImgBucket, filename, imageFile, contentType, fileSize)
|
2024-01-21 12:10:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecipeImage gets image from recipe's images bucket by filename.
|
2024-01-23 13:40:45 +02:00
|
|
|
func (o *ObjStorage) RecipeImage(ctx context.Context, filename string) (io.ReadCloser, error) {
|
2024-01-21 12:10:06 +02:00
|
|
|
const op = "media_storage.minio.RecipeImage"
|
2024-01-21 12:52:21 +02:00
|
|
|
err := o.checkBucketExists(ctx, recipeImgBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
2024-01-21 12:10:06 +02:00
|
|
|
obj, err := o.getFile(ctx, recipeImgBucket, filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
|
|
}
|
|
|
|
return obj, err
|
2024-01-20 21:01:32 +02:00
|
|
|
}
|