getting user; mod: avatar, profileBg, profile now working

This commit is contained in:
2026-04-19 18:27:36 +02:00
parent 61936b4576
commit f24cab8fb0
9 changed files with 390 additions and 199 deletions
@@ -266,6 +266,7 @@ func HandleUserDeleteConnection(response http.ResponseWriter, request *http.Requ
})
response.WriteHeader(http.StatusAccepted)
response.Write(conn.Id[:])
}
func HandleUserElevateConnection(response http.ResponseWriter, request *http.Request) {
+7 -3
View File
@@ -42,7 +42,11 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
defer file.Close()
contentType := header.Header.Get("Content-Type")
key := minio.GetKey(conn.Id, contentType, minio.File)
key := minio.GetKey(minio.GetKeyOptions{
ConnectionId: conn.Id,
MimeType: contentType,
UploadType: minio.File,
})
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
"originalName": header.Filename,
@@ -85,7 +89,7 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
return
}
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, string(minio.UserAvatarPrefix)+target.Avatar)
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, target.Avatar)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
@@ -123,7 +127,7 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
return
}
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, string(minio.UserProfileBgPrefix)+target.ProfileBg)
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBg)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
+14 -16
View File
@@ -209,11 +209,6 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
return
}
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
if !ok {
return
}
file, header, err := request.FormFile("file")
if err != nil {
http.Error(response, "missing file", http.StatusBadRequest)
@@ -228,14 +223,18 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
}
if user.Avatar != "" {
err = minio.Delete(ctx, string(minio.UserAvatarPrefix)+user.Avatar)
err = minio.Delete(ctx, user.Avatar)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
key := minio.GetKey(conn.Id, contentType, minio.File)
key := minio.GetKey(minio.GetKeyOptions{
UserId: user.Id,
MimeType: contentType,
UploadType: minio.UserAvatar,
})
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
"originalName": header.Filename,
"uploaderId": user.Id.String(),
@@ -244,7 +243,7 @@ func HandleUserModAvatar(response http.ResponseWriter, request *http.Request) {
return
}
user.Avatar = key[len(minio.UserAvatarPrefix):]
user.Avatar = key
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{Avatar: true})
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
@@ -273,11 +272,6 @@ func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request)
return
}
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
if !ok {
return
}
file, header, err := request.FormFile("file")
if err != nil {
http.Error(response, "missing file", http.StatusBadRequest)
@@ -292,14 +286,18 @@ func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request)
}
if user.ProfileBg != "" {
err = minio.Delete(ctx, string(minio.UserProfileBgPrefix)+user.ProfileBg)
err = minio.Delete(ctx, user.ProfileBg)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
key := minio.GetKey(conn.Id, contentType, minio.UserProfileBg)
key := minio.GetKey(minio.GetKeyOptions{
UserId: user.Id,
MimeType: contentType,
UploadType: minio.UserProfileBg,
})
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
"originalName": header.Filename,
"uploaderId": user.Id.String(),
@@ -308,7 +306,7 @@ func HandleUserModProfileBg(response http.ResponseWriter, request *http.Request)
return
}
user.ProfileBg = key[len(minio.UserProfileBgPrefix):]
user.ProfileBg = key
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{ProfileBg: true})
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
+17 -8
View File
@@ -33,20 +33,29 @@ const (
UserProfileBgPrefix DataTypePrefix = "userProfileBg/"
)
func GetKey(connectionId uuid.UUID, mimeType string, uploadType DataType) string {
extensions, err := mime.ExtensionsByType(mimeType)
type GetKeyOptions struct {
UserId uuid.UUID
ConnectionId uuid.UUID
MimeType string
UploadType DataType
}
func GetKey(opts GetKeyOptions) string {
extensions, err := mime.ExtensionsByType(opts.MimeType)
if err != nil || len(extensions) == 0 {
extensions = []string{".unknown"}
}
key := connectionId.String() + "/" + strconv.FormatInt(time.Now().UnixMilli(), 10) + extensions[0]
key := "/" + strconv.FormatInt(time.Now().UnixMilli(), 10) + extensions[0]
if uploadType == UserAvatar {
return string(UserAvatarPrefix) + key
} else if uploadType == UserProfileBg {
return string(UserProfileBgPrefix) + key
switch opts.UploadType {
case UserAvatar:
return string(UserAvatarPrefix) + opts.UserId.String() + key
case UserProfileBg:
return string(UserProfileBgPrefix) + opts.UserId.String() + key
default:
return string(FilePrefix) + opts.ConnectionId.String() + key
}
return string(FilePrefix) + key
}
func Init(ctx context.Context) {
+2 -2
View File
@@ -30,8 +30,8 @@ type User struct {
Pronouns string `json:"pronouns"`
Description string `json:"description"`
Avatar string `json:"avatar"`
ProfileBg string `json:"profileBg"`
PasswordHash string `json:"passwordHash"`
ProfileBg string `json:"profileBackground"`
PasswordHash string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
WsConn *websocket.Conn `json:"-"`
Id uuid.UUID `json:"-"`