From 728b5d9d4b932fa0b96ada2bcc7767263979f06a Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Sun, 15 Dec 2019 12:19:32 +0530 Subject: [PATCH] Add extension to FileUpload request url Fixes #27 --- .../txt/newFileUploadPasteSuccess.txt.tmpl | 4 ++-- handlers.go | 6 +++++- router.go | 20 +++++++++++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl b/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl index dba998e..4b02bbc 100644 --- a/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl +++ b/assets/templates/txt/newFileUploadPasteSuccess.txt.tmpl @@ -1,5 +1,5 @@ {{if .Request.PostForm.deleteToken -}} -https://{{.Request.Host}}/{{.Paste.Key}}?deleteToken={{.Paste.DeleteToken | urlquery}} +https://{{.Request.Host}}/{{.Paste.Key}}{{.FileExt}}?deleteToken={{.Paste.DeleteToken | urlquery}} {{else -}} -https://{{.Request.Host}}/{{.Paste.Key}} +https://{{.Request.Host}}/{{.Paste.Key}}{{.FileExt}} {{end -}} diff --git a/handlers.go b/handlers.go index c6fcece..06117ab 100644 --- a/handlers.go +++ b/handlers.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "path/filepath" "time" "gitea.hashru.nl/dsprenkels/rushlink/internal/db" @@ -209,7 +210,10 @@ func (rl *rushlink) newFileUploadPasteHandler(w http.ResponseWriter, r *http.Req }); err != nil { panic(err) } - data := map[string]interface{}{"Paste": paste} + data := map[string]interface{}{ + "Paste": paste, + "FileUpload": fu, + "FileExt": filepath.Ext(fu.FileName)} render(w, r, "newFileUploadPasteSuccess", data) } diff --git a/router.go b/router.go index bc93633..b18907f 100644 --- a/router.go +++ b/router.go @@ -12,6 +12,9 @@ import ( "github.com/gorilla/mux" ) +const urlKeyExpr = "{key:[A-Za-z0-9-_]{4,}}" +const urlKeyWithExtExpr = urlKeyExpr + "{ext:\\.[A-Za-z0-9-_]+}" + type rushlink struct { db *db.Database fs *db.FileStore @@ -79,14 +82,19 @@ func StartMainServer(addr string, db *db.Database, fs *db.FileStore) { router := mux.NewRouter() router.Use(recoveryMiddleware) router.Use(metricsMiddleware) + router.HandleFunc("/uploads/{id:[A-Za-z0-9-_]+}/{filename:.+}", rl.uploadFileGetHandler).Methods("GET", "HEAD") router.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD") router.HandleFunc("/", rl.newPasteHandler).Methods("POST") - router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.viewPasteHandler).Methods("GET", "HEAD") - router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") - router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") - router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.deletePasteHandler).Methods("DELETE") - router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/delete", rl.deletePasteHandler).Methods("POST") - router.HandleFunc("/uploads/{id:[A-Za-z0-9-_]+}/{filename:.+}", rl.uploadFileGetHandler).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") + router.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE") + router.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE") + router.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST") + router.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST") srv := &http.Server{ Handler: router,