2016-06-25 20:57:10 +00:00
|
|
|
package page
|
2016-06-10 21:18:44 +00:00
|
|
|
|
|
|
|
import (
|
2016-06-22 20:21:32 +00:00
|
|
|
"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-23 22:21:44 +00:00
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/assets"
|
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
2016-06-25 20:57:10 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/utils/variables"
|
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 {
|
2016-06-25 20:57:10 +00:00
|
|
|
*Info
|
2016-06-30 21:37:52 +00:00
|
|
|
Minimal bool
|
2016-06-11 21:15:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
// Info contains the information of a page
|
|
|
|
type Info 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-25 20:57:10 +00:00
|
|
|
Config *config.Config
|
2016-06-11 21:15:42 +00:00
|
|
|
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.
|
2016-06-25 20:57:10 +00:00
|
|
|
func (i Info) BreadcrumbMap() map[string]string {
|
2016-06-11 09:37:36 +00:00
|
|
|
result := map[string]string{}
|
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
if len(i.Path) == 0 {
|
2016-06-11 09:37:36 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip trailing slash
|
2016-06-25 20:57:10 +00:00
|
|
|
lpath := i.Path
|
2016-06-11 09:37:36 +00:00
|
|
|
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
|
2016-06-25 20:57:10 +00:00
|
|
|
func (i Info) PreviousLink() string {
|
2016-06-26 15:52:15 +00:00
|
|
|
path := strings.TrimSuffix(i.Path, "/")
|
|
|
|
path = strings.TrimPrefix(path, "/")
|
2016-07-01 14:46:06 +00:00
|
|
|
path = i.Config.AbsoluteURL + "/" + path
|
2016-06-26 15:52:15 +00:00
|
|
|
path = path[0 : len(path)-len(i.Name)]
|
2016-06-14 17:19:10 +00:00
|
|
|
|
2016-07-01 14:46:06 +00:00
|
|
|
if len(path) < len(i.Config.AbsoluteURL+"/") {
|
2016-06-26 15:52:15 +00:00
|
|
|
return ""
|
2016-06-14 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 15:52:15 +00:00
|
|
|
return path
|
2016-06-14 17:19:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:27:13 +00:00
|
|
|
// PrintAsHTML formats the page in HTML and executes the template
|
|
|
|
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
|
2016-06-23 22:21:44 +00:00
|
|
|
// Create the functions map, then the template, check for erros and
|
|
|
|
// execute the template if there aren't errors
|
|
|
|
functions := template.FuncMap{
|
2016-07-18 15:12:02 +00:00
|
|
|
"Defined": variables.Defined,
|
2016-08-04 20:49:49 +00:00
|
|
|
"CSS": func(s string) template.CSS {
|
|
|
|
return template.CSS(s)
|
2016-08-03 14:31:43 +00:00
|
|
|
},
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 21:37:52 +00:00
|
|
|
if p.Minimal {
|
|
|
|
templates = append(templates, "actions", "minimal")
|
|
|
|
} else {
|
2016-07-01 09:46:09 +00:00
|
|
|
templates = append(templates, "actions", "base")
|
2016-06-30 21:37:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:27:13 +00:00
|
|
|
var tpl *template.Template
|
|
|
|
|
|
|
|
// For each template, add it to the the tpl variable
|
|
|
|
for i, t := range templates {
|
|
|
|
// Get the template from the assets
|
2016-06-25 20:57:10 +00:00
|
|
|
page, err := assets.Asset("templates/" + t + ".tmpl")
|
2016-06-23 21:27:13 +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-23 21:27:13 +00:00
|
|
|
// If it's the first iteration, creates a new template and add the
|
|
|
|
// functions map
|
|
|
|
if i == 0 {
|
2016-06-23 22:21:44 +00:00
|
|
|
tpl, err = template.New(t).Funcs(functions).Parse(string(page))
|
2016-06-23 21:27:13 +00:00
|
|
|
} else {
|
|
|
|
tpl, err = tpl.Parse(string(page))
|
|
|
|
}
|
2016-06-21 14:02:30 +00:00
|
|
|
|
2016-06-23 21:27:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2016-06-21 14:02:30 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 20:21:32 +00:00
|
|
|
buf := &bytes.Buffer{}
|
2016-06-23 21:27:13 +00:00
|
|
|
err := tpl.Execute(buf, 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 20:21:32 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
_, err = buf.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
|
|
|
}
|