mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 23:36:30 +00:00
356 lines
8.3 KiB
Go
356 lines
8.3 KiB
Go
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 = `
|
|
<html>
|
|
<head>
|
|
<link href="/assets/bootstrap.css" media="screen" rel="stylesheet" type="text/css" />
|
|
<title>{{.title}}</title>
|
|
</head>
|
|
<body>
|
|
`
|
|
|
|
var navbarTemplate = template.Must(template.New("navbar").Parse(navbarTemplateHTML))
|
|
var navbarTemplateHTML = `
|
|
<div class="navbar navbar-inverse navbar-fixed-top">
|
|
<div class="navbar-inner">
|
|
<div class="container-fluid">
|
|
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
<span class="icon-bar"></span>
|
|
</a>
|
|
<a class="brand" href="/">filesrv</a>
|
|
<div class="nav-collapse collapse">
|
|
<p class="navbar-text pull-right">
|
|
</p>
|
|
<ul class="nav">
|
|
<li><a href="/">Home</a></li>
|
|
<li><a href="/upload">Upload</a></li>
|
|
<li><a href="/urlie">URLie</a></li>
|
|
<li><a href="/all">All</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
`
|
|
var containerBeginTemplate = template.Must(template.New("containerBegin").Parse(containerBeginTemplateHTML))
|
|
var containerBeginTemplateHTML = `
|
|
<div class="container-fluid">
|
|
<hr>
|
|
<hr>
|
|
<div class="row-fluid">
|
|
`
|
|
|
|
var tailTemplate = template.Must(template.New("tail").Parse(tailTemplateHTML))
|
|
var tailTemplateHTML = `
|
|
</div>
|
|
<hr>
|
|
</div>
|
|
{{if .}}
|
|
<footer>{{.footer}}</footer>
|
|
{{end}}
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
var formDeleteFileTemplate = template.Must(template.New("formDeleteFile").Parse(formDeleteFileTemplateHTML))
|
|
var formDeleteFileTemplateHTML = `
|
|
<div class="span9">
|
|
<div class="hero-unit">
|
|
<h3>Get file from URL</h3>
|
|
{{if .}}
|
|
<table>
|
|
<tr>
|
|
<b>Are you sure?</b>
|
|
</tr>
|
|
<br/>
|
|
<tr>
|
|
<td>
|
|
<a href="/v/{{.}}">no!</a>
|
|
<br/>
|
|
<a href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
{{else}}
|
|
<p>
|
|
<b>ERROR: No File provided!</b>
|
|
</p>
|
|
{{end}}
|
|
</div>{{/* hero-unit */}}
|
|
</div>{{/* span9 */}}
|
|
`
|
|
|
|
var formGetUrlTemplate = template.Must(template.New("formGetUrl").Parse(formGetUrlTemplateHTML))
|
|
var formGetUrlTemplateHTML = `
|
|
<div class="span9">
|
|
<div class="hero-unit">
|
|
<h3>Get file from URL</h3>
|
|
<form enctype="multipart/form-data" action="/urlie" method="POST">
|
|
<table>
|
|
<tr>
|
|
<td>
|
|
<input type="text" name="url" placeholder="file URL"><br/>
|
|
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i><br/>
|
|
<input type="checkbox" name="rand" value="true">Randomize filename
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<input type="submit" value="Fetch File"><br/>
|
|
</td>
|
|
</tr>
|
|
</td>
|
|
</table>
|
|
</form>
|
|
</div>{{/* hero-unit */}}
|
|
</div>{{/* span9 */}}
|
|
`
|
|
|
|
var formFileUploadTemplate = template.Must(template.New("formFileUpload").Parse(formFileUploadTemplateHTML))
|
|
var formFileUploadTemplateHTML = `
|
|
<div class="span9">
|
|
<div class="hero-unit">
|
|
<h3>Upload a File</h3>
|
|
<form enctype="multipart/form-data" action="/upload" method="POST">
|
|
<table>
|
|
<tr>
|
|
<td>
|
|
<input type="file" name="filename" placeholder="filename"><br/>
|
|
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i><br/>
|
|
<input type="checkbox" name="rand" value="true">Randomize filename
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<input type="submit" value="Upload File"><br/>
|
|
</td>
|
|
</tr>
|
|
</td>
|
|
</table>
|
|
</form>
|
|
</div>{{/* hero-unit */}}
|
|
</div>{{/* span9 */}}
|
|
`
|
|
|
|
var listTemplate = template.Must(template.New("list").Parse(listTemplateHTML))
|
|
var listTemplateHTML = `
|
|
{{if .}}
|
|
<ul>
|
|
{{range .}}
|
|
<li>
|
|
<a href="/v/{{.Filename}}">{{.Filename}}</a>
|
|
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
|
[md5: <a href="/md5/{{.Md5}}">{{.Md5 | printf "%8.8s"}}...</a>]</li>
|
|
{{end}}
|
|
</ul>
|
|
{{end}}
|
|
`
|
|
|
|
var fileViewImageTemplate = template.Must(template.New("file").Parse(fileViewImageTemplateHTML))
|
|
var fileViewImageTemplateHTML = `
|
|
{{if .}}
|
|
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
|
{{end}}
|
|
`
|
|
|
|
var fileViewAudioTemplate = template.Must(template.New("file").Parse(fileViewAudioTemplateHTML))
|
|
var fileViewAudioTemplateHTML = `
|
|
{{if .}}
|
|
<a href="/f/{{.Filename}}">
|
|
<audio controls>
|
|
<source src="/f/{{.Filename}}" type="{{.ContentType}}">
|
|
Your browser does not support the video tag.
|
|
</audio>
|
|
</a>
|
|
{{end}}
|
|
`
|
|
|
|
var fileViewVideoTemplate = template.Must(template.New("file").Parse(fileViewVideoTemplateHTML))
|
|
var fileViewVideoTemplateHTML = `
|
|
{{if .}}
|
|
<a href="/f/{{.Filename}}">
|
|
<video width="320" height="240" controls>
|
|
<source src="/f/{{.Filename}}" type="{{.ContentType}}">
|
|
Your browser does not support the video tag.
|
|
</video>
|
|
</a>
|
|
{{end}}
|
|
`
|
|
|
|
var fileViewTemplate = template.Must(template.New("file").Parse(fileViewTemplateHTML))
|
|
var fileViewTemplateHTML = `
|
|
{{if .}}
|
|
<a href="/f/{{.Filename}}">{{.Filename}}</a>
|
|
{{end}}
|
|
`
|
|
|
|
var fileViewInfoTemplate = template.Must(template.New("file").Parse(fileViewInfoTemplateHTML))
|
|
var fileViewInfoTemplateHTML = `
|
|
{{if .}}
|
|
<br/>
|
|
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
|
<br/>
|
|
[md5: <a href="/md5/{{.Md5}}">{{.Md5}}</a>]
|
|
<br/>
|
|
[size: {{.Length}}]
|
|
<br/>
|
|
[UploadDate: {{.Metadata.TimeStamp}}]
|
|
<br/>
|
|
[<a href="/f/{{.Filename}}?delete=true">Delete</a>]
|
|
{{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
|
|
}
|