From 7cbeb02662b85e37e7f1322f4df5c234e784af9a Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Thu, 5 Nov 2015 17:02:38 -0700 Subject: [PATCH] Extract id from url instead of query params Now that the handler can be used directly we cannot rely on the pat router for the id. This also makes it consistent with how ids are extracted from urls elsewhere --- handler.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/handler.go b/handler.go index c765278..7ebf7d2 100644 --- a/handler.go +++ b/handler.go @@ -298,7 +298,7 @@ func (handler *Handler) PatchFile(w http.ResponseWriter, r *http.Request) { return } - id := r.URL.Query().Get(":id") + id := extractIDFromPath(r.URL.Path) // Ensure file is not locked if _, ok := handler.locks[id]; ok { @@ -370,7 +370,7 @@ func (handler *Handler) PatchFile(w http.ResponseWriter, r *http.Request) { // GetFile handles requests to download a file using a GET request. This is not // part of the specification. func (handler *Handler) GetFile(w http.ResponseWriter, r *http.Request) { - id := r.URL.Query().Get(":id") + id := extractIDFromPath(r.URL.Path) // Ensure file is not locked if _, ok := handler.locks[id]; ok { @@ -417,7 +417,7 @@ func (handler *Handler) GetFile(w http.ResponseWriter, r *http.Request) { // DelFile terminates an upload permanently. func (handler *Handler) DelFile(w http.ResponseWriter, r *http.Request) { - id := r.URL.Query().Get(":id") + id := extractIDFromPath(r.URL.Path) // Ensure file is not locked if _, ok := handler.locks[id]; ok { @@ -594,14 +594,13 @@ func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads [] continue } - // Extract ids out of URL - result := reExtractFileID.FindStringSubmatch(value) - if len(result) != 2 { + id := extractIDFromPath(value) + if id == "" { err = ErrInvalidConcat return } - partialUploads = append(partialUploads, result[1]) + partialUploads = append(partialUploads, id) } } @@ -613,3 +612,12 @@ func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads [] return } + +// extractIDFromPath pulls the last segment from the url provided +func extractIDFromPath(url string) string { + result := reExtractFileID.FindStringSubmatch(url) + if len(result) != 2 { + return "" + } + return result[1] +}