64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package http
|
|
|
|
import (
|
|
"go-socket/packages/convertions"
|
|
"go-socket/packages/globals"
|
|
"go-socket/packages/minio"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func HandleFileUpload(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
|
|
}
|
|
|
|
connectionId, err := convertions.ConvertStringUuid(request.FormValue("connectionid"))
|
|
if err != nil {
|
|
http.Error(response, "invalid connectionid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
_, ok := user.Connections[connectionId]
|
|
if !ok {
|
|
http.Error(response, "no such connection", http.StatusUnauthorized)
|
|
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 :=
|
|
|
|
if err = minio.Upload(ctx, fileId.String(), file, header.Size, contentType, map[string]string{
|
|
"orginalName": header.Filename,
|
|
"uploaderId": user.Id.String(),
|
|
}); err != nil {
|
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response.WriteHeader(http.StatusAccepted)
|
|
response.Write([]byte(fileId.String()))
|
|
}
|