add new user set avatar and profile background
This commit is contained in:
@@ -5,11 +5,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/config"
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/minio"
|
||||
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/passwords"
|
||||
"go-socket/packages/postgresql"
|
||||
"go-socket/packages/tokens"
|
||||
@@ -152,7 +149,7 @@ func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var updateList types.UserProfileUpdateList
|
||||
updateList := &types.UserProfileUpdateList{}
|
||||
|
||||
if pronouns := request.FormValue("pronouns"); pronouns != "" {
|
||||
if len(pronouns) > 32 {
|
||||
@@ -190,132 +187,6 @@ func HandleUserModProfile(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserModAvatar(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
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(config.MaxRequestWithAvatarBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(config.MaxRequestBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
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
|
||||
}
|
||||
|
||||
if user.AvatarUrl != "" {
|
||||
err = minio.Delete(ctx, user.AvatarUrl)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
}); err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
user.AvatarUrl = key
|
||||
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{Avatar: true})
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserModProfileBg(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
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(config.MaxRequestWithProfileBgBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(config.MaxRequestBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
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
|
||||
}
|
||||
|
||||
if user.ProfileBgUrl != "" {
|
||||
err = minio.Delete(ctx, user.ProfileBgUrl)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
}); err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
user.ProfileBgUrl = key
|
||||
err = postgresql.UserUpdateProfile(ctx, user, types.UserProfileUpdateList{ProfileBg: true})
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleUserGetUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user