add new user set avatar and profile background

This commit is contained in:
gitGnome
2026-04-28 11:16:37 +02:00
parent 6f7a913e64
commit 8697cd2632
10 changed files with 203 additions and 228 deletions
+139 -8
View File
@@ -2,6 +2,8 @@ package httpRequest
import (
json2 "encoding/json"
"go-socket/packages/postgresql"
"go-socket/packages/types"
"net/http"
"strings"
@@ -42,7 +44,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
defer file.Close()
contentType := header.Header.Get("Content-Type")
key := minio.GetKey(minio.GetKeyOptions{
key := minio.GetKey(&minio.GetKeyOptions{
ConnectionId: conn.Id,
MimeType: contentType,
UploadType: minio.ConnectionFile,
@@ -60,6 +62,62 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
response.Write([]byte(key))
}
func HandleSetUserAvatar(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, avatar) {
return
}
ctx := request.Context()
user, err := getUserByToken(ctx, request.Header.Get("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
file, header, err := request.FormFile("file")
if err != nil {
http.Error(response, "missing file", http.StatusBadRequest)
return
}
defer file.Close()
isImg, contentType, err := isImage(file)
if err != nil || !isImg {
http.Error(response, "invalid file", http.StatusBadRequest)
return
}
key := minio.GetKey(&minio.GetKeyOptions{
MimeType: contentType,
UploadType: minio.UserAvatar,
UserId: user.Id,
})
err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
"originalName": header.Filename,
"uploaderId": user.Id.String(),
})
if err != nil {
http.Error(response, "upload failed", http.StatusInternalServerError)
return
}
if user.AvatarUrl != "" {
if err = minio.Delete(ctx, user.AvatarUrl); err != nil {
minio.Delete(ctx, key)
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
user.AvatarUrl = key
err = postgresql.UserUpdateProfile(ctx, user, &types.UserProfileUpdateList{Avatar: true})
if err != nil {
http.Error(response, "failed to update user avatar", http.StatusInternalServerError)
minio.Delete(ctx, user.AvatarUrl)
return
}
response.WriteHeader(http.StatusCreated)
}
func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) {
return
@@ -85,18 +143,82 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
}
if target.AvatarUrl == "" {
http.Error(response, "no avatar", http.StatusNotFound)
http.Error(response, "user have no avatar", http.StatusNoContent)
return
}
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarUrl)
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarUrl)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
json, err := json2.Marshal(map[string]any{
"url": url.String(),
"metadata": meta,
})
if err != nil {
http.Error(response, "json error", http.StatusInternalServerError)
}
response.WriteHeader(http.StatusOK)
response.Write([]byte(url.String()))
response.Write(json)
}
func HandleSetUserProfileBg(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, profileBg) {
return
}
ctx := request.Context()
user, err := getUserByToken(ctx, request.Header.Get("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
file, header, err := request.FormFile("file")
if err != nil {
http.Error(response, "missing file", http.StatusBadRequest)
return
}
defer file.Close()
isImg, contentType, err := isImage(file)
if err != nil || !isImg {
http.Error(response, "invalid file", http.StatusBadRequest)
return
}
key := minio.GetKey(&minio.GetKeyOptions{
MimeType: contentType,
UploadType: minio.UserProfileBg,
UserId: user.Id,
})
err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
"originalName": header.Filename,
"uploaderId": user.Id.String(),
})
if err != nil {
http.Error(response, "upload failed", http.StatusInternalServerError)
return
}
if user.ProfileBgUrl != "" {
if err = minio.Delete(ctx, user.ProfileBgUrl); err != nil {
minio.Delete(ctx, key)
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
user.ProfileBgUrl = key
err = postgresql.UserUpdateProfile(ctx, user, &types.UserProfileUpdateList{ProfileBg: true})
if err != nil {
http.Error(response, "failed to update user profile background", http.StatusInternalServerError)
minio.Delete(ctx, user.ProfileBgUrl)
return
}
response.WriteHeader(http.StatusCreated)
}
func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request) {
@@ -105,7 +227,8 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
}
ctx := request.Context()
if _, err := getUserByToken(ctx, request.Header.Get("token")); err != nil {
_, err := getUserByToken(ctx, request.Header.Get("token"))
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
@@ -123,18 +246,26 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
}
if target.ProfileBgUrl == "" {
http.Error(response, "no profile background", http.StatusNotFound)
http.Error(response, "user have no profile background", http.StatusNoContent)
return
}
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgUrl)
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgUrl)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
json, err := json2.Marshal(map[string]any{
"url": url.String(),
"metadata": meta,
})
if err != nil {
http.Error(response, "json error", http.StatusInternalServerError)
}
response.WriteHeader(http.StatusOK)
response.Write([]byte(url.String()))
response.Write(json)
}
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {