2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-30 05:24:17 +02:00
|
|
|
"encoding/json"
|
2019-01-25 08:33:11 +01:00
|
|
|
"io/ioutil"
|
2015-09-24 07:44:49 +02:00
|
|
|
"net/http"
|
2015-09-30 18:06:23 +02:00
|
|
|
"path/filepath"
|
2015-09-30 05:24:17 +02:00
|
|
|
"strconv"
|
2015-09-24 07:44:49 +02:00
|
|
|
"strings"
|
2015-09-30 01:00:16 +02:00
|
|
|
"time"
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
"github.com/andreimarcu/linx-server/backends"
|
2017-05-02 06:25:56 +02:00
|
|
|
"github.com/andreimarcu/linx-server/expiry"
|
2015-09-30 01:00:16 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-09-24 07:44:49 +02:00
|
|
|
"github.com/flosch/pongo2"
|
2015-10-07 21:00:42 +02:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday"
|
2015-09-24 07:44:49 +02:00
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-10-06 08:50:20 +02:00
|
|
|
const maxDisplayFileSizeBytes = 1024 * 512
|
|
|
|
|
2020-02-17 15:58:56 +01:00
|
|
|
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request, fileName string, metadata backends.Metadata) {
|
2015-09-30 01:00:16 +02:00
|
|
|
var expiryHuman string
|
2017-05-02 06:25:56 +02:00
|
|
|
if metadata.Expiry != expiry.NeverExpire {
|
2015-10-08 04:45:34 +02:00
|
|
|
expiryHuman = humanize.RelTime(time.Now(), metadata.Expiry, "", "")
|
2015-09-30 01:00:16 +02:00
|
|
|
}
|
2015-10-08 04:45:34 +02:00
|
|
|
sizeHuman := humanize.Bytes(uint64(metadata.Size))
|
2015-09-30 18:06:23 +02:00
|
|
|
extra := make(map[string]string)
|
2015-10-15 18:24:23 +02:00
|
|
|
lines := []string{}
|
2015-09-30 01:00:16 +02:00
|
|
|
|
2015-09-30 18:06:23 +02:00
|
|
|
extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-30 05:24:17 +02:00
|
|
|
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
|
|
|
|
js, _ := json.Marshal(map[string]string{
|
2019-01-15 00:23:56 +01:00
|
|
|
"filename": fileName,
|
|
|
|
"direct_url": getSiteURL(r) + Config.selifPath + fileName,
|
|
|
|
"expiry": strconv.FormatInt(metadata.Expiry.Unix(), 10),
|
|
|
|
"size": strconv.FormatInt(metadata.Size, 10),
|
|
|
|
"mimetype": metadata.Mimetype,
|
|
|
|
"sha256sum": metadata.Sha256sum,
|
2015-09-30 05:24:17 +02:00
|
|
|
})
|
|
|
|
w.Write(js)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
var tpl *pongo2.Template
|
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
if strings.HasPrefix(metadata.Mimetype, "image/") {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/image.html"]
|
2015-10-07 22:45:41 +02:00
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "video/") {
|
|
|
|
tpl = Templates["display/video.html"]
|
2015-10-07 22:45:41 +02:00
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "audio/") {
|
|
|
|
tpl = Templates["display/audio.html"]
|
2015-10-07 22:45:41 +02:00
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
} else if metadata.Mimetype == "application/pdf" {
|
|
|
|
tpl = Templates["display/pdf.html"]
|
2015-10-07 22:45:41 +02:00
|
|
|
|
2015-12-02 20:58:48 +01:00
|
|
|
} else if extension == "story" {
|
2019-01-25 08:33:11 +01:00
|
|
|
metadata, reader, err := storageBackend.Get(fileName)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
2019-01-25 08:33:11 +01:00
|
|
|
bytes, err := ioutil.ReadAll(reader)
|
2015-10-07 22:45:41 +02:00
|
|
|
if err == nil {
|
2015-09-30 18:06:23 +02:00
|
|
|
extra["contents"] = string(bytes)
|
2015-12-02 20:58:48 +01:00
|
|
|
lines = strings.Split(extra["contents"], "\n")
|
|
|
|
tpl = Templates["display/story.html"]
|
2015-09-30 18:06:23 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 06:19:33 +01:00
|
|
|
|
2015-10-07 21:00:42 +02:00
|
|
|
} else if extension == "md" {
|
2019-01-25 08:33:11 +01:00
|
|
|
metadata, reader, err := storageBackend.Get(fileName)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, err.Error())
|
|
|
|
}
|
|
|
|
|
2015-10-08 04:45:34 +02:00
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
2019-01-25 08:33:11 +01:00
|
|
|
bytes, err := ioutil.ReadAll(reader)
|
2015-10-07 22:45:41 +02:00
|
|
|
if err == nil {
|
2015-10-07 21:00:42 +02:00
|
|
|
unsafe := blackfriday.MarkdownCommon(bytes)
|
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
|
|
|
|
|
|
|
extra["contents"] = string(html)
|
|
|
|
tpl = Templates["display/md.html"]
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 06:19:33 +01:00
|
|
|
|
|
|
|
} else if strings.HasPrefix(metadata.Mimetype, "text/") || supportedBinExtension(extension) {
|
2019-01-25 08:33:11 +01:00
|
|
|
metadata, reader, err := storageBackend.Get(fileName)
|
|
|
|
if err != nil {
|
|
|
|
oopsHandler(c, w, r, RespHTML, err.Error())
|
|
|
|
}
|
|
|
|
|
2015-12-04 06:19:33 +01:00
|
|
|
if metadata.Size < maxDisplayFileSizeBytes {
|
2019-01-25 08:33:11 +01:00
|
|
|
bytes, err := ioutil.ReadAll(reader)
|
2015-12-04 06:19:33 +01:00
|
|
|
if err == nil {
|
|
|
|
extra["extension"] = extension
|
2020-03-13 00:35:50 +01:00
|
|
|
extra["lang_hl"] = extensionToHlLang(extension)
|
2015-12-04 06:19:33 +01:00
|
|
|
extra["contents"] = string(bytes)
|
|
|
|
tpl = Templates["display/bin.html"]
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 22:45:41 +02:00
|
|
|
}
|
2015-10-07 21:00:42 +02:00
|
|
|
|
2015-10-07 22:45:41 +02:00
|
|
|
// Catch other files
|
|
|
|
if tpl == nil {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/file.html"]
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
2020-02-17 15:58:56 +01:00
|
|
|
err := renderTemplate(tpl, pongo2.Context{
|
2019-01-26 11:12:49 +01:00
|
|
|
"mime": metadata.Mimetype,
|
|
|
|
"filename": fileName,
|
|
|
|
"size": sizeHuman,
|
|
|
|
"expiry": expiryHuman,
|
|
|
|
"expirylist": listExpirationTimes(),
|
|
|
|
"extra": extra,
|
|
|
|
"forcerandom": Config.forceRandomFilename,
|
|
|
|
"lines": lines,
|
|
|
|
"files": metadata.ArchiveFiles,
|
2020-05-14 10:12:24 +02:00
|
|
|
"siteurl": strings.TrimSuffix(getSiteURL(r), "/"),
|
2016-06-15 08:21:39 +02:00
|
|
|
}, r, w)
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2015-10-04 18:47:20 +02:00
|
|
|
oopsHandler(c, w, r, RespHTML, "")
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
}
|