2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-30 02:59:59 +02:00
|
|
|
"bytes"
|
2015-09-25 06:58:38 +02:00
|
|
|
"encoding/json"
|
2015-09-28 22:02:03 +02:00
|
|
|
"errors"
|
2015-09-24 07:44:49 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
2015-09-25 18:47:55 +02:00
|
|
|
"strconv"
|
2015-09-24 07:44:49 +02:00
|
|
|
"strings"
|
|
|
|
|
2015-09-30 02:59:59 +02:00
|
|
|
"bitbucket.org/taruti/mimemagic"
|
2015-09-24 07:44:49 +02:00
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
// Describes metadata directly from the user request
|
2015-09-24 07:44:49 +02:00
|
|
|
type UploadRequest struct {
|
|
|
|
src io.Reader
|
|
|
|
filename string
|
2015-09-30 01:00:16 +02:00
|
|
|
expiry int64 // Seconds until expiry, 0 = never
|
2015-09-24 07:44:49 +02:00
|
|
|
randomBarename bool
|
2015-09-28 04:17:12 +02:00
|
|
|
deletionKey string // Empty string if not defined
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
// Metadata associated with a file as it would actually be stored
|
2015-09-24 07:44:49 +02:00
|
|
|
type Upload struct {
|
2015-09-28 04:17:12 +02:00
|
|
|
Filename string // Final filename on disk
|
|
|
|
Size int64
|
2015-09-30 01:00:16 +02:00
|
|
|
Expiry int64 // Unix timestamp of expiry, 0=never
|
2015-09-28 04:17:12 +02:00
|
|
|
DeleteKey string // Deletion key, one generated if not provided
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
upReq := UploadRequest{}
|
2015-09-28 04:17:12 +02:00
|
|
|
uploadHeaderProcess(r, &upReq)
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-30 06:56:51 +02:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
2015-10-01 05:37:00 +02:00
|
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
2015-09-25 06:58:38 +02:00
|
|
|
file, headers, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-25 06:58:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2015-09-28 06:25:57 +02:00
|
|
|
r.ParseForm()
|
|
|
|
if r.Form.Get("randomize") == "true" {
|
|
|
|
upReq.randomBarename = true
|
|
|
|
}
|
|
|
|
upReq.expiry = parseExpiry(r.Form.Get("expires"))
|
2015-09-25 06:58:38 +02:00
|
|
|
upReq.src = file
|
|
|
|
upReq.filename = headers.Filename
|
2015-09-30 06:56:51 +02:00
|
|
|
} 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
|
2015-09-25 06:58:38 +02:00
|
|
|
}
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
upload, err := processUpload(upReq)
|
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:58:38 +02:00
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
2015-09-28 06:25:57 +02:00
|
|
|
js := generateJSONresponse(upload)
|
2015-09-25 06:58:38 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.Write(js)
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/"+upload.Filename, 301)
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
upReq := UploadRequest{}
|
2015-09-28 04:17:12 +02:00
|
|
|
uploadHeaderProcess(r, &upReq)
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
defer r.Body.Close()
|
2015-09-25 18:00:14 +02:00
|
|
|
upReq.filename = c.URLParams["name"]
|
2015-09-24 07:44:49 +02:00
|
|
|
upReq.src = r.Body
|
|
|
|
|
|
|
|
upload, err := processUpload(upReq)
|
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-28 06:25:57 +02:00
|
|
|
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 {
|
|
|
|
fmt.Fprintf(w, Config.siteURL+upload.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
|
|
|
|
// For legacy reasons
|
|
|
|
if r.Header.Get("X-Randomized-Filename") == "yes" {
|
|
|
|
upReq.randomBarename = true
|
|
|
|
} else if r.Header.Get("X-Randomized-Barename") == "yes" {
|
|
|
|
upReq.randomBarename = true
|
|
|
|
}
|
|
|
|
|
|
|
|
upReq.deletionKey = r.Header.Get("X-Delete-Key")
|
|
|
|
|
|
|
|
// Get seconds until expiry. Non-integer responses never expire.
|
|
|
|
expStr := r.Header.Get("X-File-Expiry")
|
|
|
|
upReq.expiry = parseExpiry(expStr)
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
2015-09-28 04:17:12 +02:00
|
|
|
// Determine the appropriate filename, then write to disk
|
2015-09-24 07:44:49 +02:00
|
|
|
barename, extension := barePlusExt(upReq.filename)
|
|
|
|
|
|
|
|
if upReq.randomBarename || len(barename) == 0 {
|
|
|
|
barename = generateBarename()
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:03:41 +02:00
|
|
|
var header []byte
|
2015-09-24 07:44:49 +02:00
|
|
|
if len(extension) == 0 {
|
2015-10-01 17:03:41 +02:00
|
|
|
// Pull the first 512 bytes off for use in MIME detection
|
|
|
|
header = make([]byte, 512)
|
|
|
|
n, err := upReq.src.Read(header)
|
|
|
|
if n == 0 {
|
|
|
|
return upload, err
|
|
|
|
}
|
|
|
|
header = header[:n]
|
|
|
|
|
2015-09-30 02:59:59 +02:00
|
|
|
// Determine the type of file from header
|
|
|
|
mimetype := mimemagic.Match("", header)
|
|
|
|
|
|
|
|
// If the mime type is in our map, use that
|
|
|
|
// otherwise just use "ext"
|
|
|
|
if val, exists := mimeToExtension[mimetype]; exists {
|
|
|
|
extension = val
|
|
|
|
} else {
|
|
|
|
extension = "ext"
|
|
|
|
}
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
upload.Filename = strings.Join([]string{barename, extension}, ".")
|
|
|
|
|
2015-09-25 18:47:55 +02:00
|
|
|
_, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
|
|
|
|
fileexists := err == nil
|
|
|
|
for fileexists {
|
|
|
|
counter, err := strconv.Atoi(string(barename[len(barename)-1]))
|
|
|
|
if err != nil {
|
|
|
|
barename = barename + "1"
|
|
|
|
} else {
|
|
|
|
barename = barename[:len(barename)-1] + strconv.Itoa(counter+1)
|
|
|
|
}
|
|
|
|
upload.Filename = strings.Join([]string{barename, extension}, ".")
|
|
|
|
|
|
|
|
_, err = os.Stat(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
fileexists = err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
|
2015-09-24 14:30:16 +02:00
|
|
|
if err != nil {
|
2015-09-24 07:44:49 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-24 14:30:16 +02:00
|
|
|
defer dst.Close()
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
// Get the rest of the metadata needed for storage
|
|
|
|
upload.Expiry = getFutureTimestamp(upReq.expiry)
|
|
|
|
|
|
|
|
// If no delete key specified, pick a random one.
|
|
|
|
if upReq.deletionKey == "" {
|
2015-09-28 18:30:21 +02:00
|
|
|
upload.DeleteKey = randomString(30)
|
2015-09-28 04:17:12 +02:00
|
|
|
} else {
|
|
|
|
upload.DeleteKey = upReq.deletionKey
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataWrite(upload.Filename, &upload)
|
|
|
|
|
2015-10-01 17:03:41 +02:00
|
|
|
bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src))
|
2015-09-24 14:30:16 +02:00
|
|
|
if err != nil {
|
2015-09-24 07:44:49 +02:00
|
|
|
return
|
|
|
|
} else if bytes == 0 {
|
2015-09-28 22:02:03 +02:00
|
|
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
|
|
|
return upload, errors.New("Empty file")
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
upload.Size = bytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBarename() string {
|
2015-09-28 18:30:21 +02:00
|
|
|
return randomString(8)
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 06:25:57 +02:00
|
|
|
func generateJSONresponse(upload Upload) []byte {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
|
|
|
"url": Config.siteURL + upload.Filename,
|
|
|
|
"filename": upload.Filename,
|
|
|
|
"delete_key": upload.DeleteKey,
|
|
|
|
"expiry": strconv.FormatInt(int64(upload.Expiry), 10),
|
|
|
|
"size": strconv.FormatInt(upload.Size, 10),
|
|
|
|
})
|
|
|
|
|
|
|
|
return js
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:04:06 +02:00
|
|
|
var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
func barePlusExt(filename string) (barename, extension string) {
|
|
|
|
|
|
|
|
filename = strings.TrimSpace(filename)
|
|
|
|
filename = strings.ToLower(filename)
|
|
|
|
|
|
|
|
extension = path.Ext(filename)
|
|
|
|
barename = filename[:len(filename)-len(extension)]
|
|
|
|
|
2015-09-25 15:04:06 +02:00
|
|
|
extension = barePlusRe.ReplaceAllString(extension, "")
|
|
|
|
barename = barePlusRe.ReplaceAllString(barename, "")
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-09-28 06:25:57 +02:00
|
|
|
|
2015-09-30 01:00:16 +02:00
|
|
|
func parseExpiry(expStr string) int64 {
|
2015-09-28 06:25:57 +02:00
|
|
|
if expStr == "" {
|
|
|
|
return 0
|
|
|
|
} else {
|
2015-09-30 01:00:16 +02:00
|
|
|
expiry, err := strconv.ParseInt(expStr, 10, 64)
|
2015-09-28 06:25:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
2015-09-30 01:00:16 +02:00
|
|
|
return int64(expiry)
|
2015-09-28 06:25:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|