mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-24 00:55:41 +00:00
ZOMG, just ironed out the '/v/' page, to handle embedding video with
html5 tag
This commit is contained in:
parent
967daf48b2
commit
c62df15429
3 changed files with 47 additions and 11 deletions
37
layouts.go
37
layouts.go
|
@ -131,14 +131,35 @@ var listTemplateHTML = `
|
||||||
{{end}}
|
{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var fileViewImageTemplate = template.Must(template.New("file").Parse(fileViewImageTemplateHTML))
|
||||||
|
var fileViewImageTemplateHTML = `
|
||||||
|
{{if .}}
|
||||||
|
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></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 fileViewTemplate = template.Must(template.New("file").Parse(fileViewTemplateHTML))
|
||||||
var fileViewTemplateHTML = `
|
var fileViewTemplateHTML = `
|
||||||
{{if .}}
|
{{if .}}
|
||||||
{{if .IsImage}}
|
|
||||||
<a href="/f/{{.Filename}}"><img src="/f/{{.Filename}}"></a>
|
|
||||||
{{else}}
|
|
||||||
<a href="/f/{{.Filename}}">{{.Filename}}</a>
|
<a href="/f/{{.Filename}}">{{.Filename}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
`
|
||||||
|
|
||||||
|
var fileViewInfoTemplate = template.Must(template.New("file").Parse(fileViewInfoTemplateHTML))
|
||||||
|
var fileViewInfoTemplateHTML = `
|
||||||
|
{{if .}}
|
||||||
<br/>
|
<br/>
|
||||||
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
[keywords:{{range $key := .Metadata.Keywords}} <a href="/k/{{$key}}">{{$key}}</a>{{end}}]
|
||||||
<br/>
|
<br/>
|
||||||
|
@ -211,7 +232,17 @@ func ImageViewPage(w io.Writer, file types.File) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if file.IsImage() {
|
||||||
|
err = fileViewImageTemplate.Execute(w, file)
|
||||||
|
} else if file.IsVideo() {
|
||||||
|
err = fileViewVideoTemplate.Execute(w, file)
|
||||||
|
} else {
|
||||||
err = fileViewTemplate.Execute(w, file)
|
err = fileViewTemplate.Execute(w, file)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = fileViewInfoTemplate.Execute(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,6 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file.SetIsImage()
|
|
||||||
err = ImageViewPage(w, file)
|
err = ImageViewPage(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: %s", err)
|
log.Printf("error: %s", err)
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
|
@ -22,12 +21,19 @@ type File struct {
|
||||||
UploadDate time.Time
|
UploadDate time.Time
|
||||||
Length int64
|
Length int64
|
||||||
Filename string ",omitempty"
|
Filename string ",omitempty"
|
||||||
IsImage bool
|
|
||||||
ContentType string "contentType,omitempty"
|
ContentType string "contentType,omitempty"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) SetIsImage() {
|
func (f *File) SetContentType() {
|
||||||
m_type := mime.TypeByExtension(filepath.Ext(f.Filename))
|
f.ContentType = mime.TypeByExtension(filepath.Ext(f.Filename))
|
||||||
f.IsImage = strings.Contains(m_type, "image")
|
}
|
||||||
fmt.Println(f.Filename,f.IsImage)
|
|
||||||
|
func (f *File) IsImage() bool {
|
||||||
|
f.SetContentType()
|
||||||
|
return strings.HasPrefix(f.ContentType, "image")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File) IsVideo() bool {
|
||||||
|
f.SetContentType()
|
||||||
|
return strings.HasPrefix(f.ContentType, "video")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue