filebrowser/page.go

196 lines
4.4 KiB
Go
Raw Normal View History

2017-06-24 11:12:15 +00:00
package filemanager
import (
"bytes"
"encoding/json"
"html/template"
"net/http"
2017-06-27 18:00:58 +00:00
"strconv"
2017-06-24 11:12:15 +00:00
"strings"
)
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{
"CSS": func(s string) template.CSS {
return template.CSS(s)
},
"Marshal": func(v interface{}) template.JS {
a, _ := json.Marshal(v)
return template.JS(a)
},
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-28 10:45:41 +00:00
User *User `json:"-"`
BaseURL string `json:"-"`
WebDavURL string `json:"-"`
2017-06-29 09:17:35 +00:00
Kind string `json:"kind"`
Data *file `json:"data"`
2017-06-24 11:12:15 +00:00
}
2017-06-29 09:17:35 +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 {
2017-06-27 18:00:58 +00:00
// TODO: when it is preview alongside with listing!!!!!!!!!!
2017-06-25 12:00:33 +00:00
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
2017-06-29 09:17:35 +00:00
} */
2017-06-24 11:12:15 +00:00
func (p page) Render(c *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
if strings.Contains(r.Header.Get("Accept"), "application/json") {
marsh, err := json.MarshalIndent(p, "", " ")
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 0, nil
}
2017-06-24 11:12:15 +00:00
var tpl *template.Template
2017-06-28 10:45:41 +00:00
// Get the template from the assets
file, err := c.fm.templates.String("index.html")
2017-06-24 11:12:15 +00:00
2017-06-28 10:45:41 +00:00
// Check if there is some error. If so, the template doesn't exist
if err != nil {
return http.StatusInternalServerError, err
}
2017-06-24 11:12:15 +00:00
2017-06-28 10:45:41 +00:00
tpl, err = template.New("index").Funcs(functions).Parse(file)
if err != nil {
return http.StatusInternalServerError, err
2017-06-24 11:12:15 +00:00
}
buf := &bytes.Buffer{}
2017-06-28 10:45:41 +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)
if err != nil {
return http.StatusInternalServerError, err
}
return 0, nil
2017-06-24 11:12:15 +00:00
}
2017-06-27 18:00:58 +00:00
// htmlError prints the error page
func htmlError(w http.ResponseWriter, code int, err error) (int, error) {
tpl := errTemplate
tpl = strings.Replace(tpl, "TITLE", strconv.Itoa(code)+" "+http.StatusText(code), -1)
tpl = strings.Replace(tpl, "CODE", err.Error(), -1)
_, err = w.Write([]byte(tpl))
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
const errTemplate = `<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
html {
background-color: #2196f3;
color: #fff;
font-family: sans-serif;
}
code {
background-color: rgba(0,0,0,0.1);
border-radius: 5px;
padding: 1em;
display: block;
box-sizing: border-box;
}
.center {
max-width: 40em;
margin: 2em auto 0;
}
a {
text-decoration: none;
color: #eee;
font-weight: bold;
}
p {
line-height: 1.3;
}
</style>
</head>
<body>
<div class="center">
<h1>TITLE</h1>
<p>Try reloading the page or hitting the back button. If this error persists, it seems that you may have found a bug! Please create an issue at <a href="https://github.com/hacdias/caddy-filemanager/issues">hacdias/caddy-filemanager</a> repository on GitHub with the code below.</p>
<code>CODE</code>
</div>
</html>`