2015-09-24 01:44:49 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/rakyll/magicmime"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
fileInfo, err := os.Stat(filePath)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-28 00:25:57 -04:00
|
|
|
if !fileExistsAndNotExpired(fileName) {
|
2015-09-25 12:00:14 -04:00
|
|
|
notFoundHandler(c, w, r)
|
2015-09-24 01:44:49 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE |
|
|
|
|
magicmime.MAGIC_SYMLINK |
|
|
|
|
magicmime.MAGIC_ERROR); err != nil {
|
2015-09-25 12:00:14 -04:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
defer magicmime.Close()
|
|
|
|
|
2015-09-24 22:20:44 -04:00
|
|
|
mimetype, err := magicmime.TypeByFile(filePath)
|
2015-09-24 01:44:49 -04:00
|
|
|
if err != nil {
|
2015-09-25 12:00:14 -04:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var tpl *pongo2.Template
|
|
|
|
|
|
|
|
if strings.HasPrefix(mimetype, "image/") {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/image.html"]
|
2015-09-24 23:21:37 -04:00
|
|
|
} else if strings.HasPrefix(mimetype, "video/") {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/video.html"]
|
2015-09-27 23:07:15 -04:00
|
|
|
} else if strings.HasPrefix(mimetype, "audio/") {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/audio.html"]
|
2015-09-27 23:07:15 -04:00
|
|
|
} else if mimetype == "application/pdf" {
|
2015-09-28 20:46:58 -04:00
|
|
|
tpl = Templates["display/pdf.html"]
|
2015-09-24 01:44:49 -04:00
|
|
|
} else {
|
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{
|
|
|
|
"mime": mimetype,
|
2015-09-24 22:20:44 -04:00
|
|
|
"filename": fileName,
|
2015-09-24 01:44:49 -04:00
|
|
|
"size": fileInfo.Size(),
|
|
|
|
}, w)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-25 12:00:14 -04:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
}
|