idk
This commit is contained in:
+18
-57
@@ -1,21 +1,14 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/globals"
|
||||
"go-socket/packages/minio"
|
||||
"go-socket/packages/postgresql"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func handleExistingUpload() {
|
||||
|
||||
}
|
||||
|
||||
func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
||||
return
|
||||
@@ -28,6 +21,13 @@ func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
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)
|
||||
@@ -39,64 +39,25 @@ func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithFileBytes))
|
||||
multipartReader, err := request.MultipartReader()
|
||||
file, header, err := request.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(response, "expected multipart form", http.StatusBadRequest)
|
||||
http.Error(response, "missing file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
uploaded := false
|
||||
for {
|
||||
part, err := multipartReader.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(response, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
|
||||
if part.FileName() == "" {
|
||||
part.Close()
|
||||
continue
|
||||
}
|
||||
key :=
|
||||
|
||||
if len(part.FileName()) != sha256.Size*2 {
|
||||
part.Close()
|
||||
http.Error(response, "filename must be hex-encoded sha256 of whole file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
contentType := part.Header.Get("Content-Type")
|
||||
key := minio.GetKey(part.FileName(), contentType)
|
||||
|
||||
if minio.DoesExist(ctx, key) {
|
||||
part.Close()
|
||||
uploaded = true
|
||||
handleExistingUpload()
|
||||
continue
|
||||
}
|
||||
|
||||
id := uuid.New()
|
||||
err = minio.Upload(ctx, key, part, -1, contentType, id)
|
||||
|
||||
postgresql.FileMetadataSave()
|
||||
|
||||
part.Close()
|
||||
|
||||
if err != nil {
|
||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
uploaded = true
|
||||
}
|
||||
|
||||
if !uploaded {
|
||||
http.Error(response, "no file in request", http.StatusBadRequest)
|
||||
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()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user