2015-09-24 01:44:49 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-29 20:59:59 -04:00
|
|
|
"bytes"
|
2015-09-25 00:58:38 -04:00
|
|
|
"encoding/json"
|
2015-09-28 16:02:03 -04:00
|
|
|
"errors"
|
2015-09-24 01:44:49 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2015-10-01 20:58:08 -04:00
|
|
|
"net/url"
|
2015-09-24 01:44:49 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-10-01 20:58:08 -04:00
|
|
|
"path/filepath"
|
2015-09-24 01:44:49 -04:00
|
|
|
"regexp"
|
2015-09-25 12:47:55 -04:00
|
|
|
"strconv"
|
2015-09-24 01:44:49 -04:00
|
|
|
"strings"
|
2015-10-06 02:50:20 -04:00
|
|
|
"time"
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-29 20:59:59 -04:00
|
|
|
"bitbucket.org/taruti/mimemagic"
|
2015-10-02 01:09:40 -04:00
|
|
|
"github.com/dchest/uniuri"
|
2015-09-24 01:44:49 -04:00
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-10-04 21:12:19 -04:00
|
|
|
var fileBlacklist = map[string]bool{
|
|
|
|
"favicon.ico": true,
|
|
|
|
"index.htm": true,
|
|
|
|
"index.html": true,
|
|
|
|
"index.php": true,
|
|
|
|
"robots.txt": true,
|
|
|
|
}
|
|
|
|
|
2015-09-27 22:17:12 -04:00
|
|
|
// Describes metadata directly from the user request
|
2015-09-24 01:44:49 -04:00
|
|
|
type UploadRequest struct {
|
|
|
|
src io.Reader
|
|
|
|
filename string
|
2015-10-06 02:50:20 -04:00
|
|
|
expiry time.Duration // Seconds until expiry, 0 = never
|
2015-09-24 01:44:49 -04:00
|
|
|
randomBarename bool
|
2015-09-27 22:17:12 -04:00
|
|
|
deletionKey string // Empty string if not defined
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
2015-09-27 22:17:12 -04:00
|
|
|
// Metadata associated with a file as it would actually be stored
|
2015-09-24 01:44:49 -04:00
|
|
|
type Upload struct {
|
2015-10-07 22:45:34 -04:00
|
|
|
Filename string // Final filename on disk
|
|
|
|
Metadata Metadata
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
upReq := UploadRequest{}
|
2015-09-27 22:17:12 -04:00
|
|
|
uploadHeaderProcess(r, &upReq)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-30 00:56:51 -04:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
2015-09-30 23:37:00 -04:00
|
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
2015-09-25 00:58:38 -04:00
|
|
|
file, headers, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2015-10-04 12:47:20 -04:00
|
|
|
oopsHandler(c, w, r, RespHTML, "Could not upload file.")
|
2015-09-25 00:58:38 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
r.ParseForm()
|
|
|
|
if r.Form.Get("randomize") == "true" {
|
|
|
|
upReq.randomBarename = true
|
|
|
|
}
|
|
|
|
upReq.expiry = parseExpiry(r.Form.Get("expires"))
|
2015-09-25 00:58:38 -04:00
|
|
|
upReq.src = file
|
|
|
|
upReq.filename = headers.Filename
|
2015-09-30 00:56:51 -04:00
|
|
|
} else {
|
|
|
|
if r.FormValue("content") == "" {
|
2015-10-04 17:27:47 -04:00
|
|
|
oopsHandler(c, w, r, RespHTML, "Empty file")
|
2015-09-30 00:56:51 -04:00
|
|
|
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 00:58:38 -04:00
|
|
|
}
|
2015-09-24 01:44:49 -04:00
|
|
|
|
|
|
|
upload, err := processUpload(upReq)
|
|
|
|
|
2015-09-25 00:58:38 -04:00
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
js := generateJSONresponse(upload)
|
2015-09-25 00:58:38 -04:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.Write(js)
|
|
|
|
} else {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:58:38 -04:00
|
|
|
http.Redirect(w, r, "/"+upload.Filename, 301)
|
|
|
|
}
|
|
|
|
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|
|
|
upReq := UploadRequest{}
|
2015-09-27 22:17:12 -04:00
|
|
|
uploadHeaderProcess(r, &upReq)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
|
|
|
defer r.Body.Close()
|
2015-09-25 12:00:14 -04:00
|
|
|
upReq.filename = c.URLParams["name"]
|
2015-09-24 01:44:49 -04:00
|
|
|
upReq.src = r.Body
|
|
|
|
|
|
|
|
upload, err := processUpload(upReq)
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
js := generateJSONresponse(upload)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.Write(js)
|
|
|
|
} else {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespPLAIN, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
fmt.Fprintf(w, Config.siteURL+upload.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:58:08 -04:00
|
|
|
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 {
|
2015-10-04 12:47:20 -04:00
|
|
|
oopsHandler(c, w, r, RespAUTO, "Could not retrieve URL")
|
2015-10-01 20:58:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upReq.filename = filepath.Base(grabUrl.Path)
|
|
|
|
upReq.src = resp.Body
|
2015-10-07 00:08:14 -04:00
|
|
|
upReq.deletionKey = r.FormValue("deletekey")
|
|
|
|
upReq.expiry = parseExpiry(r.FormValue("expiry"))
|
2015-10-01 20:58:08 -04:00
|
|
|
|
|
|
|
upload, err := processUpload(upReq)
|
|
|
|
|
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespJSON, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:58:08 -04:00
|
|
|
js := generateJSONresponse(upload)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.Write(js)
|
|
|
|
} else {
|
2015-10-04 12:47:20 -04:00
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, "Could not upload file: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-01 20:58:08 -04:00
|
|
|
http.Redirect(w, r, "/"+upload.Filename, 301)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
|
2015-10-06 00:31:09 -04:00
|
|
|
if r.Header.Get("Linx-Randomize") == "yes" {
|
2015-09-28 00:25:57 -04:00
|
|
|
upReq.randomBarename = true
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:31:09 -04:00
|
|
|
upReq.deletionKey = r.Header.Get("Linx-Delete-Key")
|
2015-09-28 00:25:57 -04:00
|
|
|
|
|
|
|
// Get seconds until expiry. Non-integer responses never expire.
|
2015-10-06 00:31:09 -04:00
|
|
|
expStr := r.Header.Get("Linx-Expiry")
|
2015-09-28 00:25:57 -04:00
|
|
|
upReq.expiry = parseExpiry(expStr)
|
|
|
|
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
2015-09-27 22:17:12 -04:00
|
|
|
// Determine the appropriate filename, then write to disk
|
2015-09-24 01:44:49 -04:00
|
|
|
barename, extension := barePlusExt(upReq.filename)
|
|
|
|
|
|
|
|
if upReq.randomBarename || len(barename) == 0 {
|
|
|
|
barename = generateBarename()
|
|
|
|
}
|
|
|
|
|
2015-10-01 11:03:41 -04:00
|
|
|
var header []byte
|
2015-09-24 01:44:49 -04:00
|
|
|
if len(extension) == 0 {
|
2015-10-01 11:03:41 -04:00
|
|
|
// Pull the first 512 bytes off for use in MIME detection
|
|
|
|
header = make([]byte, 512)
|
2015-10-07 01:15:45 -04:00
|
|
|
n, _ := upReq.src.Read(header)
|
|
|
|
if n == 0 {
|
2015-10-04 12:47:20 -04:00
|
|
|
return upload, errors.New("Empty file")
|
2015-10-01 11:03:41 -04:00
|
|
|
}
|
|
|
|
header = header[:n]
|
|
|
|
|
2015-09-29 20:59:59 -04: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 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
upload.Filename = strings.Join([]string{barename, extension}, ".")
|
|
|
|
|
2015-09-25 12:47:55 -04: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
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:12:19 -04:00
|
|
|
if fileBlacklist[strings.ToLower(upload.Filename)] {
|
|
|
|
return upload, errors.New("Prohibited filename")
|
|
|
|
}
|
|
|
|
|
2015-09-25 12:47:55 -04:00
|
|
|
dst, err := os.Create(path.Join(Config.filesDir, upload.Filename))
|
2015-09-24 08:30:16 -04:00
|
|
|
if err != nil {
|
2015-09-24 01:44:49 -04:00
|
|
|
return
|
|
|
|
}
|
2015-09-24 08:30:16 -04:00
|
|
|
defer dst.Close()
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-27 22:17:12 -04:00
|
|
|
// Get the rest of the metadata needed for storage
|
2015-10-07 22:45:34 -04:00
|
|
|
var expiry time.Time
|
2015-10-07 01:15:45 -04:00
|
|
|
if upReq.expiry == 0 {
|
2015-10-07 22:45:34 -04:00
|
|
|
expiry = neverExpire
|
2015-10-07 01:15:45 -04:00
|
|
|
} else {
|
2015-10-07 22:45:34 -04:00
|
|
|
expiry = time.Now().Add(upReq.expiry)
|
2015-10-06 02:50:20 -04:00
|
|
|
}
|
2015-09-27 22:17:12 -04:00
|
|
|
|
2015-10-01 11:03:41 -04:00
|
|
|
bytes, err := io.Copy(dst, io.MultiReader(bytes.NewReader(header), upReq.src))
|
2015-10-04 12:47:20 -04:00
|
|
|
if bytes == 0 {
|
2015-09-28 16:02:03 -04:00
|
|
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
return upload, errors.New("Empty file")
|
2015-10-04 12:47:20 -04:00
|
|
|
|
|
|
|
} else if err != nil {
|
|
|
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
return
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
upload.Metadata, err = generateMetadata(upload.Filename, expiry, upReq.deletionKey)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = metadataWrite(upload.Filename, &upload.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
|
|
|
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
|
|
|
return
|
|
|
|
}
|
2015-09-24 01:44:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateBarename() string {
|
2015-10-02 01:09:40 -04:00
|
|
|
return uniuri.NewLenChars(8, []byte("abcdefghijklmnopqrstuvwxyz0123456789"))
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
func generateJSONresponse(upload Upload) []byte {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
|
|
|
"url": Config.siteURL + upload.Filename,
|
|
|
|
"filename": upload.Filename,
|
2015-10-07 22:45:34 -04:00
|
|
|
"delete_key": upload.Metadata.DeleteKey,
|
|
|
|
"expiry": strconv.FormatInt(upload.Metadata.Expiry.Unix(), 10),
|
|
|
|
"size": strconv.FormatInt(upload.Metadata.Size, 10),
|
|
|
|
"mimetype": upload.Metadata.Mimetype,
|
|
|
|
"sha256sum": upload.Metadata.Sha256sum,
|
2015-09-28 00:25:57 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return js
|
|
|
|
}
|
|
|
|
|
2015-09-25 09:04:06 -04:00
|
|
|
var barePlusRe = regexp.MustCompile(`[^A-Za-z0-9\-]`)
|
|
|
|
|
2015-09-24 01:44:49 -04: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 09:04:06 -04:00
|
|
|
extension = barePlusRe.ReplaceAllString(extension, "")
|
|
|
|
barename = barePlusRe.ReplaceAllString(barename, "")
|
2015-09-24 01:44:49 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-09-28 00:25:57 -04:00
|
|
|
|
2015-10-06 02:50:20 -04:00
|
|
|
func parseExpiry(expStr string) time.Duration {
|
2015-09-28 00:25:57 -04:00
|
|
|
if expStr == "" {
|
|
|
|
return 0
|
|
|
|
} else {
|
2015-09-29 19:00:16 -04:00
|
|
|
expiry, err := strconv.ParseInt(expStr, 10, 64)
|
2015-09-28 00:25:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
2015-10-06 02:50:20 -04:00
|
|
|
return time.Duration(expiry) * time.Second
|
2015-09-28 00:25:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|