101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go-socket/packages/convertions"
|
|
"go-socket/packages/globals"
|
|
"go-socket/packages/minio"
|
|
)
|
|
|
|
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 := minio.GetKey(connectionId, contentType)
|
|
|
|
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 HandleFileDownload(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
|
|
}
|
|
|
|
connectionId, err := convertions.ConvertStringUuid(request.FormValue("connectionid"))
|
|
if err != nil {
|
|
http.Error(response, "invalid connectionid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if _, ok := user.Connections[connectionId]; !ok {
|
|
http.Error(response, "no such connection", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
key := request.FormValue("key")
|
|
if !strings.HasPrefix(key, connectionId.String()+"/") {
|
|
http.Error(response, "no such file", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
url, err := minio.GetDownloadUrl(ctx, key)
|
|
if err != nil {
|
|
http.Error(response, "no such file", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
response.Write([]byte(url.String()))
|
|
}
|