diff --git a/_assets/templates/base.tmpl b/_assets/templates/base.tmpl index 579e01bd..e72a29f6 100644 --- a/_assets/templates/base.tmpl +++ b/_assets/templates/base.tmpl @@ -1,30 +1,30 @@ -{{ $absURL := .Config.AbsoluteURL }} +{{ $absURL := .BaseURL }} {{.Name}} - - - + + + {{- if ne .User.StyleSheet "" -}} {{- end -}} - + {{- if .IsDir }} - + {{- else }} - - - + + + {{- end }} diff --git a/editor.go b/editor.go index cfc3eeee..57946578 100644 --- a/editor.go +++ b/editor.go @@ -11,8 +11,8 @@ import ( "github.com/spf13/hugo/parser" ) -// Editor contains the information for the editor page -type Editor struct { +// editor contains the information to fill the editor template. +type editor struct { Class string Mode string Visual bool @@ -23,12 +23,12 @@ type Editor struct { } } -// GetEditor gets the editor based on a Info struct -func GetEditor(r *http.Request, i *fileInfo) (*Editor, error) { +// getEditor gets the editor based on a Info struct +func (i *fileInfo) getEditor(r *http.Request) (*editor, error) { var err error // Create a new editor variable and set the mode - e := new(Editor) + e := &editor{} e.Mode = editorMode(i.Name) e.Class = editorClass(e.Mode) diff --git a/http.go b/http.go index 1e12cd35..0e833eee 100644 --- a/http.go +++ b/http.go @@ -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 // 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 { return serveAssets(w, r, c) } diff --git a/http_assets.go b/http_assets.go index 5e2df954..c2046a44 100644 --- a/http_assets.go +++ b/http_assets.go @@ -8,13 +8,13 @@ import ( "strings" ) -// AssetsURL is the url of the assets -const AssetsURL = "/_internal" +// assetsURL is the url where static assets are served. +const assetsURL = "/_internal" // Serve provides the needed assets for the front-end func serveAssets(w http.ResponseWriter, r *http.Request, m *FileManager) (int, error) { // 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 err error diff --git a/http_listing.go b/http_listing.go index 2cb28315..3d430e4a 100644 --- a/http_listing.go +++ b/http_listing.go @@ -78,17 +78,19 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *use }) p := &page{ - Minimal: r.Header.Get("Minimal") == "true", - Name: listing.Name, - Path: i.VirtualPath, - IsDir: true, - User: u, - Config: c, - Display: displayMode, - Data: listing, + minimal: r.Header.Get("Minimal") == "true", + Name: listing.Name, + Path: i.VirtualPath, + IsDir: true, + User: u, + PrefixURL: c.PrefixURL, + BaseURL: c.AbsoluteURL(), + 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', diff --git a/http_single.go b/http_single.go index 55943e28..853daf58 100644 --- a/http_single.go +++ b/http_single.go @@ -15,12 +15,14 @@ func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user } p := &page{ - Name: i.Name, - Path: i.VirtualPath, - IsDir: false, - Data: i, - User: u, - Config: c, + Name: i.Name, + Path: i.VirtualPath, + IsDir: false, + Data: i, + User: u, + PrefixURL: c.PrefixURL, + BaseURL: c.AbsoluteURL(), + WebDavURL: c.AbsoluteWebDavURL(), } // 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 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") } diff --git a/info.go b/info.go index 51cc38fc..a99fe3f0 100644 --- a/info.go +++ b/info.go @@ -15,20 +15,28 @@ import ( // fileInfo contains the information about a particular file or directory. type fileInfo struct { - Name string - 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 - + // Used to store the file's content temporarily. 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 @@ -75,41 +83,44 @@ var textExtensions = [...]string{ ".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi", } -// RetrieveFileType obtains the mimetype and a simplified internal Type -// using the first 512 bytes from the file. +// RetrieveFileType obtains the mimetype and converts it to a simple +// type nomenclature. 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() if err != nil { 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" return nil } - if strings.HasPrefix(i.Mimetype, "audio") { + if strings.HasPrefix(mimetype, "audio") { i.Type = "audio" return nil } - if strings.HasPrefix(i.Mimetype, "image") { + if strings.HasPrefix(mimetype, "image") { i.Type = "image" return nil } - if strings.HasPrefix(i.Mimetype, "text") { + if strings.HasPrefix(mimetype, "text") { i.Type = "text" return nil } - if strings.HasPrefix(i.Mimetype, "application/javascript") { + if strings.HasPrefix(mimetype, "application/javascript") { i.Type = "text" return nil } @@ -141,7 +152,7 @@ func (i *fileInfo) Read() error { return nil } -// StringifyContent returns the string version of Raw +// StringifyContent returns a string with the file content. func (i fileInfo) StringifyContent() string { return string(i.content) } diff --git a/page.go b/page.go index a873dca4..4d91ecb3 100644 --- a/page.go +++ b/page.go @@ -9,6 +9,7 @@ import ( "net/http" "strings" + rice "github.com/GeertJohan/go.rice" "github.com/hacdias/filemanager/variables" ) @@ -30,15 +31,17 @@ var functions = template.FuncMap{ // 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 + minimal bool + Name string + Path string + IsDir bool + User *user + PrefixURL string + BaseURL string + WebDavURL string + Data interface{} + Editor bool + Display string } // 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 { path := strings.TrimSuffix(p.Path, "/") path = strings.TrimPrefix(path, "/") - path = p.Config.AbsoluteURL() + "/" + path + path = p.BaseURL + "/" + path path = path[0 : len(path)-len(p.Name)] - if len(path) < len(p.Config.AbsoluteURL()+"/") { + if len(path) < len(p.BaseURL+"/") { return "" } @@ -100,8 +103,8 @@ func (p page) PreviousLink() string { } // PrintAsHTML formats the page in HTML and executes the template -func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { - if p.Minimal { +func (p page) PrintAsHTML(w http.ResponseWriter, box *rice.Box, templates ...string) (int, error) { + if p.minimal { templates = append(templates, "minimal") } else { 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 i, t := range templates { // 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 if err != nil {