unexport page

pull/144/head
Henrique Dias 2017-06-25 13:00:33 +01:00
parent 4fa7426b99
commit 1ba9608a9c
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
5 changed files with 98 additions and 108 deletions

View File

@ -50,8 +50,8 @@ const errTemplate = `<!DOCTYPE html>
</div>
</html>`
// PrintErrorHTML prints the error page
func PrintErrorHTML(w http.ResponseWriter, code int, err error) (int, error) {
// 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)

View File

@ -112,7 +112,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
// Checks if the User is allowed to access this file
if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.BaseURL)) {
if r.Method == http.MethodGet {
return PrintErrorHTML(
return htmlError(
w, http.StatusForbidden,
errors.New("You don't have permission to access this page"),
)
@ -134,7 +134,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
fi, err = GetInfo(r.URL, c, user)
if err != nil {
if r.Method == http.MethodGet {
return PrintErrorHTML(w, code, err)
return htmlError(w, code, err)
}
code = errorToHTTP(err, false)
return code, err
@ -162,7 +162,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
}
if err != nil {
code, err = PrintErrorHTML(w, code, err)
code, err = htmlError(w, code, err)
}
return code, err

View File

@ -77,20 +77,18 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *use
Secure: r.TLS != nil,
})
page := &Page{
p := &page{
Minimal: r.Header.Get("Minimal") == "true",
PageInfo: &PageInfo{
Name: listing.Name,
Path: i.VirtualPath,
IsDir: true,
User: u,
Config: c,
Display: displayMode,
Data: listing,
},
Name: listing.Name,
Path: i.VirtualPath,
IsDir: true,
User: u,
Config: c,
Display: displayMode,
Data: listing,
}
return page.PrintAsHTML(w, "listing")
return p.PrintAsHTML(w, "listing")
}
// handleSortOrder gets and stores for a Listing the 'sort' and 'order',

View File

@ -14,15 +14,13 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user
return errorToHTTP(err, true), err
}
p := &Page{
PageInfo: &PageInfo{
Name: i.Name,
Path: i.VirtualPath,
IsDir: false,
Data: i,
User: u,
Config: c,
},
p := &page{
Name: i.Name,
Path: i.VirtualPath,
IsDir: false,
Data: i,
User: u,
Config: c,
}
// If the request accepts JSON, we send the file information.

160
page.go
View File

@ -12,84 +12,8 @@ import (
"github.com/hacdias/filemanager/variables"
)
// Page contains the informations and functions needed to show the Page
type Page struct {
*PageInfo
Minimal bool
}
// PageInfo contains the information of a Page
type PageInfo struct {
Name string
Path string
IsDir bool
User *user
Config *FileManager
Data interface{}
Editor bool
Display string
}
// BreadcrumbMapItem ...
type BreadcrumbMapItem struct {
Name string
URL string
}
// BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names.
func (i PageInfo) BreadcrumbMap() []BreadcrumbMapItem {
result := []BreadcrumbMapItem{}
if len(i.Path) == 0 {
return result
}
// skip trailing slash
lpath := i.Path
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 == "" {
result = append([]BreadcrumbMapItem{{
Name: "/",
URL: "/",
}}, result...)
continue
}
result = append([]BreadcrumbMapItem{{
Name: part,
URL: strings.Join(parts[:i+1], "/") + "/",
}}, result...)
}
return result
}
// PreviousLink returns the path of the previous folder
func (i PageInfo) PreviousLink() string {
path := strings.TrimSuffix(i.Path, "/")
path = strings.TrimPrefix(path, "/")
path = i.Config.AbsoluteURL() + "/" + path
path = path[0 : len(path)-len(i.Name)]
if len(path) < len(i.Config.AbsoluteURL()+"/") {
return ""
}
return path
}
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
// 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 {
@ -104,9 +28,79 @@ var functions = template.FuncMap{
},
}
// PrintAsHTML formats the page in HTML and executes the template
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
// page contains the information needed to fill a page template.
type page struct {
Minimal bool
Name string
Path string
IsDir bool
User *user
Config *FileManager
Data interface{}
Editor bool
Display string
}
// breadcrumbItem contains the Name and the URL of a breadcrumb piece.
type breadcrumbItem struct {
Name string
URL string
}
// BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names.
func (p page) BreadcrumbMap() []breadcrumbItem {
result := []breadcrumbItem{}
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 == len(parts)-1 {
continue
}
if i == 0 && part == "" {
result = append([]breadcrumbItem{{
Name: "/",
URL: "/",
}}, result...)
continue
}
result = append([]breadcrumbItem{{
Name: part,
URL: strings.Join(parts[:i+1], "/") + "/",
}}, result...)
}
return result
}
// PreviousLink returns the URL of the previous folder.
func (p page) PreviousLink() string {
path := strings.TrimSuffix(p.Path, "/")
path = strings.TrimPrefix(path, "/")
path = p.Config.AbsoluteURL() + "/" + path
path = path[0 : len(path)-len(p.Name)]
if len(path) < len(p.Config.AbsoluteURL()+"/") {
return ""
}
return path
}
// PrintAsHTML formats the page in HTML and executes the template
func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
if p.Minimal {
templates = append(templates, "minimal")
} else {
@ -141,7 +135,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
}
buf := &bytes.Buffer{}
err := tpl.Execute(buf, p.PageInfo)
err := tpl.Execute(buf, p)
if err != nil {
return http.StatusInternalServerError, err
@ -153,8 +147,8 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
}
// PrintAsJSON prints the current Page information in JSON
func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.MarshalIndent(p.PageInfo.Data, "", " ")
func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.MarshalIndent(p.Data, "", " ")
if err != nil {
return http.StatusInternalServerError, err
}