move some stuff

This commit is contained in:
Henrique Dias 2016-10-22 12:07:19 +01:00
parent df888b604a
commit 5fce287cd2
10 changed files with 45 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package utils
package errors
import (
"net/http"

View File

@ -1,4 +1,4 @@
package utils
package variables
import "reflect"

View File

@ -1,4 +1,4 @@
package utils
package variables
import (
"errors"

View File

@ -1,4 +1,4 @@
package utils
package variables
import "testing"