add helper function for handling existing file in s3
This commit is contained in:
+31
-11
@@ -2,14 +2,17 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"go-socket/packages/convertions"
|
||||||
"go-socket/packages/globals"
|
"go-socket/packages/globals"
|
||||||
"go-socket/packages/minio"
|
"go-socket/packages/minio"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"go-socket/packages/convertions"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func handleExistingUpload() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
||||||
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
if !postValidCheckWithResponseOnFail(&response, request, true) {
|
||||||
return
|
return
|
||||||
@@ -36,14 +39,18 @@ func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
|||||||
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithFileBytes))
|
request.Body = http.MaxBytesReader(response, request.Body, int64(globals.MaxPostWithFileBytes))
|
||||||
multipartReader, err := request.MultipartReader()
|
multipartReader, err := request.MultipartReader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "expected multipart form", 400)
|
http.Error(response, "expected multipart form", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uploaded := false
|
||||||
for {
|
for {
|
||||||
part, err := multipartReader.NextPart()
|
part, err := multipartReader.NextPart()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
http.Error(response, "bad request", 400)
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "bad request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,22 +59,35 @@ func HandleFileUpload(response http.ResponseWriter, request *http.Request) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(part.FileName()) != sha256.Size {
|
if len(part.FileName()) != sha256.Size*2 {
|
||||||
part.Close()
|
part.Close()
|
||||||
http.Error(response, "filename must be sha256 of whole file", 400)
|
http.Error(response, "filename must be hex-encoded sha256 of whole file", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = minio.Upload(ctx, part.FileName(), part, -1, part.Header.Get("Content-Type"), user.Id)
|
contentType := part.Header.Get("Content-Type")
|
||||||
|
key := minio.GetKey(part.FileName(), contentType)
|
||||||
|
|
||||||
|
if minio.DoesExist(ctx, key) {
|
||||||
|
part.Close()
|
||||||
|
uploaded = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = minio.Upload(ctx, key, part, -1, contentType, user.Id)
|
||||||
|
part.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "upload failed", http.StatusInternalServerError)
|
http.Error(response, "upload failed", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
part.Close()
|
|
||||||
|
|
||||||
if err != nil {
|
uploaded = true
|
||||||
http.Error(response, "upload failed", 500)
|
}
|
||||||
|
|
||||||
|
if !uploaded {
|
||||||
|
http.Error(response, "no file in request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
response.WriteHeader(http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func extensionFromContentType(ct string) string {
|
|||||||
return exts[0]
|
return exts[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func getKey(hash string, contentType string) string {
|
func GetKey(hash string, contentType string) string {
|
||||||
return hash + extensionFromContentType(contentType)
|
return hash + extensionFromContentType(contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,15 +78,12 @@ func Upload(ctx context.Context, key string, body io.Reader, size int64, content
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDownloadUrl(ctx context.Context, key string) (*url.URL, error) {
|
func GetDownloadUrl(ctx context.Context, key string) (*url.URL, error) {
|
||||||
u, err := minClient.PresignedGetObject(ctx, globals.FileStorageBucketName, key, globals.FileDownloadLinkTtl, nil)
|
u, err := minClient.PresignedGetObject(ctx, globals.FileStorageBucketName, key, globals.FileDownloadLinkTtl, nil)
|
||||||
return u, err
|
return u, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoesExist(ctx context.Context, key string) bool {
|
func DoesExist(ctx context.Context, key string) bool {
|
||||||
_, err := minClient.GetObject(ctx, globals.FileStorageBucketName, key, minio.GetObjectOptions{})
|
_, err := minClient.StatObject(ctx, globals.FileStorageBucketName, key, minio.StatObjectOptions{})
|
||||||
if err != nil {
|
return err == nil
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user