Support remote uploads

This commit is contained in:
andreimarcu 2015-10-01 20:58:08 -04:00
parent 9cd20c81fc
commit 8f7b47f572
3 changed files with 52 additions and 8 deletions

View File

@ -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

View File

@ -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()

View File

@ -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" {