feat(backend): add delete avatar logic

This commit is contained in:
Sam 2023-03-13 02:19:03 +01:00
parent 163e7c3fd6
commit 57ed81add3
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
4 changed files with 95 additions and 38 deletions

View File

@ -200,3 +200,31 @@ func (db *DB) WriteMemberAvatar(ctx context.Context,
return hash, nil 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
}

View File

@ -40,6 +40,13 @@ func (s *Server) deleteMember(w http.ResponseWriter, r *http.Request) error {
return errors.Wrap(err, "deleting member") 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) render.NoContent(w, r)
return nil return nil
} }

View File

@ -129,30 +129,41 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
// update avatar // update avatar
var avatarHash *string = nil var avatarHash *string = nil
if req.Avatar != nil { if req.Avatar != nil {
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar) if *req.Avatar == "" {
if err != nil { if m.Avatar != nil {
if err == db.ErrInvalidDataURI { err = s.DB.DeleteMemberAvatar(ctx, m.ID, *m.Avatar)
return server.APIError{ if err != nil {
Code: server.ErrBadRequest, log.Errorf("deleting member avatar: %v", err)
Details: "invalid avatar data URI", return errors.Wrap(err, "deleting avatar")
}
} else if err == db.ErrInvalidContentType {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar content type",
} }
} }
avatarHash = req.Avatar
} else {
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar)
if err != nil {
if err == db.ErrInvalidDataURI {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar data URI",
}
} else if err == db.ErrInvalidContentType {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar content type",
}
}
log.Errorf("converting member avatar: %v", err) log.Errorf("converting member avatar: %v", err)
return err return err
} }
hash, err := s.DB.WriteMemberAvatar(ctx, id, webp, jpg) hash, err := s.DB.WriteMemberAvatar(ctx, id, webp, jpg)
if err != nil { if err != nil {
log.Errorf("uploading member avatar: %v", err) log.Errorf("uploading member avatar: %v", err)
return err return err
}
avatarHash = &hash
} }
avatarHash = &hash
} }
// start transaction // start transaction

View File

@ -103,30 +103,41 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error {
// update avatar // update avatar
var avatarHash *string = nil var avatarHash *string = nil
if req.Avatar != nil { if req.Avatar != nil {
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar) if *req.Avatar == "" {
if err != nil { if u.Avatar != nil {
if err == db.ErrInvalidDataURI { err = s.DB.DeleteUserAvatar(ctx, u.ID, *u.Avatar)
return server.APIError{ if err != nil {
Code: server.ErrBadRequest, log.Errorf("deleting user avatar: %v", err)
Details: "invalid avatar data URI", return errors.Wrap(err, "deleting avatar")
}
} else if err == db.ErrInvalidContentType {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar content type",
} }
} }
avatarHash = req.Avatar
} else {
webp, jpg, err := s.DB.ConvertAvatar(*req.Avatar)
if err != nil {
if err == db.ErrInvalidDataURI {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar data URI",
}
} else if err == db.ErrInvalidContentType {
return server.APIError{
Code: server.ErrBadRequest,
Details: "invalid avatar content type",
}
}
log.Errorf("converting user avatar: %v", err) log.Errorf("converting user avatar: %v", err)
return err return err
} }
hash, err := s.DB.WriteUserAvatar(ctx, claims.UserID, webp, jpg) hash, err := s.DB.WriteUserAvatar(ctx, claims.UserID, webp, jpg)
if err != nil { if err != nil {
log.Errorf("uploading user avatar: %v", err) log.Errorf("uploading user avatar: %v", err)
return err return err
}
avatarHash = &hash
} }
avatarHash = &hash
} }
// start transaction // start transaction