2015-09-24 01:44:49 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-29 23:24:17 -04:00
|
|
|
"encoding/json"
|
2015-09-30 12:06:23 -04:00
|
|
|
"io/ioutil"
|
2015-09-24 01:44:49 -04:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-09-30 12:06:23 -04:00
|
|
|
"path/filepath"
|
2015-09-29 23:24:17 -04:00
|
|
|
"strconv"
|
2015-09-24 01:44:49 -04:00
|
|
|
"strings"
|
2015-09-29 19:00:16 -04:00
|
|
|
"time"
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-29 19:00:16 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-09-24 01:44:49 -04:00
|
|
|
"github.com/flosch/pongo2"
|
2015-10-07 15:00:42 -04:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday"
|
2015-09-24 01:44:49 -04:00
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-10-06 02:50:20 -04:00
|
|
|
const maxDisplayFileSizeBytes = 1024 * 512
|
|
|
|
|
2015-09-24 01:44:49 -04:00
|
|
|
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2015-09-24 22:20:44 -04:00
|
|
|
fileName := c.URLParams["name"]
|
|
|
|
filePath := path.Join(Config.filesDir, fileName)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
err := checkFile(fileName)
|
|
|
|
if err == NotFoundErr {
|
2015-09-25 12:00:14 -04:00
|
|
|
notFoundHandler(c, w, r)
|
2015-09-24 01:44:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
metadata, err := metadataRead(fileName)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
|
|
|
|
return
|
|
|
|
}
|
2015-09-29 19:00:16 -04:00
|
|
|
var expiryHuman string
|
2015-10-07 22:45:34 -04:00
|
|
|
if metadata.Expiry != neverExpire {
|
|
|
|
expiryHuman = humanize.RelTime(time.Now(), metadata.Expiry, "", "")
|
2015-09-29 19:00:16 -04:00
|
|
|
}
|
2015-10-07 22:45:34 -04:00
|
|
|
sizeHuman := humanize.Bytes(uint64(metadata.Size))
|
2015-09-30 12:06:23 -04:00
|
|
|
extra := make(map[string]string)
|
2015-10-15 12:24:23 -04:00
|
|
|
lines := []string{}
|
2015-09-29 19:00:16 -04:00
|
|
|
|
2015-09-28 21:41:07 -04:00
|
|
|
file, _ := os.Open(filePath)
|
2015-10-07 16:45:41 -04:00
|
|
|
defer file.Close()
|
|
|
|
|
2015-09-28 21:41:07 -04:00
|
|
|
header := make([]byte, 512)
|
|
|
|
file.Read(header)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-30 12:06:23 -04:00
|
|
|
extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-29 23:24:17 -04:00
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
2015-10-07 22:45:34 -04:00
|
|
|
"filename": fileName,
|
|
|
|
"expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
|
|
|
|
"size": strconv.FormatInt(metadata.Size, 10),
|
|
|
|
"mimetype": metadata.Mimetype,
|
|
|
|
"sha256sum": metadata.Sha256sum,
|
2015-09-29 23:24:17 -04:00
|
|
|
})
|
|
|
|
w.Write(js)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-24 01:44:49 -04:00
|
|
|
var tpl *pongo2.Template
|
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
if strings.HasPrefix(metadata.Mimetype, "image/") {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/image.html"]
|
2015-10-07 16:45:41 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "video/") {
|
|
|
|
tpl = Templates["display/video.html"]
|
2015-10-07 16:45:41 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "audio/") {
|
|
|
|
tpl = Templates["display/audio.html"]
|
2015-10-07 16:45:41 -04:00
|
|
|
|
2015-10-07 22:45:34 -04:00
|
|
|
} else if metadata.Mimetype == "application/pdf" {
|
|
|
|
tpl = Templates["display/pdf.html"]
|
2015-10-07 16:45:41 -04:00
|
|
|
|
2015-11-12 01:04:05 -05:00
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
|
2015-10-07 22:45:34 -04:00
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
2015-09-30 12:06:23 -04:00
|
|
|
bytes, err := ioutil.ReadFile(filePath)
|
2015-10-07 16:45:41 -04:00
|
|
|
if err == nil {
|
2015-09-30 12:06:23 -04:00
|
|
|
extra["extension"] = extension
|
|
|
|
extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
|
|
|
|
extra["contents"] = string(bytes)
|
|
|
|
tpl = Templates["display/bin.html"]
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 12:24:23 -04:00
|
|
|
} else if extension == "story" {
|
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
|
|
|
bytes, err := ioutil.ReadFile(filePath)
|
|
|
|
if err == nil {
|
|
|
|
extra["contents"] = string(bytes)
|
|
|
|
lines = strings.Split(extra["contents"], "\n")
|
|
|
|
tpl = Templates["display/story.html"]
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 15:00:42 -04:00
|
|
|
} else if extension == "md" {
|
2015-10-07 22:45:34 -04:00
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
2015-10-07 15:00:42 -04:00
|
|
|
bytes, err := ioutil.ReadFile(filePath)
|
2015-10-07 16:45:41 -04:00
|
|
|
if err == nil {
|
2015-10-07 15:00:42 -04:00
|
|
|
unsafe := blackfriday.MarkdownCommon(bytes)
|
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
|
|
|
|
|
|
|
extra["contents"] = string(html)
|
|
|
|
tpl = Templates["display/md.html"]
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 16:45:41 -04:00
|
|
|
}
|
2015-10-07 15:00:42 -04:00
|
|
|
|
2015-10-07 16:45:41 -04:00
|
|
|
// Catch other files
|
|
|
|
if tpl == nil {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/file.html"]
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tpl.ExecuteWriter(pongo2.Context{
|
2015-10-07 22:45:34 -04:00
|
|
|
"mime": metadata.Mimetype,
|
2015-09-24 22:20:44 -04:00
|
|
|
"filename": fileName,
|
2015-09-29 19:00:16 -04:00
|
|
|
"size": sizeHuman,
|
|
|
|
"expiry": expiryHuman,
|
2015-09-30 12:06:23 -04:00
|
|
|
"extra": extra,
|
2015-10-15 12:24:23 -04:00
|
|
|
"lines": lines,
|
2015-10-07 22:45:34 -04:00
|
|
|
"files": metadata.ArchiveFiles,
|
2015-09-24 01:44:49 -04:00
|
|
|
}, w)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-10-04 12:47:20 -04:00
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
}
|