Skip to content

Commit

Permalink
Replace pat router with own logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Jan 25, 2024
1 parent 496da4b commit 5dc130d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
50 changes: 36 additions & 14 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package handler

import (
"net/http"
"strings"
)

// Handler is a ready to use handler with routing (using pat)
// Handler is a ready to use handler with routing
type Handler struct {
*UnroutedHandler
http.Handler
Expand Down Expand Up @@ -32,20 +33,41 @@ func NewHandler(config Config) (*Handler, error) {
}

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)
method := r.Method
path := strings.Trim(r.URL.Path, "/")

switch path {
case "":
// Root endpoint for upload creation
switch method {
case "POST":
handler.PostFile(w, r)
default:
w.Header().Add("Allow", "POST")
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(`method not allowed`))
}
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`combination of path and method are not recognized`))
// URL points to an upload resource
switch {
case method == "HEAD" && r.URL.Path != "":
// Offset retrieval
handler.HeadFile(w, r)
case method == "PATCH" && r.URL.Path != "":
// Upload apppending
handler.PatchFile(w, r)
case method == "GET" && r.URL.Path != "" && !config.DisableDownload:
// Upload download
handler.GetFile(w, r)
case method == "DELETE" && r.URL.Path != "" && config.StoreComposer.UsesTerminater && !config.DisableTermination:
// Upload termination
handler.DelFile(w, r)
default:
// TODO: Only add GET and DELETE if they are supported
w.Header().Add("Allow", "GET, HEAD, PATCH, DELETE")
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(`method not allowed`))
}
}
})

Expand Down
30 changes: 19 additions & 11 deletions pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"mime"
Expand Down Expand Up @@ -277,7 +276,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
}

// Parse Upload-Concat header
isPartial, isFinal, partialUploadIDs, err := parseConcat(concatHeader)
isPartial, isFinal, partialUploadIDs, err := parseConcat(concatHeader, handler.basePath)
if err != nil {
handler.sendError(c, err)
return
Expand Down Expand Up @@ -1399,7 +1398,7 @@ func SerializeMetadataHeader(meta map[string]string) string {
// Parse the Upload-Concat header, e.g.
// Upload-Concat: partial
// Upload-Concat: final;http://tus.io/files/a /files/b/
func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []string, err error) {
func parseConcat(header string, basePath string) (isPartial bool, isFinal bool, partialUploads []string, err error) {
if len(header) == 0 {
return
}
Expand All @@ -1420,7 +1419,7 @@ func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []
continue
}

id, extractErr := extractIDFromPath(value)
id, extractErr := extractIDFromURL(value, basePath)
if extractErr != nil {
err = extractErr
return
Expand All @@ -1439,16 +1438,25 @@ func parseConcat(header string) (isPartial bool, isFinal bool, partialUploads []
return
}

// extractIDFromPath pulls the last segment from the url provided
func extractIDFromPath(url string) (string, error) {
return url, nil
// extractIDFromPath extracts the upload ID from a path, which has already
// been stripped of the base path (done by the user). Effectively, we only
// remove leading and trailing slashes.
func extractIDFromPath(path string) (string, error) {
return strings.Trim(path, "/"), nil
}

fmt.Println(">>>>>>>", url)
result := reExtractFileID.FindStringSubmatch(url)
if len(result) != 2 {
// extractIDFromURL extracts the upload ID from a full URL or a full path
// (including the base path). For example:
//
// https://example.com/files/1234/5678 -> 1234/5678
// /files/1234/5678 -> 1234/5678
func extractIDFromURL(url string, basePath string) (string, error) {
_, id, ok := strings.Cut(url, basePath)
if !ok {
return "", ErrNotFound
}
return result[1], nil

return extractIDFromPath(id)
}

// getRequestId returns the value of the X-Request-ID header, if available,
Expand Down

0 comments on commit 5dc130d

Please sign in to comment.