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:
@@ -21,6 +21,7 @@ import (
|
||||
"easywish/internal/database"
|
||||
"easywish/internal/dto"
|
||||
errs "easywish/internal/errors"
|
||||
"easywish/internal/utils"
|
||||
mapspecial "easywish/internal/utils/mapSpecial"
|
||||
"errors"
|
||||
"time"
|
||||
@@ -36,10 +37,9 @@ import (
|
||||
type ProfileService interface {
|
||||
GetProfileByUsername(cinfo dto.ClientInfo, username string) (*dto.ProfileDto, error)
|
||||
GetMyProfile(cinfo dto.ClientInfo) (*dto.ProfileDto, error)
|
||||
UpdateProfile(cinfo dto.ClientInfo, newProfile dto.ProfileDto) (bool, error)
|
||||
UpdateProfile(cinfo dto.ClientInfo, newProfile dto.NewProfileDto) (bool, error)
|
||||
GetProfileSettings(cinfo dto.ClientInfo) (*dto.ProfileSettingsDto, error)
|
||||
UpdateProfileSettings(cinfo dto.ClientInfo, newProfileSettings dto.ProfileSettingsDto) (bool, error)
|
||||
UploadAvatar(cinfo dto.ClientInfo, filePath string) (*string, error)
|
||||
}
|
||||
|
||||
type profileServiceImpl struct {
|
||||
@@ -47,6 +47,7 @@ type profileServiceImpl struct {
|
||||
dbctx database.DbContext
|
||||
redis *redis.Client
|
||||
minio *minio.Client
|
||||
s3 S3Service
|
||||
}
|
||||
|
||||
func NewProfileService(_log *zap.Logger, _dbctx database.DbContext, _redis *redis.Client, _minio *minio.Client) ProfileService {
|
||||
@@ -117,12 +118,12 @@ func (p *profileServiceImpl) GetProfileSettings(cinfo dto.ClientInfo) (*dto.Prof
|
||||
}
|
||||
|
||||
// XXX: no validation for timestamps' allowed ranges
|
||||
func (p *profileServiceImpl) UpdateProfile(cinfo dto.ClientInfo, newProfile dto.ProfileDto) (bool, error) {
|
||||
func (p *profileServiceImpl) UpdateProfile(cinfo dto.ClientInfo, newProfile dto.NewProfileDto) (bool, error) {
|
||||
helper, db, err := database.NewDbHelperTransaction(p.dbctx); if err != nil {
|
||||
p.log.Error(
|
||||
"Failed to open transaction",
|
||||
zap.Error(err))
|
||||
return false, err
|
||||
return false, errs.ErrServerError
|
||||
}
|
||||
defer helper.Rollback()
|
||||
|
||||
@@ -131,11 +132,25 @@ func (p *profileServiceImpl) UpdateProfile(cinfo dto.ClientInfo, newProfile dto.
|
||||
Valid: true,
|
||||
}
|
||||
|
||||
var avatarUrl *string
|
||||
if newProfile.AvatarUploadID != nil {
|
||||
key, err := p.s3.SaveUpload(*newProfile.AvatarUploadID, "avatars"); if err != nil {
|
||||
p.log.Error("Failed to save avatar",
|
||||
zap.String("upload_id", *newProfile.AvatarUploadID),
|
||||
zap.Error(err))
|
||||
return false, errs.ErrServerError
|
||||
}
|
||||
|
||||
urlObj := p.s3.GetLocalizedFileUrl(*key, "avatars")
|
||||
avatarUrl = utils.NewPointer(urlObj.String())
|
||||
}
|
||||
|
||||
err = db.TXlessQueries.UpdateProfileByUsername(db.CTX, database.UpdateProfileByUsernameParams{
|
||||
Username: cinfo.Username,
|
||||
Name: newProfile.Name,
|
||||
Bio: newProfile.Bio,
|
||||
Birthday: birthdayTimestamp,
|
||||
AvatarUrl: avatarUrl,
|
||||
}); if err != nil {
|
||||
p.log.Error(
|
||||
"Failed to update user profile",
|
||||
@@ -193,8 +208,3 @@ func (p *profileServiceImpl) UpdateProfileSettings(cinfo dto.ClientInfo, newProf
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TODO: implement S3 before I can do anything with it
|
||||
func (p *profileServiceImpl) UploadAvatar(cinfo dto.ClientInfo, filePath string) (*string, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user