chore: remove direct avatar upload endpoint (POST /profile/avatar);
feat: add endpoints for presigned upload URLs (GET /upload/avatar, GET /upload/image); refactor: replace ProfileDto with NewProfileDto in update profile endpoint; feat: implement S3 integration for avatar management; fix: update database queries to handle new avatar upload flow; chore: add new dependencies for S3 handling (golang.org/x/time); refactor: rename UploadService to S3Service; refactor: change return type for func LocalizeS3Url(originalURL string) (*url.URL, error); feat: add custom validator for upload_id
This commit is contained in:
169
backend/internal/services/s3.go
Normal file
169
backend/internal/services/s3.go
Normal file
@@ -0,0 +1,169 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"easywish/config"
|
||||
minioclient "easywish/internal/minioClient"
|
||||
"easywish/internal/utils"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type S3Service interface {
|
||||
CreateAvatarUrl() (*string, *map[string]string, error)
|
||||
CreateImageUrl() (*string, *map[string]string, error)
|
||||
SaveUpload(uploadID string, bucket string) (*string, error)
|
||||
|
||||
GetLocalizedFileUrl(key string, bucket string) url.URL
|
||||
}
|
||||
|
||||
type s3ServiceImpl struct {
|
||||
minio *minio.Client
|
||||
log *zap.Logger
|
||||
|
||||
avatarPolicy minio.PostPolicy
|
||||
imagePolicy minio.PostPolicy
|
||||
}
|
||||
|
||||
func NewUploadService(_minio *minio.Client, _log *zap.Logger) S3Service {
|
||||
service := s3ServiceImpl{
|
||||
minio: _minio,
|
||||
log: _log,
|
||||
}
|
||||
|
||||
avatarPolicy := minio.NewPostPolicy()
|
||||
imagePolicy := minio.NewPostPolicy()
|
||||
|
||||
// At the moment the parameters match for both policies but this may
|
||||
// change with introduction of new policies
|
||||
for _, policy := range [...]*minio.PostPolicy{avatarPolicy, imagePolicy} {
|
||||
if err := policy.SetBucket(minioclient.Buckets["uploads"]); err != nil {
|
||||
panic("Failed to set bucket for policy: " + err.Error())
|
||||
}
|
||||
|
||||
if err := policy.SetExpires(time.Now().UTC().Add(10 * time.Minute)); err != nil {
|
||||
panic("Failed to set expiration time for policy: " + err.Error())
|
||||
}
|
||||
|
||||
if err := policy.SetContentTypeStartsWith("image/"); err != nil {
|
||||
panic("Failed to set allowed content types for the policy: " + err.Error())
|
||||
}
|
||||
|
||||
if err := policy.SetContentLengthRange(1, 512*1024); err != nil {
|
||||
panic("Failed to set allowed content length range for the policy: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
service.imagePolicy = *imagePolicy
|
||||
service.avatarPolicy = *avatarPolicy
|
||||
|
||||
return &service
|
||||
}
|
||||
|
||||
func (s *s3ServiceImpl) genUrl(policy minio.PostPolicy, prefix string) (*string, *map[string]string, error) {
|
||||
|
||||
object := prefix + uuid.New().String()
|
||||
|
||||
if err := policy.SetKey(object); err != nil {
|
||||
s.log.Error(
|
||||
"Failed to set random key for presigned url",
|
||||
zap.Error(err))
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
url, formData, err := s.minio.PresignedPostPolicy(context.Background(), &policy)
|
||||
if err != nil {
|
||||
s.log.Error(
|
||||
"Failed to generate presigned url",
|
||||
zap.String("object", object),
|
||||
zap.Error(err))
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
convertedUrl, err := utils.LocalizeS3Url(url.String())
|
||||
if err != nil {
|
||||
s.log.Error(
|
||||
"Failed to localize object URL to user-accessible format",
|
||||
zap.String("url", url.String()),
|
||||
zap.Error(err))
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return utils.NewPointer(convertedUrl.String()), &formData, nil
|
||||
}
|
||||
|
||||
func (s *s3ServiceImpl) CreateAvatarUrl() (*string, *map[string]string, error) {
|
||||
return s.genUrl(s.avatarPolicy, "avatar-")
|
||||
}
|
||||
|
||||
func (s *s3ServiceImpl) CreateImageUrl() (*string, *map[string]string, error) {
|
||||
return s.genUrl(s.imagePolicy, "image-")
|
||||
}
|
||||
|
||||
func (s *s3ServiceImpl) SaveUpload(uploadID string, bucketAlias string) (*string, error) {
|
||||
sourceBucket := minioclient.Buckets["uploads"]
|
||||
bucket := minioclient.Buckets[bucketAlias]
|
||||
newObjectKey := uuid.New().String()
|
||||
|
||||
_, err := s.minio.CopyObject(context.Background(), minio.CopyDestOptions{
|
||||
Bucket: bucket,
|
||||
Object: newObjectKey,
|
||||
}, minio.CopySrcOptions{
|
||||
Bucket: sourceBucket,
|
||||
Object: uploadID,
|
||||
})
|
||||
if err != nil {
|
||||
s.log.Error(
|
||||
"Failed to copy object to new bucket",
|
||||
zap.String("sourceBucket", sourceBucket),
|
||||
zap.String("uploadID", uploadID),
|
||||
zap.String("destinationBucket", bucket),
|
||||
zap.String("newObjectKey", newObjectKey),
|
||||
zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.minio.RemoveObject(context.Background(), sourceBucket, uploadID, minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
s.log.Error(
|
||||
"Failed to remove original object from uploads bucket",
|
||||
zap.String("sourceBucket", sourceBucket),
|
||||
zap.String("uploadID", uploadID),
|
||||
zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &newObjectKey, nil
|
||||
}
|
||||
|
||||
func (s *s3ServiceImpl) GetLocalizedFileUrl(key string, bucketAlias string) url.URL {
|
||||
cfg := config.GetConfig()
|
||||
|
||||
return url.URL{
|
||||
Scheme: "http",
|
||||
Host: fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port),
|
||||
Path: fmt.Sprintf("/s3/%s/%s", minioclient.Buckets[bucketAlias], key),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user