add a preview page

This commit is contained in:
mappu 2017-11-18 13:48:34 +13:00
parent d35c81ed21
commit 930869759b
2 changed files with 61 additions and 0 deletions

View File

@ -91,6 +91,7 @@ func (this *Server) remoteIP(r *http.Request) string {
const (
downloadUrlPrefix = `/get/`
metadataUrlPrefix = `/info/`
previewUrlPrefix = `/p/`
)
var rxThumbUrl = regexp.MustCompile(`^/thumb/(.)/(.*)$`)
@ -113,6 +114,9 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.Method == "GET" && strings.HasPrefix(r.URL.Path, metadataUrlPrefix) {
this.handleInformation(w, r.URL.Path[len(metadataUrlPrefix):])
} else if r.Method == "GET" && strings.HasPrefix(r.URL.Path, previewUrlPrefix) {
this.handlePreview(w, r.URL.Path[len(previewUrlPrefix):])
} else if r.Method == "GET" && rxThumbUrl.MatchString(r.URL.Path) {
parts := rxThumbUrl.FindStringSubmatch(r.URL.Path)
this.handleThumb(w, r, parts[1][0], parts[2])

57
preview.go Normal file
View File

@ -0,0 +1,57 @@
package contented
import (
"fmt"
"html"
"log"
"net/http"
"os"
"time"
)
func (this *Server) handlePreview(w http.ResponseWriter, fileID string) {
m, err := this.Metadata(fileID)
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "Not found", 404)
return
}
log.Println(err.Error())
http.Error(w, "Internal error", 500)
return
}
tmpl := `<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body {
background: #333;
color: #F0F0F0;
font-family: sans-serif;
}
</style>
</head>
<body>
<div class="container">
<div class="properties">
<b>Name:</b> ` + html.EscapeString(m.Filename) + `<br>
<b>Hash:</b> ` + html.EscapeString(m.FileHash) + `<br>
<b>File type:</b> ` + html.EscapeString(m.MimeType) + `<br>
<b>Size:</b> ` + html.EscapeString(fmt.Sprintf("%d", m.FileSize)) + `<br>
<b>Uploader:</b> ` + html.EscapeString(m.UploadIP) + `<br>
<b>Uploaded at:</b> ` + html.EscapeString(m.UploadTime.Format(time.RFC3339)) + `<br>
</div>
<div class="thumbnail">
<a href="` + html.EscapeString(`/get/`+fileID) + `"><img src="` + html.EscapeString(`/thumb/m/`+fileID) + `"></a>
</div>
</div>
</body>
</html>`
w.Header().Set(`Content-Type`, `text/html; charset=UTF-8`)
w.Header().Set(`Content-Length`, fmt.Sprintf("%d", len(tmpl)))
w.WriteHeader(200)
w.Write([]byte(tmpl))
}