ProfouzorsLinx/display.go

185 lines
4.2 KiB
Go
Raw Normal View History

2015-09-24 07:44:49 +02:00
package main
import (
"archive/tar"
"archive/zip"
"compress/bzip2"
"compress/gzip"
"encoding/json"
"io"
2015-09-30 18:06:23 +02:00
"io/ioutil"
2015-09-24 07:44:49 +02:00
"net/http"
"os"
"path"
2015-09-30 18:06:23 +02:00
"path/filepath"
"sort"
"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
2015-09-29 03:41:07 +02:00
"bitbucket.org/taruti/mimemagic"
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"
)
const maxDisplayFileSizeBytes = 1024 * 512
2015-09-24 07:44:49 +02:00
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
filePath := path.Join(Config.filesDir, fileName)
fileInfo, err := os.Stat(filePath)
2015-09-24 07:44:49 +02:00
if !fileExistsAndNotExpired(fileName) {
notFoundHandler(c, w, r)
2015-09-24 07:44:49 +02:00
return
}
2015-09-30 01:00:16 +02:00
expiry, _ := metadataGetExpiry(fileName)
var expiryHuman string
2015-10-07 09:00:03 +02:00
if expiry != neverExpire {
expiryHuman = humanize.RelTime(time.Now(), expiry, "", "")
2015-09-30 01:00:16 +02:00
}
sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
2015-09-30 18:06:23 +02:00
extra := make(map[string]string)
files := []string{}
2015-09-30 01:00:16 +02:00
2015-09-29 03:41:07 +02:00
file, _ := os.Open(filePath)
defer file.Close()
2015-09-29 03:41:07 +02:00
header := make([]byte, 512)
file.Read(header)
2015-09-24 07:44:49 +02:00
2015-09-29 03:41:07 +02:00
mimetype := mimemagic.Match("", header)
2015-09-30 18:06:23 +02:00
extension := strings.TrimPrefix(filepath.Ext(fileName), ".")
2015-09-24 07:44:49 +02:00
if strings.EqualFold("application/json", r.Header.Get("Accept")) {
js, _ := json.Marshal(map[string]string{
"filename": fileName,
"mimetype": mimetype,
"expiry": strconv.FormatInt(expiry.Unix(), 10),
"size": strconv.FormatInt(fileInfo.Size(), 10),
})
w.Write(js)
return
}
2015-09-24 07:44:49 +02:00
var tpl *pongo2.Template
if strings.HasPrefix(mimetype, "image/") {
tpl = Templates["display/image.html"]
} else if strings.HasPrefix(mimetype, "video/") {
tpl = Templates["display/video.html"]
2015-09-28 05:07:15 +02:00
} else if strings.HasPrefix(mimetype, "audio/") {
tpl = Templates["display/audio.html"]
2015-09-28 05:07:15 +02:00
} else if mimetype == "application/pdf" {
tpl = Templates["display/pdf.html"]
} else if mimetype == "application/x-tar" {
f, _ := os.Open(filePath)
defer f.Close()
tReadr := tar.NewReader(f)
for {
header, err := tReadr.Next()
if err == io.EOF || err != nil {
break
}
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg {
files = append(files, header.Name)
}
}
sort.Strings(files)
} else if mimetype == "application/x-gzip" {
f, _ := os.Open(filePath)
defer f.Close()
gzf, err := gzip.NewReader(f)
if err == nil {
tReadr := tar.NewReader(gzf)
for {
header, err := tReadr.Next()
if err == io.EOF || err != nil {
break
}
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg {
files = append(files, header.Name)
}
}
sort.Strings(files)
}
} else if mimetype == "application/x-bzip" {
f, _ := os.Open(filePath)
defer f.Close()
bzf := bzip2.NewReader(f)
tReadr := tar.NewReader(bzf)
for {
header, err := tReadr.Next()
if err == io.EOF || err != nil {
break
}
if header.Typeflag == tar.TypeDir || header.Typeflag == tar.TypeReg {
files = append(files, header.Name)
}
}
sort.Strings(files)
} else if mimetype == "application/zip" {
f, _ := os.Open(filePath)
defer f.Close()
zf, err := zip.NewReader(f, fileInfo.Size())
if err == nil {
for _, f := range zf.File {
files = append(files, f.Name)
}
}
2015-09-30 18:06:23 +02:00
} else if supportedBinExtension(extension) {
if fileInfo.Size() < maxDisplayFileSizeBytes {
2015-09-30 18:06:23 +02:00
bytes, err := ioutil.ReadFile(filePath)
if err == nil {
2015-09-30 18:06:23 +02:00
extra["extension"] = extension
extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
extra["contents"] = string(bytes)
tpl = Templates["display/bin.html"]
}
}
2015-10-07 21:00:42 +02:00
} else if extension == "md" {
if fileInfo.Size() < maxDisplayFileSizeBytes {
bytes, err := ioutil.ReadFile(filePath)
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-10-07 21:00:42 +02:00
// Catch other files
if tpl == nil {
tpl = Templates["display/file.html"]
2015-09-24 07:44:49 +02:00
}
err = tpl.ExecuteWriter(pongo2.Context{
"mime": mimetype,
"filename": fileName,
2015-09-30 01:00:16 +02:00
"size": sizeHuman,
"expiry": expiryHuman,
2015-09-30 18:06:23 +02:00
"extra": extra,
"files": files,
2015-09-24 07:44:49 +02:00
}, w)
if err != nil {
2015-10-04 18:47:20 +02:00
oopsHandler(c, w, r, RespHTML, "")
2015-09-24 07:44:49 +02:00
}
}