mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
6d853d63ed
Former-commit-id: b6c33f0ae36e7caa8cf813eeb6f8fdeb554659e9 [formerly 4d62c13f0b7e5b7610a0c9037d646c2005e9ba88] [formerly 1e9ded0427d81084c92a68d5eddb8c7620fb189c [formerly ae8eaf96c4
]]
Former-commit-id: 76699219a6d176777f4478eab5141bba5a613a17 [formerly bb698073213359bebce84ea196b30e9467f448c0]
Former-commit-id: 538927b5a6fad024eb062cf746068372c384b883
259 lines
6.4 KiB
Go
259 lines
6.4 KiB
Go
package plugins
|
|
|
|
import (
|
|
"errors"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hacdias/filemanager"
|
|
"github.com/hacdias/varutils"
|
|
"github.com/robfig/cron"
|
|
)
|
|
|
|
func init() {
|
|
filemanager.RegisterPlugin("hugo", filemanager.Plugin{
|
|
JavaScript: hugoJavaScript,
|
|
CommandEvents: []string{"before_publish", "after_publish"},
|
|
BeforeAPI: beforeAPI,
|
|
Handlers: map[string]filemanager.PluginHandler{
|
|
"/preview": previewHandler,
|
|
},
|
|
Permissions: []filemanager.Permission{
|
|
{
|
|
Name: "allowPublish",
|
|
Value: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
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
|
|
Root string `name:"Website Root"`
|
|
// Public folder
|
|
Public string `name:"Public Directory"`
|
|
// Hugo executable path
|
|
Exe string `name:"Hugo Executable"`
|
|
// Hugo arguments
|
|
Args []string `name:"Hugo Arguments"`
|
|
// Indicates if we should clean public before a new publish.
|
|
CleanPublic bool `name:"Clean Public"`
|
|
// previewPath is the temporary path for a preview
|
|
previewPath string
|
|
}
|
|
|
|
// Find finds the hugo executable in the path.
|
|
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
|
|
}
|
|
|
|
func beforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
o := c.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.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.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 previewHandler(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
h := c.Plugins["hugo"].(*Hugo)
|
|
|
|
// Get a new temporary path if there is none.
|
|
if h.previewPath == "" {
|
|
path, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
h.previewPath = path
|
|
}
|
|
|
|
// Build the arguments to execute Hugo: change the base URL,
|
|
// build the drafts and update the destination.
|
|
args := h.Args
|
|
args = append(args, "--baseURL", c.RootURL()+"/preview/")
|
|
args = append(args, "--buildDrafts")
|
|
args = append(args, "--destination", h.previewPath)
|
|
|
|
// Builds the preview.
|
|
if err := Run(h.Exe, args, h.Root); err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
// Serves the temporary path with the preview.
|
|
http.FileServer(http.Dir(h.previewPath)).ServeHTTP(w, r)
|
|
return 0, nil
|
|
}
|