add comments and unexport some fields

pull/144/head
Henrique Dias 2017-06-25 14:24:26 +01:00
parent b93158b291
commit fffbcc7098
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
8 changed files with 94 additions and 76 deletions

View File

@ -1,30 +1,30 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
{{ $absURL := .Config.AbsoluteURL }} {{ $absURL := .BaseURL }}
<head> <head>
<title>{{.Name}}</title> <title>{{.Name}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="stylesheet" href="{{ .Config.AbsoluteURL }}/_internal/css/normalize.css"> <link rel="stylesheet" href="{{ .BaseURL }}/_internal/css/normalize.css">
<link rel="stylesheet" href="{{ .Config.AbsoluteURL }}/_internal/css/fonts.css"> <link rel="stylesheet" href="{{ .BaseURL }}/_internal/css/fonts.css">
<link rel="stylesheet" href="{{ .Config.AbsoluteURL }}/_internal/css/styles.css"> <link rel="stylesheet" href="{{ .BaseURL }}/_internal/css/styles.css">
{{- if ne .User.StyleSheet "" -}} {{- if ne .User.StyleSheet "" -}}
<style>{{ CSS .User.StyleSheet }}</style> <style>{{ CSS .User.StyleSheet }}</style>
{{- end -}} {{- end -}}
<script> <script>
var user = JSON.parse('{{ Marshal .User }}'), var user = JSON.parse('{{ Marshal .User }}'),
webdavURL = "{{.Config.AbsoluteWebDavURL }}", webdavURL = "{{ .WebDavURL }}",
baseURL = "{{.Config.AbsoluteURL}}", baseURL = "{{.BaseURL}}",
prefixURL = "{{ .Config.PrefixURL }}"; prefixURL = "{{ .PrefixURL }}";
</script> </script>
<script src="{{ .Config.AbsoluteURL }}/_internal/js/common.js" defer></script> <script src="{{ .BaseURL }}/_internal/js/common.js" defer></script>
{{- if .IsDir }} {{- if .IsDir }}
<script src="{{ .Config.AbsoluteURL }}/_internal/js/listing.js" defer></script> <script src="{{ .BaseURL }}/_internal/js/listing.js" defer></script>
{{- else }} {{- else }}
<script src="{{ .Config.AbsoluteURL }}/_internal/js/vendor/ace/src-min/ace.js" defer></script> <script src="{{ .BaseURL }}/_internal/js/vendor/ace/src-min/ace.js" defer></script>
<script src="{{ .Config.AbsoluteURL }}/_internal/js/vendor/form2js.js" defer></script> <script src="{{ .BaseURL }}/_internal/js/vendor/form2js.js" defer></script>
<script src="{{ .Config.AbsoluteURL }}/_internal/js/editor.js" defer></script> <script src="{{ .BaseURL }}/_internal/js/editor.js" defer></script>
{{- end }} {{- end }}
</head> </head>
<body> <body>

View File

@ -11,8 +11,8 @@ import (
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
) )
// Editor contains the information for the editor page // editor contains the information to fill the editor template.
type Editor struct { type editor struct {
Class string Class string
Mode string Mode string
Visual bool Visual bool
@ -23,12 +23,12 @@ type Editor struct {
} }
} }
// GetEditor gets the editor based on a Info struct // getEditor gets the editor based on a Info struct
func GetEditor(r *http.Request, i *fileInfo) (*Editor, error) { func (i *fileInfo) getEditor(r *http.Request) (*editor, error) {
var err error var err error
// Create a new editor variable and set the mode // Create a new editor variable and set the mode
e := new(Editor) e := &editor{}
e.Mode = editorMode(i.Name) e.Mode = editorMode(i.Name)
e.Class = editorClass(e.Mode) e.Class = editorClass(e.Mode)

View File

@ -26,7 +26,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
// Checks if the URL matches the Assets URL. Returns the asset if the // Checks if the URL matches the Assets URL. Returns the asset if the
// method is GET and Status Forbidden otherwise. // method is GET and Status Forbidden otherwise.
if matchURL(r.URL.Path, c.baseURL+AssetsURL) { if matchURL(r.URL.Path, c.baseURL+assetsURL) {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
return serveAssets(w, r, c) return serveAssets(w, r, c)
} }

View File

@ -8,13 +8,13 @@ import (
"strings" "strings"
) )
// AssetsURL is the url of the assets // assetsURL is the url where static assets are served.
const AssetsURL = "/_internal" const assetsURL = "/_internal"
// Serve provides the needed assets for the front-end // Serve provides the needed assets for the front-end
func serveAssets(w http.ResponseWriter, r *http.Request, m *FileManager) (int, error) { func serveAssets(w http.ResponseWriter, r *http.Request, m *FileManager) (int, error) {
// gets the filename to be used with Assets function // gets the filename to be used with Assets function
filename := strings.Replace(r.URL.Path, m.baseURL+AssetsURL, "", 1) filename := strings.Replace(r.URL.Path, m.baseURL+assetsURL, "", 1)
var file []byte var file []byte
var err error var err error

View File

@ -78,17 +78,19 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *use
}) })
p := &page{ p := &page{
Minimal: r.Header.Get("Minimal") == "true", minimal: r.Header.Get("Minimal") == "true",
Name: listing.Name, Name: listing.Name,
Path: i.VirtualPath, Path: i.VirtualPath,
IsDir: true, IsDir: true,
User: u, User: u,
Config: c, PrefixURL: c.PrefixURL,
Display: displayMode, BaseURL: c.AbsoluteURL(),
Data: listing, WebDavURL: c.AbsoluteWebDavURL(),
Display: displayMode,
Data: listing,
} }
return p.PrintAsHTML(w, "listing") return p.PrintAsHTML(w, c.Assets.Templates, "listing")
} }
// handleSortOrder gets and stores for a Listing the 'sort' and 'order', // handleSortOrder gets and stores for a Listing the 'sort' and 'order',

View File

@ -15,12 +15,14 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user
} }
p := &page{ p := &page{
Name: i.Name, Name: i.Name,
Path: i.VirtualPath, Path: i.VirtualPath,
IsDir: false, IsDir: false,
Data: i, Data: i,
User: u, User: u,
Config: c, PrefixURL: c.PrefixURL,
BaseURL: c.AbsoluteURL(),
WebDavURL: c.AbsoluteWebDavURL(),
} }
// If the request accepts JSON, we send the file information. // If the request accepts JSON, we send the file information.
@ -41,8 +43,8 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
return p.PrintAsHTML(w, "frontmatter", "editor") return p.PrintAsHTML(w, c.Assets.Templates, "frontmatter", "editor")
} }
return p.PrintAsHTML(w, "single") return p.PrintAsHTML(w, c.Assets.Templates, "single")
} }

59
info.go
View File

@ -15,20 +15,28 @@ import (
// fileInfo contains the information about a particular file or directory. // fileInfo contains the information about a particular file or directory.
type fileInfo struct { type fileInfo struct {
Name string // Used to store the file's content temporarily.
Size int64
URL string
Extension string
ModTime time.Time
Mode os.FileMode
IsDir bool
Path string // Relative path to Current Working Directory
VirtualPath string // Relative path to user's virtual File System
Mimetype string
Type string
UserAllowed bool // Indicates if the user has enough permissions
content []byte content []byte
Name string
Size int64
URL string
Extension string
ModTime time.Time
Mode os.FileMode
IsDir bool
// Absolute path.
Path string
// Relative path to user's virtual File System.
VirtualPath string
// Indicates the file content type: video, text, image, music or blob.
Type string
// Indicates if the user has enough permissions to edit the file.
UserAllowed bool
} }
// getInfo gets the file information and, in case of error, returns the // getInfo gets the file information and, in case of error, returns the
@ -75,41 +83,44 @@ var textExtensions = [...]string{
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi", ".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
} }
// RetrieveFileType obtains the mimetype and a simplified internal Type // RetrieveFileType obtains the mimetype and converts it to a simple
// using the first 512 bytes from the file. // type nomenclature.
func (i *fileInfo) RetrieveFileType() error { func (i *fileInfo) RetrieveFileType() error {
i.Mimetype = mime.TypeByExtension(i.Extension) // Tries to get the file mimetype using its extension.
mimetype := mime.TypeByExtension(i.Extension)
if i.Mimetype == "" { if mimetype == "" {
err := i.Read() err := i.Read()
if err != nil { if err != nil {
return err return err
} }
i.Mimetype = http.DetectContentType(i.content) // Tries to get the file mimetype using its first
// 512 bytes.
mimetype = http.DetectContentType(i.content)
} }
if strings.HasPrefix(i.Mimetype, "video") { if strings.HasPrefix(mimetype, "video") {
i.Type = "video" i.Type = "video"
return nil return nil
} }
if strings.HasPrefix(i.Mimetype, "audio") { if strings.HasPrefix(mimetype, "audio") {
i.Type = "audio" i.Type = "audio"
return nil return nil
} }
if strings.HasPrefix(i.Mimetype, "image") { if strings.HasPrefix(mimetype, "image") {
i.Type = "image" i.Type = "image"
return nil return nil
} }
if strings.HasPrefix(i.Mimetype, "text") { if strings.HasPrefix(mimetype, "text") {
i.Type = "text" i.Type = "text"
return nil return nil
} }
if strings.HasPrefix(i.Mimetype, "application/javascript") { if strings.HasPrefix(mimetype, "application/javascript") {
i.Type = "text" i.Type = "text"
return nil return nil
} }
@ -141,7 +152,7 @@ func (i *fileInfo) Read() error {
return nil return nil
} }
// StringifyContent returns the string version of Raw // StringifyContent returns a string with the file content.
func (i fileInfo) StringifyContent() string { func (i fileInfo) StringifyContent() string {
return string(i.content) return string(i.content)
} }

31
page.go
View File

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"strings" "strings"
rice "github.com/GeertJohan/go.rice"
"github.com/hacdias/filemanager/variables" "github.com/hacdias/filemanager/variables"
) )
@ -30,15 +31,17 @@ var functions = template.FuncMap{
// page contains the information needed to fill a page template. // page contains the information needed to fill a page template.
type page struct { type page struct {
Minimal bool minimal bool
Name string Name string
Path string Path string
IsDir bool IsDir bool
User *user User *user
Config *FileManager PrefixURL string
Data interface{} BaseURL string
Editor bool WebDavURL string
Display string Data interface{}
Editor bool
Display string
} }
// breadcrumbItem contains the Name and the URL of a breadcrumb piece. // breadcrumbItem contains the Name and the URL of a breadcrumb piece.
@ -89,10 +92,10 @@ func (p page) BreadcrumbMap() []breadcrumbItem {
func (p page) PreviousLink() string { func (p page) PreviousLink() string {
path := strings.TrimSuffix(p.Path, "/") path := strings.TrimSuffix(p.Path, "/")
path = strings.TrimPrefix(path, "/") path = strings.TrimPrefix(path, "/")
path = p.Config.AbsoluteURL() + "/" + path path = p.BaseURL + "/" + path
path = path[0 : len(path)-len(p.Name)] path = path[0 : len(path)-len(p.Name)]
if len(path) < len(p.Config.AbsoluteURL()+"/") { if len(path) < len(p.BaseURL+"/") {
return "" return ""
} }
@ -100,8 +103,8 @@ func (p page) PreviousLink() string {
} }
// PrintAsHTML formats the page in HTML and executes the template // PrintAsHTML formats the page in HTML and executes the template
func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { func (p page) PrintAsHTML(w http.ResponseWriter, box *rice.Box, templates ...string) (int, error) {
if p.Minimal { if p.minimal {
templates = append(templates, "minimal") templates = append(templates, "minimal")
} else { } else {
templates = append(templates, "base") templates = append(templates, "base")
@ -112,7 +115,7 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
// For each template, add it to the the tpl variable // For each template, add it to the the tpl variable
for i, t := range templates { for i, t := range templates {
// Get the template from the assets // Get the template from the assets
Page, err := p.Config.Assets.Templates.String(t + ".tmpl") Page, err := box.String(t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist // Check if there is some error. If so, the template doesn't exist
if err != nil { if err != nil {