package figtree
import (
"fmt"
"path"
"html/template"
"strings"
"io/ioutil"
)
type Fig struct {
Id string
Name string `json:"name"`
Desc string `json:"desc"`
Type string `json:"type"`
Ext string `json:"ext"`
Birth string `json:"birth"`
}
type FancyFig struct {
Name string
Path string
Desc string
Birth string
View template.HTML
}
func (f Fig) Fancy() FancyFig {
path := fmt.Sprintf("%s%s/%s.%s", FIGS, f.Type, f.Id, f.Ext)
view := tryDispHtml(f)
return FancyFig{ f.Name, path, f.Desc, f.Birth, view}
}
func tryDispHtml(fig Fig) template.HTML {
figtype := strings.ToLower(fig.Ext)
figpath := fig.Type + "/" + fig.Id + "." + fig.Ext
switch figtype {
case "png", "jpg", "jpeg", "gif":
return template.HTML("")
case "txt", "org":
contents, err := ioutil.ReadFile(path.Join(FIGS, figpath))
if err != nil {
panic(err)
}
return template.HTML(fmt.Sprintf("
", contents)) default: return template.HTML("%s
somethin good
") } }