feat(backend): add delete avatar logic
This commit is contained in:
parent
163e7c3fd6
commit
57ed81add3
|
@ -200,3 +200,31 @@ func (db *DB) WriteMemberAvatar(ctx context.Context,
|
|||
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
func (db *DB) DeleteUserAvatar(ctx context.Context, userID xid.ID, hash string) error {
|
||||
err := db.minio.RemoveObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".webp", minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting webp avatar")
|
||||
}
|
||||
|
||||
err = db.minio.RemoveObject(ctx, db.minioBucket, "/users/"+userID.String()+"/"+hash+".jpg", minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting jpeg avatar")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) DeleteMemberAvatar(ctx context.Context, memberID xid.ID, hash string) error {
|
||||
err := db.minio.RemoveObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".webp", minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting webp avatar")
|
||||
}
|
||||
|
||||
err = db.minio.RemoveObject(ctx, db.minioBucket, "/members/"+memberID.String()+"/"+hash+".webp", minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting jpeg avatar")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -40,6 +40,13 @@ func (s *Server) deleteMember(w http.ResponseWriter, r *http.Request) error {
|
|||
return errors.Wrap(err, "deleting member")
|
||||
}
|
||||
|
||||
if m.Avatar != nil {
|
||||
err = s.DB.DeleteMemberAvatar(ctx, m.ID, *m.Avatar)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting member avatar")
|
||||
}
|
||||
}
|
||||
|
||||
render.NoContent(w, r)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -129,6 +129,16 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
|
|||
// update avatar
|
||||
var avatarHash *string = nil
|
||||
if req.Avatar != nil {
|
||||
if *req.Avatar == "" {
|
||||
if m.Avatar != nil {
|
||||
err = s.DB.DeleteMemberAvatar(ctx, m.ID, *m.Avatar)
|
||||
if err != nil {
|
||||
log.Errorf("deleting member avatar: %v", err)
|
||||
return errors.Wrap(err, "deleting avatar")
|
||||
}
|
||||
}
|
||||
avatarHash = req.Avatar
|
||||
} else {
|
||||
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar)
|
||||
if err != nil {
|
||||
if err == db.ErrInvalidDataURI {
|
||||
|
@ -154,6 +164,7 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
|
|||
}
|
||||
avatarHash = &hash
|
||||
}
|
||||
}
|
||||
|
||||
// start transaction
|
||||
tx, err := s.DB.Begin(ctx)
|
||||
|
|
|
@ -103,6 +103,16 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|||
// update avatar
|
||||
var avatarHash *string = nil
|
||||
if req.Avatar != nil {
|
||||
if *req.Avatar == "" {
|
||||
if u.Avatar != nil {
|
||||
err = s.DB.DeleteUserAvatar(ctx, u.ID, *u.Avatar)
|
||||
if err != nil {
|
||||
log.Errorf("deleting user avatar: %v", err)
|
||||
return errors.Wrap(err, "deleting avatar")
|
||||
}
|
||||
}
|
||||
avatarHash = req.Avatar
|
||||
} else {
|
||||
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar)
|
||||
if err != nil {
|
||||
if err == db.ErrInvalidDataURI {
|
||||
|
@ -128,6 +138,7 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
|
|||
}
|
||||
avatarHash = &hash
|
||||
}
|
||||
}
|
||||
|
||||
// start transaction
|
||||
tx, err := s.DB.Begin(ctx)
|
||||
|
|
Loading…
Reference in New Issue