64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"strings"
|
|
|
|
lib "git.makerforce.io/photos/photos/pkg/bucket"
|
|
"git.makerforce.io/photos/photos/web"
|
|
)
|
|
|
|
var funcs = template.FuncMap{
|
|
"ar": func(p Photo) float64 { return float64(p.Size.Width) / float64(p.Size.Height) },
|
|
"mul": func(a, b float64) float64 { return a * b },
|
|
"bucketmetadatatitle": func(t lib.BucketMetadataTitle) string { return strings.TrimSpace(string(t)) },
|
|
"photometadatatitle": func(t lib.PhotoMetadataTitle) string { return strings.TrimSpace(string(t)) },
|
|
"photo": photo,
|
|
"preview": preview,
|
|
}
|
|
|
|
type IndexTemplateData struct {
|
|
Assets string
|
|
Control string
|
|
|
|
Bucket lib.Bucket
|
|
Title lib.BucketMetadataTitle
|
|
Photos []Photo
|
|
|
|
Development bool
|
|
}
|
|
|
|
type Photo struct {
|
|
lib.Photo
|
|
Size lib.PhotoMetadataSize
|
|
Title lib.PhotoMetadataTitle
|
|
Tags lib.PhotoMetadataTags
|
|
}
|
|
|
|
var viewTemplate = mustTemplateAsset("view/index.tmpl")
|
|
var manageTemplate = mustTemplateAsset("manage/index.tmpl")
|
|
|
|
func mustTemplateAsset(name string) *template.Template {
|
|
buf, err := web.FS.ReadFile(name)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return template.Must(
|
|
template.New(name).Funcs(funcs).Parse(string(buf)),
|
|
)
|
|
}
|
|
|
|
func photo(p Photo) template.URL {
|
|
return template.URL(p.Path().String())
|
|
}
|
|
|
|
func preview(p Photo, height int, format lib.PhotoFormat, quality int) template.Srcset {
|
|
preview := p.GetPreview(lib.PreviewOption{
|
|
Height: height,
|
|
Format: format,
|
|
Quality: quality,
|
|
})
|
|
path := strings.ReplaceAll(preview.Path().String(), ",", "%2C")
|
|
return template.Srcset(path)
|
|
}
|