114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package minio
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
// Bucket names
|
|
const (
|
|
recipeImgBucket string = "recipeimg"
|
|
)
|
|
|
|
const location string = "eu_ru"
|
|
|
|
type ObjStorage struct {
|
|
minio *minio.Client
|
|
}
|
|
|
|
func New(ctx context.Context, addr, user, password string) (*ObjStorage, error) {
|
|
const op = "media_storage.minio.New"
|
|
|
|
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
|
|
func (o *ObjStorage) uploadFile(ctx context.Context, bucketName string, objectName string, fileBuffer io.Reader, contentType string, fileSize int64) error {
|
|
const op = "media_storage.minio.UploadFile"
|
|
// Upload the zip file with PutObject
|
|
_, err := o.minio.PutObject(ctx, bucketName, objectName, fileBuffer, fileSize, minio.PutObjectOptions{ContentType: contentType})
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Get file from bucket
|
|
func (o *ObjStorage) getFile(ctx context.Context, bucketName string, objectName string) (*minio.Object, error) {
|
|
const op = "media_storage.minio.GetFile"
|
|
// 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
|
|
func (o *ObjStorage) delFile(ctx context.Context, bucketName string, objectName string) error {
|
|
const op = "media_storage.minio.DelFile"
|
|
err := o.minio.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{ForceDelete: true})
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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"
|
|
err := o.checkBucketExists(ctx, recipeImgBucket)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
err = o.uploadFile(ctx, recipeImgBucket, filename, imageFile, contentType, fileSize)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// RecipeImage gets image from recipe's images bucket by filename.
|
|
func (o *ObjStorage) RecipeImage(ctx context.Context, filename string) (*minio.Object, error) {
|
|
const op = "media_storage.minio.RecipeImage"
|
|
err := o.checkBucketExists(ctx, recipeImgBucket)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
obj, err := o.getFile(ctx, recipeImgBucket, filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return obj, err
|
|
}
|