add option for avatar/profileBg download
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/globals"
|
||||
"go-socket/packages/minio"
|
||||
)
|
||||
@@ -55,6 +56,83 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
response.Write([]byte(key))
|
||||
}
|
||||
|
||||
func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
_, err := getUserByToken(ctx, request.Header.Get("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := convertions.ConvertStringUuid(request.FormValue("userid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid userid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
target, err := getUserById(ctx, targetId)
|
||||
if err != nil {
|
||||
http.Error(response, "user not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if target.Avatar == "" {
|
||||
http.Error(response, "no avatar", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, string(minio.UserAvatarPrefix)+target.Avatar)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write([]byte(url.String()))
|
||||
}
|
||||
|
||||
func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
if _, err := getUserByToken(ctx, request.Header.Get("token")); err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
targetId, err := convertions.ConvertStringUuid(request.FormValue("userid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid userid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
target, err := getUserById(ctx, targetId)
|
||||
if err != nil {
|
||||
http.Error(response, "user not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if target.ProfileBg == "" {
|
||||
http.Error(response, "no profile background", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, string(minio.UserProfileBgPrefix)+target.ProfileBg)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write([]byte(url.String()))
|
||||
}
|
||||
|
||||
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postNormal) {
|
||||
return
|
||||
@@ -2,9 +2,10 @@ package httpRequest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go-socket/packages/convertions"
|
||||
"net/http"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/postgresql"
|
||||
"go-socket/packages/tokens"
|
||||
|
||||
Reference in New Issue
Block a user