add hub function
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, file) {
|
||||
if !validCheckWithResponseOnFail(response, request, file) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -36,7 +36,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func HandleSetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, avatar) {
|
||||
if !validCheckWithResponseOnFail(response, request, avatar) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -105,18 +105,18 @@ func HandleSetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if user.AvatarType != "" {
|
||||
if err = minio.Delete(ctx, user.AvatarType); err != nil {
|
||||
if user.AvatarKey != "" {
|
||||
if err = minio.Delete(ctx, user.AvatarKey); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
user.AvatarType = key
|
||||
user.AvatarKey = 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.AvatarType)
|
||||
minio.Delete(ctx, user.AvatarKey)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func HandleSetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -165,31 +165,32 @@ func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if target.AvatarType == "" {
|
||||
if target.AvatarKey == "" {
|
||||
http.Error(response, "user have no avatar", http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarType)
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarKey)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(map[string]any{
|
||||
avatarData, err := json.Marshal(map[string]any{
|
||||
"url": url.String(),
|
||||
"metadata": meta,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(response, "json error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
response.Write(avatarData)
|
||||
}
|
||||
|
||||
func HandleSetUserProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, profileBg) {
|
||||
if !validCheckWithResponseOnFail(response, request, profileBg) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -226,18 +227,18 @@ func HandleSetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if user.ProfileBgType != "" {
|
||||
if err = minio.Delete(ctx, user.ProfileBgType); err != nil {
|
||||
if user.ProfileBgKey != "" {
|
||||
if err = minio.Delete(ctx, user.ProfileBgKey); err != nil {
|
||||
minio.Delete(ctx, key)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
user.ProfileBgType = key
|
||||
user.ProfileBgKey = 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.ProfileBgType)
|
||||
minio.Delete(ctx, user.ProfileBgKey)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -262,7 +263,7 @@ func HandleSetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -285,31 +286,32 @@ func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if target.ProfileBgType == "" {
|
||||
if target.ProfileBgKey == "" {
|
||||
http.Error(response, "user have no profile background", http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgType)
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgKey)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(map[string]any{
|
||||
profileBgData, err := json.Marshal(map[string]any{
|
||||
"url": url.String(),
|
||||
"metadata": meta,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(response, "json error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
response.Write(profileBgData)
|
||||
}
|
||||
|
||||
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
if !validCheckWithResponseOnFail(response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
@@ -320,7 +322,7 @@ func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
conn, ok := getConnectionWithResponseOnFail(response, request.FormValue("connectionid"), user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -337,7 +339,7 @@ func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(map[string]string{
|
||||
fileData, err := json.Marshal(map[string]string{
|
||||
"url": url.String(),
|
||||
"originalName": meta["originalName"],
|
||||
})
|
||||
@@ -347,5 +349,5 @@ func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Re
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
response.Write(fileData)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user