add option for avatar/profileBg download
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/globals"
|
||||
"go-socket/packages/minio"
|
||||
)
|
||||
|
||||
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, postFile) {
|
||||
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(globals.MaxPostWithFileBytes))
|
||||
|
||||
if err = request.ParseMultipartForm(int64(globals.MaxPostBytes)); err != nil {
|
||||
http.Error(response, "invalid multipart form", http.StatusBadRequest)
|
||||
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)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
key := minio.GetKey(conn.Id, contentType, minio.File)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
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
|
||||
}
|
||||
ctx := request.Context()
|
||||
|
||||
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
conn, ok := getConnectionWithResponseOnFail(&response, request, user)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
key := request.FormValue("key")
|
||||
if !strings.HasPrefix(key, conn.Id.String()+"/") {
|
||||
http.Error(response, "no such file", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, key)
|
||||
if err != nil {
|
||||
http.Error(response, "no such file", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
json, err := json2.Marshal(map[string]string{
|
||||
"url": url.String(),
|
||||
"originalName": meta["originalName"],
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(response, "metadata error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusOK)
|
||||
response.Write(json)
|
||||
}
|
||||
Reference in New Issue
Block a user