filebrowser/page.go

128 lines
2.8 KiB
Go
Raw Normal View History

2016-06-10 21:18:44 +00:00
package filemanager
import (
2016-06-22 16:44:01 +00:00
"bufio"
"bytes"
2016-06-11 09:37:36 +00:00
"encoding/json"
2016-06-11 21:15:42 +00:00
"html/template"
2016-06-10 21:38:52 +00:00
"log"
2016-06-11 09:37:36 +00:00
"net/http"
2016-06-11 09:08:33 +00:00
"strings"
2016-06-10 21:18:44 +00:00
)
2016-06-11 21:15:42 +00:00
// Page contains the informations and functions needed to show the page
type Page struct {
Info *PageInfo
2016-06-21 14:02:30 +00:00
Tpl *template.Template
2016-06-11 21:15:42 +00:00
}
2016-06-21 14:02:30 +00:00
// AssetFunc is an Assets function
type AssetFunc func(name string) ([]byte, error)
2016-06-11 09:37:36 +00:00
// PageInfo contains the information of a page
type PageInfo struct {
2016-06-11 21:15:42 +00:00
Name string
Path string
2016-06-14 19:33:59 +00:00
IsDir bool
2016-06-11 21:15:42 +00:00
Config *Config
Data interface{}
2016-06-11 09:37:36 +00:00
}
// BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names.
func (p PageInfo) BreadcrumbMap() map[string]string {
result := map[string]string{}
if len(p.Path) == 0 {
return result
}
// skip trailing slash
lpath := p.Path
if lpath[len(lpath)-1] == '/' {
lpath = lpath[:len(lpath)-1]
}
parts := strings.Split(lpath, "/")
for i, part := range parts {
if i == 0 && part == "" {
// Leading slash (root)
result["/"] = "/"
continue
}
result[strings.Join(parts[:i+1], "/")] = part
}
return result
2016-06-10 21:18:44 +00:00
}
2016-06-14 17:19:10 +00:00
// PreviousLink returns the path of the previous folder
func (p PageInfo) PreviousLink() string {
2016-06-14 19:33:59 +00:00
parts := strings.Split(strings.TrimSuffix(p.Path, "/"), "/")
2016-06-14 17:19:10 +00:00
if len(parts) <= 1 {
return ""
}
2016-06-14 19:33:59 +00:00
if parts[len(parts)-2] == "" {
if p.Config.BaseURL == "" {
return "/"
}
return p.Config.BaseURL
}
2016-06-14 17:19:10 +00:00
return parts[len(parts)-2]
}
2016-06-21 14:02:30 +00:00
// AddTemplate adds a template file to the page template
2016-06-21 15:00:57 +00:00
func (p *Page) AddTemplate(name string, assets AssetFunc, functions template.FuncMap) (int, error) {
2016-06-10 21:38:52 +00:00
2016-06-21 14:02:30 +00:00
// Get the template from the assets
page, err := assets("templates/" + name + ".tmpl")
2016-06-10 21:38:52 +00:00
2016-06-21 14:02:30 +00:00
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return http.StatusInternalServerError, err
2016-06-10 21:18:44 +00:00
}
2016-06-21 14:02:30 +00:00
// If it's the first iteration, creates a new template and add the
// functions map
if p.Tpl == nil {
2016-06-21 15:00:57 +00:00
p.Tpl, err = template.New(name).Funcs(functions).Parse(string(page))
2016-06-21 14:02:30 +00:00
} else {
p.Tpl, err = p.Tpl.Parse(string(page))
}
if err != nil {
log.Print(err)
return http.StatusInternalServerError, err
}
return 0, nil
}
// PrintAsHTML formats the page in HTML and executes the template
func (p Page) PrintAsHTML(w http.ResponseWriter) (int, error) {
2016-06-22 16:44:01 +00:00
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
err := p.Tpl.Execute(writer, p.Info)
2016-06-11 09:08:33 +00:00
2016-06-11 09:37:36 +00:00
if err != nil {
return http.StatusInternalServerError, err
2016-06-11 09:08:33 +00:00
}
2016-06-22 16:44:01 +00:00
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buffer.WriteTo(w)
2016-06-11 09:37:36 +00:00
return http.StatusOK, nil
}
2016-06-11 09:08:33 +00:00
2016-06-11 09:37:36 +00:00
// PrintAsJSON prints the current page infromation in JSON
func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.Marshal(p.Info.Data)
if err != nil {
return http.StatusInternalServerError, err
2016-06-11 09:08:33 +00:00
}
2016-06-11 09:37:36 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
return w.Write(marsh)
2016-06-11 09:08:33 +00:00
}