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

View File

@ -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)

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
// 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)
}

View File

@ -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

View File

@ -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',

View File

@ -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")
}

59
info.go
View File

@ -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)
}

31
page.go
View File

@ -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 {