package main
import (
"fmt"
"github.com/vbatts/imgsrv/types"
"io"
"text/template"
)
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 fileViewImageTemplate = template.Must(template.New("file").Parse(fileViewImageTemplateHTML))
var fileViewImageTemplateHTML = `
{{if .}}
{{end}}
`
var fileViewAudioTemplate = template.Must(template.New("file").Parse(fileViewAudioTemplateHTML))
var fileViewAudioTemplateHTML = `
{{if .}}
Your browser does not support the video tag.
{{end}}
`
var fileViewVideoTemplate = template.Must(template.New("file").Parse(fileViewVideoTemplateHTML))
var fileViewVideoTemplateHTML = `
{{if .}}
Your browser does not support the video tag.
{{end}}
`
var fileViewTemplate = template.Must(template.New("file").Parse(fileViewTemplateHTML))
var fileViewTemplateHTML = `
{{if .}}
{{.Filename}}
{{end}}
`
var fileViewInfoTemplate = template.Must(template.New("file").Parse(fileViewInfoTemplateHTML))
var fileViewInfoTemplateHTML = `
{{if .}}
[keywords:{{range $key := .Metadata.Keywords}} {{$key}} {{end}}]
[md5: {{.Md5}} ]
[size: {{.Length}}]
[UploadDate: {{.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
}