created media storage package + interface
This commit is contained in:
parent
218e7d652f
commit
1a67c02e00
3 changed files with 102 additions and 0 deletions
15
internal/interfaces/mediaStorage.go
Normal file
15
internal/interfaces/mediaStorage.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MediaStorage interface {
|
||||
СreateBucketIfNotExists(bucketName string) error
|
||||
Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error
|
||||
Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error)
|
||||
Delete(ctx context.Context, bucket, object string) error
|
||||
}
|
||||
35
internal/mediaStorage/handler.go
Normal file
35
internal/mediaStorage/handler.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package mediaStorage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
*Service
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func NewHandler(deps Deps) *Handler {
|
||||
endpoint := fmt.Sprintf("%s:%s", deps.Host, deps.Port)
|
||||
minioClient, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(deps.User, deps.Password, ""),
|
||||
Secure: false,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Media storage | Failed to create minio client")
|
||||
}
|
||||
|
||||
return &Handler{
|
||||
newService(minioClient),
|
||||
}
|
||||
}
|
||||
52
internal/mediaStorage/service.go
Normal file
52
internal/mediaStorage/service.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package mediaStorage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/minio/minio-go/v7"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
client *minio.Client
|
||||
}
|
||||
|
||||
func newService(client *minio.Client) *Service {
|
||||
return &Service{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) СreateBucketIfNotExists(bucketName string) error {
|
||||
ctx := context.Background()
|
||||
err := s.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
var minioErr minio.ErrorResponse
|
||||
if errors.As(err, &minioErr) {
|
||||
if minioErr.Code == "BucketAlreadyExists" || minioErr.Code == "BucketAlreadyOwnedByYou" {
|
||||
log.Infof("Media storage | Bucket %s already exists, skipping creation", bucketName)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("failed to create bucket: %w", err)
|
||||
}
|
||||
log.Infof("Media storage | Bucket %s created successfully", bucketName)
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, bucket, object string, expires time.Duration, params url.Values) (*url.URL, error) {
|
||||
return s.client.PresignedGetObject(ctx, bucket, object, expires, params)
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, bucket, object string) error {
|
||||
return s.client.RemoveObject(ctx, bucket, object, minio.RemoveObjectOptions{})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue