fix: remove 500 error responses from upload endpoints; fix: return validation error strings instead of error lists; fix: handle invalid avatar upload IDs with 400 Bad Request response; fix: add missing S3Controller to controller initialization; fix: change avatar_upload_id to string type and update validation rules; chore: add license header to smtp.go; refactor: replace manual proxy implementation with httputil.ReverseProxy; fix: inject S3Service dependency into ProfileService; fix: set color and color_grad fields during profile update; fix: correct DTO mapping for profile and settings; fix: check object existence before copying in SaveUpload; fix: adjust profile DTO mapping function for proper pointer handling
177 lines
5.0 KiB
Go
177 lines
5.0 KiB
Go
// 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"
|
|
errs "easywish/internal/errors"
|
|
"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.StatObject(context.Background(), sourceBucket, uploadID, minio.StatObjectOptions{})
|
|
if err != nil {
|
|
if minio.ToErrorResponse(err).Code == minio.NoSuchKey {
|
|
return nil, errs.ErrFileNotFound
|
|
}
|
|
}
|
|
_, 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),
|
|
}
|
|
}
|