From fce3b466e63fea00cde2818d7004d000e0bc7686 Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Wed, 25 Oct 2023 11:57:22 +0200 Subject: [PATCH] Allow upload ID to contain slashes See https://community.transloadit.com/t/tusd-azure-changefileinfo-id-with-folder-support/16871/1 --- pkg/handler/handler.go | 32 +++++++++++++++++--------------- pkg/handler/unrouted_handler.go | 4 ++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go index c5030a672..89ce5eb35 100644 --- a/pkg/handler/handler.go +++ b/pkg/handler/handler.go @@ -2,8 +2,6 @@ package handler import ( "net/http" - - "github.com/bmizerany/pat" ) // Handler is a ready to use handler with routing (using pat) @@ -33,21 +31,25 @@ func NewHandler(config Config) (*Handler, error) { UnroutedHandler: handler, } - mux := pat.New() + mux := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == "POST" && r.URL.Path == "": + handler.PostFile(w, r) + case r.Method == "HEAD" && r.URL.Path != "": + handler.HeadFile(w, r) + case r.Method == "PATCH" && r.URL.Path != "": + handler.PatchFile(w, r) + case r.Method == "GET" && r.URL.Path != "" && !config.DisableDownload: + handler.GetFile(w, r) + case r.Method == "DELETE" && r.URL.Path != "" && config.StoreComposer.UsesTerminater && !config.DisableTermination: + handler.DelFile(w, r) + default: + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`combination of path and method are not recognized`)) + } + }) routedHandler.Handler = handler.Middleware(mux) - mux.Post("", http.HandlerFunc(handler.PostFile)) - mux.Head(":id", http.HandlerFunc(handler.HeadFile)) - mux.Add("PATCH", ":id", http.HandlerFunc(handler.PatchFile)) - if !config.DisableDownload { - mux.Get(":id", http.HandlerFunc(handler.GetFile)) - } - - // Only attach the DELETE handler if the Terminate() method is provided - if config.StoreComposer.UsesTerminater && !config.DisableTermination { - mux.Del(":id", http.HandlerFunc(handler.DelFile)) - } - return routedHandler, nil } diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index 969c3e397..9b053a390 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "errors" + "fmt" "io" "math" "mime" @@ -1440,6 +1441,9 @@ func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads [] // extractIDFromPath pulls the last segment from the url provided func extractIDFromPath(url string) (string, error) { + return url, nil + + fmt.Println(">>>>>>>", url) result := reExtractFileID.FindStringSubmatch(url) if len(result) != 2 { return "", ErrNotFound