312 lines
8.0 KiB
Go
312 lines
8.0 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
json2 "encoding/json"
|
|
"go-socket/packages/postgresql"
|
|
"go-socket/packages/types"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go-socket/packages/config"
|
|
"go-socket/packages/convertions"
|
|
"go-socket/packages/minio"
|
|
)
|
|
|
|
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, file) {
|
|
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.MaxRequestWithFileBytes))
|
|
|
|
if err = request.ParseMultipartForm(int64(config.MaxRequestBytes)); 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(&minio.GetKeyOptions{
|
|
ConnectionId: conn.Id,
|
|
MimeType: contentType,
|
|
UploadType: minio.ConnectionFile,
|
|
})
|
|
|
|
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 HandleSetUserAvatar(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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
key := minio.GetKey(&minio.GetKeyOptions{
|
|
MimeType: contentType,
|
|
UploadType: minio.UserAvatar,
|
|
UserId: user.Id,
|
|
})
|
|
err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
|
"originalName": header.Filename,
|
|
"uploaderId": user.Id.String(),
|
|
})
|
|
if err != nil {
|
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if user.AvatarUrl != "" {
|
|
if err = minio.Delete(ctx, user.AvatarUrl); err != nil {
|
|
minio.Delete(ctx, key)
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
user.AvatarUrl = 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.AvatarUrl)
|
|
return
|
|
}
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
}
|
|
|
|
func HandleGetUserAvatar(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
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.StringToUuid(request.URL.Query().Get("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.AvatarUrl == "" {
|
|
http.Error(response, "user have no avatar", http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.AvatarUrl)
|
|
if err != nil {
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
json, err := json2.Marshal(map[string]any{
|
|
"url": url.String(),
|
|
"metadata": meta,
|
|
})
|
|
if err != nil {
|
|
http.Error(response, "json error", http.StatusInternalServerError)
|
|
}
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
response.Write(json)
|
|
}
|
|
|
|
func HandleSetUserProfileBg(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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
key := minio.GetKey(&minio.GetKeyOptions{
|
|
MimeType: contentType,
|
|
UploadType: minio.UserProfileBg,
|
|
UserId: user.Id,
|
|
})
|
|
err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
|
"originalName": header.Filename,
|
|
"uploaderId": user.Id.String(),
|
|
})
|
|
if err != nil {
|
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if user.ProfileBgUrl != "" {
|
|
if err = minio.Delete(ctx, user.ProfileBgUrl); err != nil {
|
|
minio.Delete(ctx, key)
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
user.ProfileBgUrl = 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.ProfileBgUrl)
|
|
return
|
|
}
|
|
|
|
response.WriteHeader(http.StatusCreated)
|
|
}
|
|
|
|
func HandleGetUserProfileBg(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
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.StringToUuid(request.URL.Query().Get("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.ProfileBgUrl == "" {
|
|
http.Error(response, "user have no profile background", http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
url, meta, err := minio.GetDownloadUrlAndMetadata(ctx, target.ProfileBgUrl)
|
|
if err != nil {
|
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
json, err := json2.Marshal(map[string]any{
|
|
"url": url.String(),
|
|
"metadata": meta,
|
|
})
|
|
if err != nil {
|
|
http.Error(response, "json error", http.StatusInternalServerError)
|
|
}
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
response.Write(json)
|
|
}
|
|
|
|
func HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
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.URL.Query().Get("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)
|
|
}
|