add dynamic profile update, connection helper, file download metadata
- replace UserSetColor/UserSetPronouns with single UserUpdateProfile that dynamically builds one UPDATE query from UserProfileUpdateList - add getConnectionWithResponseOnFail helper to deduplicate connection ID parsing and validation across handlers - rename file.go to attachmentFile.go and update handler names - GetDownloadUrlAndMetadata now fetches object metadata via StatObject and returns it alongside the presigned URL - file download endpoint returns JSON with url and originalName - add description field to user and DB schema - remove unused ConnectionState variants (GroupFellow, GroupFriend) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package httpRequest
|
||||
|
||||
import (
|
||||
json2 "encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-socket/packages/globals"
|
||||
"go-socket/packages/minio"
|
||||
)
|
||||
|
||||
func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
||||
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 HandleAttachmentFileDownload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, false) {
|
||||
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