2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/rakyll/magicmime"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
2015-09-25 18:00:14 +02:00
|
|
|
var imageTpl = pongo2.Must(pongo2.FromCache("templates/display/image.html"))
|
2015-09-28 05:07:15 +02:00
|
|
|
var audioTpl = pongo2.Must(pongo2.FromCache("templates/display/audio.html"))
|
2015-09-25 18:00:14 +02:00
|
|
|
var videoTpl = pongo2.Must(pongo2.FromCache("templates/display/video.html"))
|
|
|
|
var fileTpl = pongo2.Must(pongo2.FromCache("templates/display/file.html"))
|
2015-09-28 05:07:15 +02:00
|
|
|
var pdfTpl = pongo2.Must(pongo2.FromCache("templates/display/pdf.html"))
|
2015-09-25 18:00:14 +02:00
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2015-09-25 04:20:44 +02:00
|
|
|
fileName := c.URLParams["name"]
|
|
|
|
filePath := path.Join(Config.filesDir, fileName)
|
|
|
|
fileInfo, err := os.Stat(filePath)
|
2015-09-24 07:44:49 +02:00
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
2015-09-25 18:00:14 +02:00
|
|
|
notFoundHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-28 05:07:15 +02:00
|
|
|
// file expiry checking
|
|
|
|
|
2015-09-24 07:44:49 +02:00
|
|
|
if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE |
|
|
|
|
magicmime.MAGIC_SYMLINK |
|
|
|
|
magicmime.MAGIC_ERROR); err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
defer magicmime.Close()
|
|
|
|
|
2015-09-25 04:20:44 +02:00
|
|
|
mimetype, err := magicmime.TypeByFile(filePath)
|
2015-09-24 07:44:49 +02:00
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tpl *pongo2.Template
|
|
|
|
|
|
|
|
if strings.HasPrefix(mimetype, "image/") {
|
2015-09-25 18:00:14 +02:00
|
|
|
tpl = imageTpl
|
2015-09-25 05:21:37 +02:00
|
|
|
} else if strings.HasPrefix(mimetype, "video/") {
|
2015-09-25 18:00:14 +02:00
|
|
|
tpl = videoTpl
|
2015-09-28 05:07:15 +02:00
|
|
|
} else if strings.HasPrefix(mimetype, "audio/") {
|
|
|
|
tpl = audioTpl
|
|
|
|
} else if mimetype == "application/pdf" {
|
|
|
|
tpl = pdfTpl
|
2015-09-24 07:44:49 +02:00
|
|
|
} else {
|
2015-09-25 18:00:14 +02:00
|
|
|
tpl = fileTpl
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = tpl.ExecuteWriter(pongo2.Context{
|
|
|
|
"mime": mimetype,
|
2015-09-25 04:20:44 +02:00
|
|
|
"filename": fileName,
|
2015-09-24 07:44:49 +02:00
|
|
|
"size": fileInfo.Size(),
|
|
|
|
}, w)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
}
|