Browse Source

Added pasteHandler and paste upload

pull/16/head
andreimarcu 9 years ago
parent
commit
2f5bf2cd65
  1. 7
      pages.go
  2. 2
      server.go
  3. 1
      templates.go
  4. 25
      upload.go

7
pages.go

@ -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) {
w.WriteHeader(404)
err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w)

2
server.go

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

1
templates.go

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

25
upload.go

@ -38,7 +38,9 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq := UploadRequest{}
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" {
upReq.randomBarename = true
}
@ -48,7 +50,9 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq.src = r.Body
upReq.filename = r.URL.Query().Get("qqfile")
} else {
} else if strings.HasPrefix(contentType, "multipart/form-data") {
return
file, headers, err := r.FormFile("file")
if err != nil {
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.src = file
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)
@ -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
header := make([]byte, 512)
_, err = upReq.src.Read(header)
if err != nil {
n, err := upReq.src.Read(header)
if n == 0 || err != nil {
return
}

Loading…
Cancel
Save