From 65f6db2bf9a412235f1a84649828d6e4f87bc891 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Fri, 24 Jun 2016 11:46:50 +1000 Subject: [PATCH] Add download handler --- main.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 54cb4d4..48c0710 100644 --- a/main.go +++ b/main.go @@ -256,6 +256,13 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) { func cloneHandler(w http.ResponseWriter, r *http.Request) { } func downloadHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + paste := vars["pasteId"] + s := getPaste(paste, "") + w.Header().Set("Content-Disposition", "attachment; filename="+paste) + w.Header().Set("Content-Type", r.Header.Get("Content-Type")) + io.Copy(w, s) + } func rawHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -270,8 +277,8 @@ func main() { router.HandleFunc("/p/{pasteId}", pasteHandler) router.HandleFunc("/raw/{pasteId}", rawHandler) router.HandleFunc("/p/{pasteId}/{lang}", pasteHandler) - router.HandleFunc("/{clone}/{pasteId}", cloneHandler) - router.HandleFunc("/{download}/{pasteId}", downloadHandler) + router.HandleFunc("/clone/{pasteId}", cloneHandler) + router.HandleFunc("/download/{pasteId}", downloadHandler) router.HandleFunc("/save", saveHandler) router.HandleFunc("/save/{output}", saveHandler) router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)