181 lines
4.5 KiB
Go
181 lines
4.5 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
json2 "encoding/json"
|
|
"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.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 !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.Avatar == "" {
|
|
http.Error(response, "no avatar", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, 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 !validCheckWithResponseOnFail(&response, request, normal) {
|
|
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.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.ProfileBg == "" {
|
|
http.Error(response, "no profile background", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
url, _, err := minio.GetDownloadUrlAndMetadata(ctx, 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 !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)
|
|
}
|