package main
import (
"fmt"
"io"
"text/template"
humanize "github.com/dustin/go-humanize"
"github.com/vbatts/imgsrv/types"
)
var emptyInterface interface{}
var headTemplate = template.Must(template.New("head").Parse(headTemplateHTML))
var headTemplateHTML = `
{{.title}}
`
var navbarTemplate = template.Must(template.New("navbar").Parse(navbarTemplateHTML))
var navbarTemplateHTML = `
`
var containerBeginTemplate = template.Must(template.New("containerBegin").Parse(containerBeginTemplateHTML))
var containerBeginTemplateHTML = `
`
var tailTemplate = template.Must(template.New("tail").Parse(tailTemplateHTML))
var tailTemplateHTML = `
{{if .}}
{{end}}
`
var formDeleteFileTemplate = template.Must(template.New("formDeleteFile").Parse(formDeleteFileTemplateHTML))
var formDeleteFileTemplateHTML = `
Get file from URL
{{if .}}
{{else}}
ERROR: No File provided!
{{end}}
{{/* hero-unit */}}
{{/* span9 */}}
`
var formGetUrlTemplate = template.Must(template.New("formGetUrl").Parse(formGetUrlTemplateHTML))
var formGetUrlTemplateHTML = `
{{/* span9 */}}
`
var formFileUploadTemplate = template.Must(template.New("formFileUpload").Parse(formFileUploadTemplateHTML))
var formFileUploadTemplateHTML = `
{{/* span9 */}}
`
var listTemplate = template.Must(template.New("list").Parse(listTemplateHTML))
var listTemplateHTML = `
{{if .}}
{{end}}
`
var tagcloudTemplate = template.Must(template.New("tagcloud").Parse(tagcloudTemplateHTML))
var tagcloudTemplateHTML = `
{{if .}}
{{end}}
`
var fileViewImageTemplate = template.Must(template.New("file").Parse(fileViewImageTemplateHTML))
var fileViewImageTemplateHTML = `
{{if .}}
{{end}}
`
var fileViewAudioTemplate = template.Must(template.New("file").Parse(fileViewAudioTemplateHTML))
var fileViewAudioTemplateHTML = `
{{if .}}
{{end}}
`
var fileViewVideoTemplate = template.Must(template.New("file").Parse(fileViewVideoTemplateHTML))
var fileViewVideoTemplateHTML = `
{{if .}}
{{end}}
`
var fileViewTemplate = template.Must(template.New("file").Parse(fileViewTemplateHTML))
var fileViewTemplateHTML = `
{{if .}}
{{.Filename}}
{{end}}
`
var funcs = template.FuncMap{
"humanBytes": humanize.Bytes,
"humanTime": humanize.Time,
}
var fileViewInfoTemplate = template.Must(template.New("file").Funcs(funcs).Parse(fileViewInfoTemplateHTML))
var fileViewInfoTemplateHTML = `
{{if .}}
[keywords:{{range $key := .Metadata.Keywords}} {{$key}}{{end}}]
[md5: {{.Md5}}]
[size: {{humanBytes .Length}}]
[UploadDate: {{.Metadata.TimeStamp}} ({{humanTime .Metadata.TimeStamp}})]
[Delete]
{{end}}
`
func DeleteFilePage(w io.Writer, filename string) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: delete"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
err = formDeleteFileTemplate.Execute(w, &filename)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func UrliePage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
err = formGetUrlTemplate.Execute(w, &emptyInterface)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func UploadPage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: Upload"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
// main context of this page
err = formFileUploadTemplate.Execute(w, &emptyInterface)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func ImageViewPage(w io.Writer, file types.File) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
if file.IsImage() {
err = fileViewImageTemplate.Execute(w, file)
} else if file.IsAudio() {
err = fileViewAudioTemplate.Execute(w, file)
} else if file.IsVideo() {
err = fileViewVideoTemplate.Execute(w, file)
} else {
err = fileViewTemplate.Execute(w, file)
}
if err != nil {
return err
}
err = fileViewInfoTemplate.Execute(w, file)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func ListFilesPage(w io.Writer, files []types.File) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
// main context of this page
err = listTemplate.Execute(w, files)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func ListTagCloudPage(w io.Writer, ic []types.IdCount) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
// main context of this page
err = tagcloudTemplate.Execute(w, ic)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}
func ErrorPage(w io.Writer, e error) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: ERROR"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
// main context of this page
err = template.Must(template.New("serverError").Parse(`
{{if .}}
{{/* span9 */}}
{{end}}
`)).Execute(w, e)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}