Updates; instable

Former-commit-id: e3f4a16286e135cb643f36fc9518a760dca82333 [formerly ffd101f980b04f10bc4bc99e43bcc3ff962993a4] [formerly 15e574f9edbd1579a12424d51aa9926e0dbd84dd [formerly ae8e97a43e]]
Former-commit-id: a2db52a130b73a1ac235630d87797edd0c38c756 [formerly bf85e68f90f75cad611b90545ce7415123de84bd]
Former-commit-id: f9915ecf27cc6c2456eb867a7d3a8beb29531e35
This commit is contained in:
Henrique Dias 2017-07-29 10:54:05 +01:00
parent 0d453229d9
commit 00be85db13
5 changed files with 212 additions and 168 deletions

View File

@ -28,7 +28,7 @@
<link rel="<%= chunk.initial?'preload':'prefetch' %>" href="{{ .BaseURL }}/<%= file %>" as="<%= file.match(/\.css$/)?'style':'script' %>"><% }}} %> <link rel="<%= chunk.initial?'preload':'prefetch' %>" href="{{ .BaseURL }}/<%= file %>" as="<%= file.match(/\.css$/)?'style':'script' %>"><% }}} %>
<!-- Plugins info --> <!-- Plugins info -->
<script>{{ range $index, $plugin := .Plugins }}{{ JS $plugin.JavaScript }}{{ end}}</script> <script>{{ .JavaScript }}</script>
<style> <style>
#loading { #loading {
position: fixed; position: fixed;

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
@ -123,26 +122,12 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
} }
// Try to find the Hugo executable path. // Try to find the Hugo executable path.
if hugo.Exe, err = exec.LookPath("hugo"); err != nil { if err = hugo.Find(); err != nil {
return nil, plugins.ErrHugoNotFound
}
err = m.RegisterPlugin("hugo", hugo)
if err != nil {
return nil, err return nil, err
} }
err = m.RegisterEventType("before_publish") // Attaches Hugo plugin to this file manager instance.
if err != nil { err = m.ActivatePlugin("hugo", hugo)
return nil, err
}
err = m.RegisterEventType("after_publish")
if err != nil {
return nil, err
}
err = m.RegisterPermission("allowPublish", true)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,6 +20,7 @@ var (
errUserNotExist = errors.New("user does not exist") errUserNotExist = errors.New("user does not exist")
errEmptyRequest = errors.New("request body is empty") errEmptyRequest = errors.New("request body is empty")
errEmptyPassword = errors.New("password is empty") errEmptyPassword = errors.New("password is empty")
plugins = map[string]Plugin{}
) )
// FileManager is a file manager instance. It should be creating using the // FileManager is a file manager instance. It should be creating using the
@ -53,8 +54,8 @@ type FileManager struct {
// A map of events to a slice of commands. // A map of events to a slice of commands.
Commands map[string][]string Commands map[string][]string
// The plugins that have been plugged in. // The options of the plugins that have been plugged into this instance.
Plugins map[string]Plugin Plugins map[string]interface{}
} }
// Command is a command function. // Command is a command function.
@ -115,15 +116,32 @@ type Regexp struct {
regexp *regexp.Regexp regexp *regexp.Regexp
} }
// Plugin is a File Manager plugin. type Plugin struct {
type Plugin interface { JavaScript string
// The JavaScript that will be injected into the main page. CommandEvents []string
JavaScript() string Permissions []Permission
Handler PluginHandler
Options interface{}
}
type Permission struct {
Name string
Value bool
}
type PluginHandler interface {
// If the Plugin returns (0, nil), the executation of File Manager will procced as usual. // If the Plugin returns (0, nil), the executation of File Manager will procced as usual.
// Otherwise it will stop. // Otherwise it will stop.
BeforeAPI(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) Before(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error)
AfterAPI(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) After(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error)
}
func RegisterPlugin(name string, plugin Plugin) {
if _, ok := plugins[name]; ok {
panic(name + " plugin is already registred")
}
plugins[name] = plugin
} }
// DefaultUser is used on New, when no 'base' user is provided. // DefaultUser is used on New, when no 'base' user is provided.
@ -148,8 +166,8 @@ func New(database string, base User) (*FileManager, error) {
// map and Assets box. // map and Assets box.
m := &FileManager{ m := &FileManager{
Users: map[string]*User{}, Users: map[string]*User{},
Plugins: map[string]interface{}{},
assets: rice.MustFindBox("./assets/dist"), assets: rice.MustFindBox("./assets/dist"),
Plugins: map[string]Plugin{},
} }
// Tries to open a database on the location provided. This // Tries to open a database on the location provided. This
@ -265,41 +283,59 @@ func (m *FileManager) SetBaseURL(url string) {
m.BaseURL = strings.TrimSuffix(url, "/") m.BaseURL = strings.TrimSuffix(url, "/")
} }
// RegisterPlugin registers a plugin to a File Manager instance and // ActivatePlugin activates a plugin to a File Manager instance and
// loads its options from the database. // loads its options from the database.
func (m *FileManager) RegisterPlugin(name string, plugin Plugin) error { func (m *FileManager) ActivatePlugin(name string, options interface{}) error {
var plugin Plugin
if p, ok := plugins[name]; !ok {
plugin = p
return errors.New(name + " plugin is not registred")
}
if _, ok := m.Plugins[name]; ok { if _, ok := m.Plugins[name]; ok {
return errors.New("Plugin already registred") return errors.New(name + " plugin is already activated")
} }
err := m.db.Get("plugins", name, &plugin) err := m.db.Get("plugins", name, &plugin)
if err != nil && err == storm.ErrNotFound { if err != nil && err == storm.ErrNotFound {
err = m.db.Set("plugins", name, plugin) err = m.db.Set("plugin", name, plugin)
} }
if err != nil { if err != nil {
return err return err
} }
m.Plugins[name] = plugin // Register the command event hooks.
for _, evt := range plugin.CommandEvents {
if _, ok := m.Commands[evt]; ok {
continue
}
m.Commands[evt] = []string{}
}
err = m.db.Set("config", "commands", m.Commands)
if err != nil {
return err
}
// Register the user permissions.
for _, perm := range plugin.Permissions {
err = m.registerPermission(perm.Name, perm.Value)
if err != nil {
return err
}
}
m.Plugins[name] = options
return nil return nil
} }
// RegisterEventType registers a new event type which can be triggered using Runner // registerPermission registers a new user permission and adds it to every
// function.
func (m *FileManager) RegisterEventType(name string) error {
if _, ok := m.Commands[name]; ok {
return nil
}
m.Commands[name] = []string{}
return m.db.Set("config", "commands", m.Commands)
}
// RegisterPermission registers a new user permission and adds it to every
// user with it default's 'value'. If the user is an admin, it will // user with it default's 'value'. If the user is an admin, it will
// be true. // be true.
func (m *FileManager) RegisterPermission(name string, value bool) error { func (m *FileManager) registerPermission(name string, value bool) error {
if _, ok := m.DefaultUser.Permissions[name]; ok { if _, ok := m.DefaultUser.Permissions[name]; ok {
return nil return nil
} }

25
http.go
View File

@ -107,8 +107,8 @@ func apiHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int,
return http.StatusForbidden, nil return http.StatusForbidden, nil
} }
for _, p := range c.FM.Plugins { for p := range c.FM.Plugins {
code, err := p.BeforeAPI(c, w, r) code, err := plugins[p].Handler.Before(c, w, r)
if code != 0 || err != nil { if code != 0 || err != nil {
return code, err return code, err
} }
@ -150,8 +150,8 @@ func apiHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int,
return code, err return code, err
} }
for _, p := range c.FM.Plugins { for p := range c.FM.Plugins {
code, err := p.AfterAPI(c, w, r) code, err := plugins[p].Handler.After(c, w, r)
if code != 0 || err != nil { if code != 0 || err != nil {
return code, err return code, err
} }
@ -194,18 +194,17 @@ func splitURL(path string) (string, string) {
// renderFile renders a file using a template with some needed variables. // renderFile renders a file using a template with some needed variables.
func renderFile(w http.ResponseWriter, file string, contentType string, c *RequestContext) (int, error) { func renderFile(w http.ResponseWriter, file string, contentType string, c *RequestContext) (int, error) {
functions := template.FuncMap{ tpl := template.Must(template.New("file").Parse(file))
"JS": func(s string) template.JS {
return template.JS(s)
},
}
tpl := template.Must(template.New("file").Funcs(functions).Parse(file))
w.Header().Set("Content-Type", contentType+"; charset=utf-8") w.Header().Set("Content-Type", contentType+"; charset=utf-8")
var javascript = ""
for name := range c.FM.Plugins {
javascript += plugins[name].JavaScript + "\n"
}
err := tpl.Execute(w, map[string]interface{}{ err := tpl.Execute(w, map[string]interface{}{
"BaseURL": c.FM.RootURL(), "BaseURL": c.FM.RootURL(),
"Plugins": c.FM.Plugins, "JavaScript": template.JS(javascript),
}) })
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -15,6 +16,20 @@ import (
"github.com/robfig/cron" "github.com/robfig/cron"
) )
func init() {
filemanager.RegisterPlugin("hugo", filemanager.Plugin{
JavaScript: rice.MustFindBox("./assets/").MustString("hugo.js"),
CommandEvents: []string{"before_publish", "after_publish"},
Permissions: []filemanager.Permission{
{
Name: "allowPublish",
Value: true,
},
},
Handler: &hugo{},
})
}
var ( var (
ErrHugoNotFound = errors.New("It seems that tou don't have 'hugo' on your PATH") ErrHugoNotFound = errors.New("It seems that tou don't have 'hugo' on your PATH")
ErrUnsupportedFileType = errors.New("The type of the provided file isn't supported for this action") ErrUnsupportedFileType = errors.New("The type of the provided file isn't supported for this action")
@ -34,117 +49,13 @@ type Hugo struct {
CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."` CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
} }
func (h Hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { func (h *Hugo) Find() error {
// If we are using the 'magic url' for the settings, we should redirect the var err error
// request for the acutual path. if h.Exe, err = exec.LookPath("hugo"); err != nil {
if r.URL.Path == "/settings/" || r.URL.Path == "/settings" { return ErrHugoNotFound
var frontmatter string
var err error
if _, err = os.Stat(filepath.Join(h.Root, "config.yaml")); err == nil {
frontmatter = "yaml"
}
if _, err = os.Stat(filepath.Join(h.Root, "config.json")); err == nil {
frontmatter = "json"
}
if _, err = os.Stat(filepath.Join(h.Root, "config.toml")); err == nil {
frontmatter = "toml"
}
r.URL.Path = "/config." + frontmatter
} }
// From here on, we only care about 'hugo' router so we can bypass return nil
// the others.
if c.Router != "hugo" {
return 0, nil
}
// If we are not using HTTP Post, we shall return Method Not Allowed
// since we are only working with this method.
if r.Method != http.MethodPost {
return http.StatusMethodNotAllowed, nil
}
// If we are creating a file built from an archetype.
if r.Header.Get("Archetype") != "" {
if !c.User.AllowNew {
return http.StatusForbidden, nil
}
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
archetype := r.Header.Get("archetype")
ext := filepath.Ext(filename)
// If the request isn't for a markdown file, we can't
// handle it.
if ext != ".markdown" && ext != ".md" {
return http.StatusBadRequest, ErrUnsupportedFileType
}
// Tries to create a new file based on this archetype.
args := []string{"new", filename, "--kind", archetype}
if err := Run(h.Exe, args, h.Root); err != nil {
return http.StatusInternalServerError, err
}
// Writes the location of the new file to the Header.
w.Header().Set("Location", "/files/content/"+filename)
return http.StatusCreated, nil
}
// If we are trying to regenerate the website.
if r.Header.Get("Regenerate") == "true" {
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
// Before save command handler.
if err := c.FM.Runner("before_publish", filename); err != nil {
return http.StatusInternalServerError, err
}
// We only run undraft command if it is a file.
if strings.HasSuffix(filename, ".md") && strings.HasSuffix(filename, ".markdown") {
if err := h.undraft(filename); err != nil {
return http.StatusInternalServerError, err
}
}
// Regenerates the file
h.run(false)
// Executed the before publish command.
if err := c.FM.Runner("before_publish", filename); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
if r.Header.Get("Schedule") != "" {
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
return h.schedule(c, w, r)
}
return http.StatusNotFound, nil
}
func (h Hugo) AfterAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
return 0, nil
}
func (h Hugo) JavaScript() string {
return rice.MustFindBox("./assets/").MustString("hugo.js")
} }
// run runs Hugo with the define arguments. // run runs Hugo with the define arguments.
@ -202,3 +113,116 @@ func (h Hugo) undraft(file string) error {
return nil return nil
} }
type hugo struct{}
func (h hugo) Before(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
o := c.FM.Plugins["hugo"].(Hugo)
// If we are using the 'magic url' for the settings, we should redirect the
// request for the acutual path.
if r.URL.Path == "/settings/" || r.URL.Path == "/settings" {
var frontmatter string
var err error
if _, err = os.Stat(filepath.Join(o.Root, "config.yaml")); err == nil {
frontmatter = "yaml"
}
if _, err = os.Stat(filepath.Join(o.Root, "config.json")); err == nil {
frontmatter = "json"
}
if _, err = os.Stat(filepath.Join(o.Root, "config.toml")); err == nil {
frontmatter = "toml"
}
r.URL.Path = "/config." + frontmatter
}
// From here on, we only care about 'hugo' router so we can bypass
// the others.
if c.Router != "hugo" {
return 0, nil
}
// If we are not using HTTP Post, we shall return Method Not Allowed
// since we are only working with this method.
if r.Method != http.MethodPost {
return http.StatusMethodNotAllowed, nil
}
// If we are creating a file built from an archetype.
if r.Header.Get("Archetype") != "" {
if !c.User.AllowNew {
return http.StatusForbidden, nil
}
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
archetype := r.Header.Get("archetype")
ext := filepath.Ext(filename)
// If the request isn't for a markdown file, we can't
// handle it.
if ext != ".markdown" && ext != ".md" {
return http.StatusBadRequest, ErrUnsupportedFileType
}
// Tries to create a new file based on this archetype.
args := []string{"new", filename, "--kind", archetype}
if err := Run(o.Exe, args, o.Root); err != nil {
return http.StatusInternalServerError, err
}
// Writes the location of the new file to the Header.
w.Header().Set("Location", "/files/content/"+filename)
return http.StatusCreated, nil
}
// If we are trying to regenerate the website.
if r.Header.Get("Regenerate") == "true" {
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
// Before save command handler.
if err := c.FM.Runner("before_publish", filename); err != nil {
return http.StatusInternalServerError, err
}
// We only run undraft command if it is a file.
if strings.HasSuffix(filename, ".md") && strings.HasSuffix(filename, ".markdown") {
if err := o.undraft(filename); err != nil {
return http.StatusInternalServerError, err
}
}
// Regenerates the file
o.run(false)
// Executed the before publish command.
if err := c.FM.Runner("before_publish", filename); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
if r.Header.Get("Schedule") != "" {
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
return o.schedule(c, w, r)
}
return http.StatusNotFound, nil
}
func (h hugo) After(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
return 0, nil
}