2017-06-24 11:12:15 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hacdias/filemanager/variables"
|
|
|
|
)
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
// functions contains the non-standard functions that are available
|
|
|
|
// to use on the HTML templates.
|
|
|
|
var functions = template.FuncMap{
|
|
|
|
"Defined": variables.FieldInStruct,
|
|
|
|
"CSS": func(s string) template.CSS {
|
|
|
|
return template.CSS(s)
|
|
|
|
},
|
|
|
|
"Marshal": func(v interface{}) template.JS {
|
|
|
|
a, _ := json.Marshal(v)
|
|
|
|
return template.JS(a)
|
|
|
|
},
|
|
|
|
"EncodeBase64": func(s string) string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(s))
|
|
|
|
},
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
// page contains the information needed to fill a page template.
|
|
|
|
type page struct {
|
2017-06-25 13:24:26 +00:00
|
|
|
minimal bool
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
IsDir bool
|
2017-06-25 14:24:16 +00:00
|
|
|
User *User
|
2017-06-25 13:24:26 +00:00
|
|
|
PrefixURL string
|
|
|
|
BaseURL string
|
|
|
|
WebDavURL string
|
|
|
|
Data interface{}
|
|
|
|
Editor bool
|
|
|
|
Display string
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
// breadcrumbItem contains the Name and the URL of a breadcrumb piece.
|
|
|
|
type breadcrumbItem struct {
|
2017-06-24 11:12:15 +00:00
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BreadcrumbMap returns p.Path where every element is a map
|
|
|
|
// of URLs and path segment names.
|
2017-06-25 12:00:33 +00:00
|
|
|
func (p page) BreadcrumbMap() []breadcrumbItem {
|
|
|
|
result := []breadcrumbItem{}
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
if len(p.Path) == 0 {
|
2017-06-24 11:12:15 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip trailing slash
|
2017-06-25 12:00:33 +00:00
|
|
|
lpath := p.Path
|
2017-06-24 11:12:15 +00:00
|
|
|
if lpath[len(lpath)-1] == '/' {
|
|
|
|
lpath = lpath[:len(lpath)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(lpath, "/")
|
|
|
|
for i, part := range parts {
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 && part == "" {
|
2017-06-25 12:00:33 +00:00
|
|
|
result = append([]breadcrumbItem{{
|
2017-06-24 11:12:15 +00:00
|
|
|
Name: "/",
|
|
|
|
URL: "/",
|
|
|
|
}}, result...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
result = append([]breadcrumbItem{{
|
2017-06-24 11:12:15 +00:00
|
|
|
Name: part,
|
|
|
|
URL: strings.Join(parts[:i+1], "/") + "/",
|
|
|
|
}}, result...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-06-25 12:00:33 +00:00
|
|
|
// PreviousLink returns the URL of the previous folder.
|
|
|
|
func (p page) PreviousLink() string {
|
|
|
|
path := strings.TrimSuffix(p.Path, "/")
|
2017-06-24 11:12:15 +00:00
|
|
|
path = strings.TrimPrefix(path, "/")
|
2017-06-25 13:24:26 +00:00
|
|
|
path = p.BaseURL + "/" + path
|
2017-06-25 12:00:33 +00:00
|
|
|
path = path[0 : len(path)-len(p.Name)]
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-06-25 13:24:26 +00:00
|
|
|
if len(path) < len(p.BaseURL+"/") {
|
2017-06-24 11:12:15 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintAsHTML formats the page in HTML and executes the template
|
2017-06-25 20:54:01 +00:00
|
|
|
func (p page) PrintAsHTML(w http.ResponseWriter, m *FileManager, templates ...string) (int, error) {
|
2017-06-26 19:38:40 +00:00
|
|
|
templates = append(templates, "actions")
|
2017-06-25 21:43:43 +00:00
|
|
|
templates = append(templates, "templates")
|
|
|
|
|
2017-06-25 13:24:26 +00:00
|
|
|
if p.minimal {
|
2017-06-24 11:12:15 +00:00
|
|
|
templates = append(templates, "minimal")
|
|
|
|
} else {
|
|
|
|
templates = append(templates, "base")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-06-25 20:54:01 +00:00
|
|
|
Page, err := getTemplate(m, t+".tmpl")
|
2017-06-24 11:12:15 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's the first iteration, creates a new template and add the
|
|
|
|
// functions map
|
|
|
|
if i == 0 {
|
|
|
|
tpl, err = template.New(t).Funcs(functions).Parse(Page)
|
|
|
|
} else {
|
|
|
|
tpl, err = tpl.Parse(string(Page))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-25 12:00:33 +00:00
|
|
|
err := tpl.Execute(buf, p)
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
_, err = buf.WriteTo(w)
|
|
|
|
return http.StatusOK, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintAsJSON prints the current Page information in JSON
|
2017-06-25 12:00:33 +00:00
|
|
|
func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) {
|
|
|
|
marsh, err := json.MarshalIndent(p.Data, "", " ")
|
2017-06-24 11:12:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if _, err := w.Write(marsh); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2017-06-25 20:54:01 +00:00
|
|
|
|
|
|
|
func getTemplate(m *FileManager, template string) (string, error) {
|
|
|
|
if m.Assets.Templates != nil {
|
|
|
|
if tpl, err := m.Assets.Templates.String(template); err == nil {
|
|
|
|
return tpl, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Assets.baseTemplates.String(template)
|
|
|
|
}
|