filebrowser/plugins/hugo.go

229 lines
5.8 KiB
Go
Raw Normal View History

2017-07-29 09:14:34 +00:00
package plugins
import (
2017-07-29 09:14:34 +00:00
"errors"
"log"
"net/http"
"os"
2017-07-29 09:54:05 +00:00
"os/exec"
"path/filepath"
"strings"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/hacdias/filemanager"
2017-07-28 08:59:31 +00:00
"github.com/hacdias/varutils"
"github.com/robfig/cron"
)
2017-07-29 09:54:05 +00:00
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{},
})
}
2017-07-29 09:14:34 +00:00
var (
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")
)
// Hugo is a hugo (https://gohugo.io) plugin.
type Hugo struct {
// Website root
2017-07-12 14:28:35 +00:00
Root string `description:"The relative or absolute path to the place where your website is located."`
// Public folder
2017-07-12 14:28:35 +00:00
Public string `description:"The relative or absolute path to the public folder."`
// Hugo executable path
2017-07-12 14:28:35 +00:00
Exe string `description:"The absolute path to the Hugo executable or the command to execute."`
// Hugo arguments
2017-07-12 14:28:35 +00:00
Args []string `description:"The arguments to run when running Hugo"`
// Indicates if we should clean public before a new publish.
2017-07-12 14:28:35 +00:00
CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
}
2017-07-29 09:54:05 +00:00
func (h *Hugo) Find() error {
var err error
if h.Exe, err = exec.LookPath("hugo"); err != nil {
return ErrHugoNotFound
}
return nil
}
// run runs Hugo with the define arguments.
func (h Hugo) run(force bool) {
// If the CleanPublic option is enabled, clean it.
if h.CleanPublic {
os.RemoveAll(h.Public)
}
// Prevent running if watching is enabled
if b, pos := varutils.StringInSlice("--watch", h.Args); b && !force {
if len(h.Args) > pos && h.Args[pos+1] != "false" {
return
}
if len(h.Args) == pos+1 {
return
}
}
if err := Run(h.Exe, h.Args, h.Root); err != nil {
log.Println(err)
}
}
// schedule schedules a post to be published later.
func (h Hugo) schedule(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
t, err := time.Parse("2006-01-02T15:04", r.Header.Get("Schedule"))
path := filepath.Join(string(c.User.FileSystem), r.URL.Path)
path = filepath.Clean(path)
if err != nil {
return http.StatusInternalServerError, err
}
scheduler := cron.New()
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
if err := h.undraft(path); err != nil {
log.Printf(err.Error())
return
}
h.run(false)
})
scheduler.Start()
return http.StatusOK, nil
}
func (h Hugo) undraft(file string) error {
args := []string{"undraft", file}
if err := Run(h.Exe, args, h.Root); err != nil && !strings.Contains(err.Error(), "not a Draft") {
return err
}
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
2017-07-29 09:54:05 +00:00
if _, err = os.Stat(filepath.Join(o.Root, "config.yaml")); err == nil {
frontmatter = "yaml"
}
2017-07-29 09:54:05 +00:00
if _, err = os.Stat(filepath.Join(o.Root, "config.json")); err == nil {
frontmatter = "json"
}
2017-07-29 09:54:05 +00:00
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
}
2017-07-12 14:28:35 +00:00
// 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
}
2017-07-12 14:28:35 +00:00
// If we are creating a file built from an archetype.
if r.Header.Get("Archetype") != "" {
2017-07-14 07:30:56 +00:00
if !c.User.AllowNew {
return http.StatusForbidden, nil
}
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
archetype := r.Header.Get("archetype")
2017-07-12 14:28:35 +00:00
ext := filepath.Ext(filename)
// If the request isn't for a markdown file, we can't
// handle it.
if ext != ".markdown" && ext != ".md" {
2017-07-29 09:14:34 +00:00
return http.StatusBadRequest, ErrUnsupportedFileType
}
2017-07-12 14:28:35 +00:00
// Tries to create a new file based on this archetype.
args := []string{"new", filename, "--kind", archetype}
2017-07-29 09:54:05 +00:00
if err := Run(o.Exe, args, o.Root); err != nil {
return http.StatusInternalServerError, err
}
2017-07-12 14:28:35 +00:00
// Writes the location of the new file to the Header.
w.Header().Set("Location", "/files/content/"+filename)
return http.StatusCreated, nil
}
2017-07-12 14:28:35 +00:00
// If we are trying to regenerate the website.
if r.Header.Get("Regenerate") == "true" {
2017-07-14 07:30:56 +00:00
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
2017-07-12 14:28:35 +00:00
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
// Before save command handler.
2017-07-12 14:28:35 +00:00
if err := c.FM.Runner("before_publish", filename); err != nil {
return http.StatusInternalServerError, err
}
2017-07-12 14:28:35 +00:00
// We only run undraft command if it is a file.
2017-07-29 09:14:34 +00:00
if strings.HasSuffix(filename, ".md") && strings.HasSuffix(filename, ".markdown") {
2017-07-29 09:54:05 +00:00
if err := o.undraft(filename); err != nil {
2017-07-12 14:28:35 +00:00
return http.StatusInternalServerError, err
}
2017-07-29 09:14:34 +00:00
}
2017-07-12 14:28:35 +00:00
// Regenerates the file
2017-07-29 09:54:05 +00:00
o.run(false)
2017-07-12 14:28:35 +00:00
// 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") != "" {
2017-07-14 07:30:56 +00:00
if !c.User.Permissions["allowPublish"] {
return http.StatusForbidden, nil
}
2017-07-29 09:54:05 +00:00
return o.schedule(c, w, r)
}
return http.StatusNotFound, nil
}
2017-07-29 09:54:05 +00:00
func (h hugo) After(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
return 0, nil
}