From 8f7b47f57230e0ca0df5c818cb4f1b685a98d0e2 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Thu, 1 Oct 2015 20:58:08 -0400 Subject: [PATCH] Support remote uploads --- README.md | 1 + server.go | 24 ++++++++++++++++-------- upload.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7f676db..96a1196 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Command-line options - ```-siteurl "http://mylinx.example.org/"``` -- the site url (for generating links) - ```-filespath files/"``` -- Path to store uploads (default is files/) - ```-metapath meta/``` -- Path to store information about uploads (default is meta/) +- ```-remoteuploads``` -- (optionally) enable remote uploads (/upload?url=https://...) - ```-fastcgi``` -- (optionally) serve through fastcgi - ```-nologs``` -- (optionally) disable request logs in stdout diff --git a/server.go b/server.go index 00bbbb9..7fac1b8 100644 --- a/server.go +++ b/server.go @@ -19,14 +19,15 @@ import ( ) var Config struct { - bind string - filesDir string - metaDir string - noLogs bool - allowHotlink bool - siteName string - siteURL string - fastcgi bool + bind string + filesDir string + metaDir string + noLogs bool + allowHotlink bool + siteName string + siteURL string + fastcgi bool + remoteUploads bool } var Templates = make(map[string]*pongo2.Template) @@ -86,6 +87,11 @@ func setup() { goji.Get("/paste/", pasteHandler) goji.Get("/paste", http.RedirectHandler("/paste/", 301)) + if Config.remoteUploads { + goji.Get("/upload", uploadRemote) + goji.Get("/upload/", uploadRemote) + } + goji.Post("/upload", uploadPostHandler) goji.Post("/upload/", uploadPostHandler) goji.Put("/upload", uploadPutHandler) @@ -117,6 +123,8 @@ func main() { "site base url (including trailing slash)") flag.BoolVar(&Config.fastcgi, "fastcgi", false, "serve through fastcgi") + flag.BoolVar(&Config.remoteUploads, "remoteuploads", false, + "enable remote uploads") flag.Parse() setup() diff --git a/upload.go b/upload.go index ca1dcf6..a49812a 100644 --- a/upload.go +++ b/upload.go @@ -7,8 +7,10 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "path" + "path/filepath" "regexp" "strconv" "strings" @@ -109,6 +111,39 @@ func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { } } +func uploadRemote(c web.C, w http.ResponseWriter, r *http.Request) { + if r.FormValue("url") == "" { + http.Redirect(w, r, "/", 301) + return + } + + upReq := UploadRequest{} + grabUrl, _ := url.Parse(r.FormValue("url")) + + resp, err := http.Get(grabUrl.String()) + if err != nil { + oopsHandler(c, w, r) + return + } + + upReq.filename = filepath.Base(grabUrl.Path) + upReq.src = resp.Body + + upload, err := processUpload(upReq) + if err != nil { + oopsHandler(c, w, r) + return + } + + if strings.EqualFold("application/json", r.Header.Get("Accept")) { + js := generateJSONresponse(upload) + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.Write(js) + } else { + http.Redirect(w, r, "/"+upload.Filename, 301) + } +} + func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) { // For legacy reasons if r.Header.Get("X-Randomized-Filename") == "yes" {