package main import ( "encoding/json" "io/ioutil" "net/http" "os" "path" "path/filepath" "strconv" "github.com/dustin/go-humanize" ) func uploaderPost(w http.ResponseWriter, r *http.Request) { /* read 32Mb at a time */ r.ParseMultipartForm(32 << 20) links := []string{} for _, h := range r.MultipartForm.File["file"] { if h.Size > conf.maxsize { http.Error(w, "File is too big", http.StatusRequestEntityTooLarge) return } post, err := h.Open() if err != nil { http.Error(w, "Internal error", http.StatusInternalServerError) return } defer post.Close() tmp, _ := ioutil.TempFile(conf.filepath, "*"+path.Ext(h.Filename)) f, err := os.Create(tmp.Name()) if err != nil { http.Error(w, "Internal error", http.StatusInternalServerError) return } defer f.Close() if err = writefile(f, post, h.Size); err != nil { http.Error(w, "Internal error", http.StatusInternalServerError) defer os.Remove(tmp.Name()) return } expiry, err := strconv.Atoi(r.PostFormValue("expiry")) if err != nil || expiry < 0 { expiry = int(conf.expiry) } writemeta(tmp.Name(), int64(expiry)) link := conf.baseuri + conf.filectx + filepath.Base(tmp.Name()) links = append(links, link) } switch r.PostFormValue("output") { case "html": data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize)), Links: links, } servetemplate(w, "/index.html", data) case "json": data, _ := json.Marshal(links) w.Write(data) default: for _, link := range links { w.Write([]byte(link + "\r\n")) } } }