// 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 . package services import ( "context" minioclient "easywish/internal/minioClient" "easywish/internal/utils" "time" "github.com/google/uuid" "github.com/minio/minio-go/v7" "go.uber.org/zap" ) type UploadService interface { GetAvatarUrl() (*string, *map[string]string, error) GetImageUrl() (*string, *map[string]string, error) } type uploadServiceImpl struct { minio *minio.Client log *zap.Logger avatarPolicy minio.PostPolicy imagePolicy minio.PostPolicy } func NewUploadService(_minio *minio.Client, _log *zap.Logger) UploadService { service := uploadServiceImpl{ 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 (u *uploadServiceImpl) genUrl(policy minio.PostPolicy, prefix string) (*string, *map[string]string, error) { object := prefix + uuid.New().String() if err := policy.SetKey(object); err != nil { u.log.Error( "Failed to set random key for presigned url", zap.Error(err)) return nil, nil, err } url, formData, err := u.minio.PresignedPostPolicy(context.Background(), &policy) if err != nil { u.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 { u.log.Error( "Failed to localize object URL to user-accessible format", zap.String("url", url.String()), zap.Error(err)) return nil, nil, err } return &convertedUrl, &formData, nil } func (u *uploadServiceImpl) GetAvatarUrl() (*string, *map[string]string, error) { return u.genUrl(u.avatarPolicy, "avatar-") } func (u *uploadServiceImpl) GetImageUrl() (*string, *map[string]string, error) { return u.genUrl(u.imagePolicy, "image-") }