From 5fce287cd23a5318da4bae441180c54861cfea27 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 22 Oct 2016 12:07:19 +0100 Subject: [PATCH] move some stuff --- file/info.go | 30 ++++++++++++++++++++++-- frontmatter/frontmatter.go | 6 ++--- {file => handlers}/editor.go | 31 +++---------------------- handlers/listing.go | 4 ++-- handlers/single.go | 6 ++--- page/page.go | 4 ++-- utils/{ => errors}/errors.go | 2 +- utils/{ => variables}/types.go | 2 +- utils/{ => variables}/variables.go | 2 +- utils/{ => variables}/variables_test.go | 2 +- 10 files changed, 45 insertions(+), 44 deletions(-) rename {file => handlers}/editor.go (78%) rename utils/{ => errors}/errors.go (95%) rename utils/{ => variables}/types.go (94%) rename utils/{ => variables}/variables.go (98%) rename utils/{ => variables}/variables_test.go (97%) diff --git a/file/info.go b/file/info.go index 9ffaa66d..18060e38 100644 --- a/file/info.go +++ b/file/info.go @@ -10,7 +10,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hacdias/caddy-filemanager/config" - "github.com/hacdias/caddy-filemanager/utils" + "github.com/hacdias/caddy-filemanager/utils/errors" ) // Info contains the information about a particular file or directory @@ -41,7 +41,7 @@ func GetInfo(url *url.URL, c *config.Config, u *config.User) (*Info, int, error) i.FileInfo, err = os.Stat(i.Path) if err != nil { - return i, utils.ErrorToHTTPCode(err, false), err + return i, errors.ErrorToHTTPCode(err, false), err } return i, 0, nil @@ -74,6 +74,32 @@ func (i Info) HumanModTime(format string) string { return i.ModTime().Format(format) } +// CanBeEdited checks if the extension of a file is supported by the editor +func (i Info) CanBeEdited() bool { + if i.Type == "text" { + return true + } + + extensions := [...]string{ + "md", "markdown", "mdown", "mmark", + "asciidoc", "adoc", "ad", + "rst", + ".json", ".toml", ".yaml", + ".css", ".sass", ".scss", + ".js", + ".html", + ".txt", + } + + for _, extension := range extensions { + if strings.HasSuffix(i.Name(), extension) { + return true + } + } + + return false +} + func simplifyMediaType(name string) string { if strings.HasPrefix(name, "video") { return "video" diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go index 18edbc5b..d50df10e 100644 --- a/frontmatter/frontmatter.go +++ b/frontmatter/frontmatter.go @@ -13,7 +13,7 @@ import ( "gopkg.in/yaml.v2" "github.com/BurntSushi/toml" - "github.com/hacdias/caddy-filemanager/utils" + "github.com/hacdias/caddy-filemanager/utils/variables" "github.com/spf13/cast" ) @@ -126,9 +126,9 @@ func rawToPretty(config interface{}, parent *Block) *Content { } for name, element := range cnf { - if utils.IsMap(element) { + if variables.IsMap(element) { objects = append(objects, handleObjects(element, parent, name)) - } else if utils.IsSlice(element) { + } else if variables.IsSlice(element) { arrays = append(arrays, handleArrays(element, parent, name)) } else { if name == "title" && parent.Name == mainName { diff --git a/file/editor.go b/handlers/editor.go similarity index 78% rename from file/editor.go rename to handlers/editor.go index e0cd3ba8..f9343538 100644 --- a/file/editor.go +++ b/handlers/editor.go @@ -1,10 +1,11 @@ -package file +package handlers import ( "bytes" "path/filepath" "strings" + "github.com/hacdias/caddy-filemanager/file" "github.com/hacdias/caddy-filemanager/frontmatter" "github.com/spf13/hugo/parser" ) @@ -18,7 +19,7 @@ type Editor struct { } // GetEditor gets the editor based on a FileInfo struct -func (i *Info) GetEditor() (*Editor, error) { +func GetEditor(i *file.Info) (*Editor, error) { // Create a new editor variable and set the mode editor := new(Editor) editor.Mode = strings.TrimPrefix(filepath.Ext(i.Name()), ".") @@ -81,29 +82,3 @@ func (i *Info) GetEditor() (*Editor, error) { return editor, nil } - -// CanBeEdited checks if the extension of a file is supported by the editor -func (i Info) CanBeEdited() bool { - if i.Type == "text" { - return true - } - - extensions := [...]string{ - "md", "markdown", "mdown", "mmark", - "asciidoc", "adoc", "ad", - "rst", - ".json", ".toml", ".yaml", - ".css", ".sass", ".scss", - ".js", - ".html", - ".txt", - } - - for _, extension := range extensions { - if strings.HasSuffix(i.Name(), extension) { - return true - } - } - - return false -} diff --git a/handlers/listing.go b/handlers/listing.go index 13b36d33..28d022db 100644 --- a/handlers/listing.go +++ b/handlers/listing.go @@ -9,7 +9,7 @@ import ( "github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/file" "github.com/hacdias/caddy-filemanager/page" - "github.com/hacdias/caddy-filemanager/utils" + "github.com/hacdias/caddy-filemanager/utils/errors" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -20,7 +20,7 @@ func ServeListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *c // Loads the content of the directory listing, err := file.GetListing(u, i.VirtualPath, r.URL.Path) if err != nil { - return utils.ErrorToHTTPCode(err, true), err + return errors.ErrorToHTTPCode(err, true), err } listing.Context = httpserver.Context{ diff --git a/handlers/single.go b/handlers/single.go index 93dd3b85..2c22f885 100644 --- a/handlers/single.go +++ b/handlers/single.go @@ -6,7 +6,7 @@ import ( "github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/file" "github.com/hacdias/caddy-filemanager/page" - "github.com/hacdias/caddy-filemanager/utils" + "github.com/hacdias/caddy-filemanager/utils/errors" ) // ServeSingle serves a single file in an editor (if it is editable), shows the @@ -14,7 +14,7 @@ import ( func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User, i *file.Info) (int, error) { err := i.Read() if err != nil { - return utils.ErrorToHTTPCode(err, true), err + return errors.ErrorToHTTPCode(err, true), err } p := &page.Page{ @@ -29,7 +29,7 @@ func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *co } if i.CanBeEdited() && u.AllowEdit { - p.Data, err = i.GetEditor() + p.Data, err = GetEditor(i) if err != nil { return http.StatusInternalServerError, err } diff --git a/page/page.go b/page/page.go index 2b2ca46e..dd7c60a3 100644 --- a/page/page.go +++ b/page/page.go @@ -11,7 +11,7 @@ import ( "github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/config" - "github.com/hacdias/caddy-filemanager/utils" + "github.com/hacdias/caddy-filemanager/utils/variables" ) // Page contains the informations and functions needed to show the Page @@ -81,7 +81,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro // Create the functions map, then the template, check for erros and // execute the template if there aren't errors functions := template.FuncMap{ - "Defined": utils.Defined, + "Defined": variables.Defined, "CSS": func(s string) template.CSS { return template.CSS(s) }, diff --git a/utils/errors.go b/utils/errors/errors.go similarity index 95% rename from utils/errors.go rename to utils/errors/errors.go index 35841d85..ad4a2743 100644 --- a/utils/errors.go +++ b/utils/errors/errors.go @@ -1,4 +1,4 @@ -package utils +package errors import ( "net/http" diff --git a/utils/types.go b/utils/variables/types.go similarity index 94% rename from utils/types.go rename to utils/variables/types.go index 7e6b408b..ee43dad3 100644 --- a/utils/types.go +++ b/utils/variables/types.go @@ -1,4 +1,4 @@ -package utils +package variables import "reflect" diff --git a/utils/variables.go b/utils/variables/variables.go similarity index 98% rename from utils/variables.go rename to utils/variables/variables.go index 28f8383a..7a0168b4 100644 --- a/utils/variables.go +++ b/utils/variables/variables.go @@ -1,4 +1,4 @@ -package utils +package variables import ( "errors" diff --git a/utils/variables_test.go b/utils/variables/variables_test.go similarity index 97% rename from utils/variables_test.go rename to utils/variables/variables_test.go index 7122478d..ec76d459 100644 --- a/utils/variables_test.go +++ b/utils/variables/variables_test.go @@ -1,4 +1,4 @@ -package utils +package variables import "testing"