2015-09-24 07:44:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-30 05:24:17 +02:00
|
|
|
"encoding/json"
|
2015-09-24 07:44:49 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
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
|
|
|
|
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"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-09-28 06:25:57 +02:00
|
|
|
if !fileExistsAndNotExpired(fileName) {
|
2015-09-25 18:00:14 +02:00
|
|
|
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
|
|
|
|
if expiry != 0 {
|
|
|
|
expiryHuman = humanize.RelTime(time.Now(), time.Unix(expiry, 0), "", "")
|
|
|
|
}
|
|
|
|
sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
|
|
|
|
|
2015-09-29 03:41:07 +02:00
|
|
|
file, _ := os.Open(filePath)
|
|
|
|
header := make([]byte, 512)
|
|
|
|
file.Read(header)
|
|
|
|
file.Close()
|
2015-09-24 07:44:49 +02:00
|
|
|
|
2015-09-29 03:41:07 +02:00
|
|
|
mimetype := mimemagic.Match("", header)
|
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{
|
|
|
|
"filename": fileName,
|
|
|
|
"mimetype": mimetype,
|
|
|
|
"expiry": strconv.FormatInt(expiry, 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/") {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/image.html"]
|
2015-09-25 05:21:37 +02:00
|
|
|
} else if strings.HasPrefix(mimetype, "video/") {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/video.html"]
|
2015-09-28 05:07:15 +02:00
|
|
|
} else if strings.HasPrefix(mimetype, "audio/") {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/audio.html"]
|
2015-09-28 05:07:15 +02:00
|
|
|
} else if mimetype == "application/pdf" {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/pdf.html"]
|
2015-09-24 07:44:49 +02:00
|
|
|
} else {
|
2015-09-29 02:46:58 +02:00
|
|
|
tpl = Templates["display/file.html"]
|
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-30 01:00:16 +02:00
|
|
|
"size": sizeHuman,
|
|
|
|
"expiry": expiryHuman,
|
2015-09-24 07:44:49 +02:00
|
|
|
}, w)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-25 18:00:14 +02:00
|
|
|
oopsHandler(c, w, r)
|
2015-09-24 07:44:49 +02:00
|
|
|
}
|
|
|
|
}
|