Added pasteHandler and paste upload

This commit is contained in:
andreimarcu 2015-09-30 00:56:51 -04:00
parent fd75607652
commit 2f5bf2cd65
4 changed files with 31 additions and 4 deletions

View File

@ -14,6 +14,13 @@ func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
} }
} }
func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := Templates["paste.html"].ExecuteWriter(pongo2.Context{}, w)
if err != nil {
oopsHandler(c, w, r)
}
}
func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) { func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w) err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w)

View File

@ -72,6 +72,8 @@ func setup() {
torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`) torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
goji.Get("/", indexHandler) goji.Get("/", indexHandler)
goji.Get("/paste/", pasteHandler)
goji.Get("/paste", http.RedirectHandler("/paste/", 301))
goji.Post("/upload", uploadPostHandler) goji.Post("/upload", uploadPostHandler)
goji.Post("/upload/", uploadPostHandler) goji.Post("/upload/", uploadPostHandler)

View File

@ -44,6 +44,7 @@ func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Temp
templates := [...]string{ templates := [...]string{
"index.html", "index.html",
"paste.html",
"404.html", "404.html",
"401.html", "401.html",
"oops.html", "oops.html",

View File

@ -38,7 +38,9 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq := UploadRequest{} upReq := UploadRequest{}
uploadHeaderProcess(r, &upReq) uploadHeaderProcess(r, &upReq)
if r.Header.Get("Content-Type") == "application/octet-stream" { contentType := r.Header.Get("Content-Type")
if contentType == "application/octet-stream" {
if r.URL.Query().Get("randomize") == "true" { if r.URL.Query().Get("randomize") == "true" {
upReq.randomBarename = true upReq.randomBarename = true
} }
@ -48,7 +50,9 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq.src = r.Body upReq.src = r.Body
upReq.filename = r.URL.Query().Get("qqfile") upReq.filename = r.URL.Query().Get("qqfile")
} else { } else if strings.HasPrefix(contentType, "multipart/form-data") {
return
file, headers, err := r.FormFile("file") file, headers, err := r.FormFile("file")
if err != nil { if err != nil {
oopsHandler(c, w, r) oopsHandler(c, w, r)
@ -63,6 +67,19 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq.expiry = parseExpiry(r.Form.Get("expires")) upReq.expiry = parseExpiry(r.Form.Get("expires"))
upReq.src = file upReq.src = file
upReq.filename = headers.Filename upReq.filename = headers.Filename
} else {
if r.FormValue("content") == "" {
oopsHandler(c, w, r)
return
}
extension := r.FormValue("extension")
if extension == "" {
extension = "txt"
}
upReq.src = strings.NewReader(r.FormValue("content"))
upReq.expiry = parseExpiry(r.FormValue("expires"))
upReq.filename = r.FormValue("filename") + "." + extension
} }
upload, err := processUpload(upReq) upload, err := processUpload(upReq)
@ -126,8 +143,8 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
// Pull the first 512 bytes off for use in MIME detection if needed // Pull the first 512 bytes off for use in MIME detection if needed
header := make([]byte, 512) header := make([]byte, 512)
_, err = upReq.src.Read(header) n, err := upReq.src.Read(header)
if err != nil { if n == 0 || err != nil {
return return
} }