Skip to content

Commit

Permalink
feat: Upgrade versions, introduce Uppy batch signing
Browse files Browse the repository at this point in the history
  • Loading branch information
serverwentdown committed Feb 16, 2022
1 parent b5dacbc commit 0d78f1d
Show file tree
Hide file tree
Showing 6 changed files with 820 additions and 1,483 deletions.
55 changes: 55 additions & 0 deletions handlers-s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,61 @@ func handleGetUploadedParts(w http.ResponseWriter, req *http.Request) {
encoder.Encode(getUploadedPartsRes(parts))
}

/* batchSignPartsUpload */

type batchSignPartsUploadRes struct {
PresignedURLs map[string]string `json:"presignedUrls"`
}

func handleBatchSignPartsUpload(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
cred, err := getCredential(vars["id"])
if err != nil {
errorResponse(w, req, err)
return
}

uploadID := vars["uploadID"]
key := req.URL.Query().Get("key")
partNumbers := req.URL.Query().Get("partNumbers")

if uploadID == "" || key == "" || partNumbers == "" {
errorResponse(w, req, fmt.Errorf("%w", errBadRequest))
return
}

partNumbersArray := strings.Split(partNumbers, ",")
partNumbersParsed := make([]uint16, 0, len(partNumbersArray))
for _, partNumber := range partNumbersArray {
n, err := strconv.ParseUint(partNumber, 10, 16)
if n < 1 || n > 10000 || err != nil {
errorResponse(w, req, fmt.Errorf("%w: invalid part number", errBadRequest))
return
}
partNumbersParsed = append(partNumbersParsed, uint16(n))
}

presignedURLs := make(map[string]string, len(partNumbersParsed))
for _, partNumber := range partNumbersParsed {
params := make(url.Values)
params.Add("partNumber", strconv.FormatUint(uint64(partNumber), 10))
params.Add("uploadId", uploadID)
unsignedReq, err := http.NewRequest(http.MethodPut, cred.Endpoint+"/"+key+"?"+params.Encode(), nil)
if err != nil {
errorResponse(w, req, fmt.Errorf("%w: %s", errInternalServerError, err))
return
}

signedReq := preSign(unsignedReq, cred)
presignedURLs[strconv.FormatUint(uint64(partNumber), 10)] = signedReq.URL.String()
}

encoder := json.NewEncoder(w)
encoder.Encode(batchSignPartsUploadRes{
PresignedURLs: presignedURLs,
})
}

/* signPartUpload */

type signPartUploadRes struct {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {

s3Router.Methods(http.MethodPost).Path("").HandlerFunc(handleCreateMultipartUpload)
s3Router.Methods(http.MethodGet).Path("/{uploadID}").HandlerFunc(handleGetUploadedParts)
s3Router.Methods(http.MethodGet).Path("/{uploadID}/batch").HandlerFunc(handleBatchSignPartsUpload)
s3Router.Methods(http.MethodGet).Path("/{uploadID}/{uploadPart}").HandlerFunc(handleSignPartUpload)
s3Router.Methods(http.MethodPost).Path("/{uploadID}/complete").HandlerFunc(handleCompleteMultipartUpload)
s3Router.Methods(http.MethodDelete).Path("/{uploadID}").HandlerFunc(handleAbortMultipartUpload)
Expand Down
Loading

0 comments on commit 0d78f1d

Please sign in to comment.