mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
Some updates
This commit is contained in:
parent
476d20606d
commit
45b4ec5a43
@ -1,7 +1,6 @@
|
|||||||
package hugo
|
package hugo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -17,21 +16,18 @@ import (
|
|||||||
|
|
||||||
type hugo struct {
|
type hugo struct {
|
||||||
// Website root
|
// Website root
|
||||||
Root string
|
Root string `description:"The relative or absolute path to the place where your website is located."`
|
||||||
// Public folder
|
// Public folder
|
||||||
Public string
|
Public string `description:"The relative or absolute path to the public folder."`
|
||||||
// Hugo executable path
|
// Hugo executable path
|
||||||
Exe string
|
Exe string `description:"The absolute path to the Hugo executable or the command to execute."`
|
||||||
// Hugo arguments
|
// Hugo arguments
|
||||||
Args []string
|
Args []string `description:"The arguments to run when running Hugo"`
|
||||||
// Indicates if we should clean public before a new publish.
|
// Indicates if we should clean public before a new publish.
|
||||||
CleanPublic bool
|
CleanPublic bool `description:"Indicates if the public folder should be cleaned before publishing the website."`
|
||||||
// A map of events to a slice of commands.
|
|
||||||
Commands map[string][]string
|
|
||||||
|
|
||||||
// AllowPublish
|
// TODO: AllowPublish
|
||||||
|
// TODO: admin interface to cgange options
|
||||||
javascript string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
@ -63,45 +59,60 @@ func (h hugo) BeforeAPI(c *filemanager.RequestContext, w http.ResponseWriter, r
|
|||||||
return 0, nil
|
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 {
|
if r.Method != http.MethodPost {
|
||||||
return http.StatusMethodNotAllowed, nil
|
return http.StatusMethodNotAllowed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we are creating a file built from an archetype.
|
||||||
if r.Header.Get("Archetype") != "" {
|
if r.Header.Get("Archetype") != "" {
|
||||||
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||||
filename = filepath.Clean(filename)
|
|
||||||
filename = strings.TrimPrefix(filename, "/")
|
filename = strings.TrimPrefix(filename, "/")
|
||||||
archetype := r.Header.Get("archetype")
|
archetype := r.Header.Get("archetype")
|
||||||
|
|
||||||
if !strings.HasSuffix(filename, ".md") && !strings.HasSuffix(filename, ".markdown") {
|
ext := filepath.Ext(filename)
|
||||||
return http.StatusBadRequest, errors.New("Your file must be markdown")
|
|
||||||
|
// 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}
|
args := []string{"new", filename, "--kind", archetype}
|
||||||
|
|
||||||
if err := Run(h.Exe, args, h.Root); err != nil {
|
if err := Run(h.Exe, args, h.Root); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writes the location of the new file to the Header.
|
||||||
w.Header().Set("Location", "/files/content/"+filename)
|
w.Header().Set("Location", "/files/content/"+filename)
|
||||||
return http.StatusCreated, nil
|
return http.StatusCreated, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we are trying to regenerate the website.
|
||||||
if r.Header.Get("Regenerate") == "true" {
|
if r.Header.Get("Regenerate") == "true" {
|
||||||
|
filename := filepath.Join(string(c.User.FileSystem), r.URL.Path)
|
||||||
|
filename = strings.TrimPrefix(filename, "/")
|
||||||
|
|
||||||
// Before save command handler.
|
// Before save command handler.
|
||||||
path := filepath.Clean(filepath.Join(string(c.User.FileSystem), r.URL.Path))
|
if err := c.FM.Runner("before_publish", filename); err != nil {
|
||||||
if err := c.FM.Runner("before_publish", path); err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{"undraft", path}
|
// We only run undraft command if it is a file.
|
||||||
if err := Run(h.Exe, args, h.Root); err != nil {
|
if !strings.HasSuffix(filename, "/") {
|
||||||
return http.StatusInternalServerError, err
|
args := []string{"undraft", filename}
|
||||||
|
if err := Run(h.Exe, args, h.Root); err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Regenerates the file
|
||||||
h.run(false)
|
h.run(false)
|
||||||
|
|
||||||
if err := c.FM.Runner("before_publish", path); err != nil {
|
// Executed the before publish command.
|
||||||
|
if err := c.FM.Runner("before_publish", filename); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,14 +27,36 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let newArchetype = function (data, file, type) {
|
let newArchetype = function (data, url, type) {
|
||||||
|
url = data.api.removePrefix(url)
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request = new window.XMLHttpRequest()
|
||||||
|
request.open('POST', `${data.store.state.baseURL}/api/hugo${url}`, true)
|
||||||
|
request.setRequestHeader('Authorization', `Bearer ${data.store.state.jwt}`)
|
||||||
|
request.setRequestHeader('Archetype', encodeURIComponent(type))
|
||||||
|
|
||||||
|
request.onload = () => {
|
||||||
|
if (request.status === 200) {
|
||||||
|
resolve(request.getResponseHeader('Location'))
|
||||||
|
} else {
|
||||||
|
reject(request.responseText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request.onerror = (error) => reject(error)
|
||||||
|
request.send()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let schedule = function (data, file, date) {
|
||||||
file = data.api.removePrefix(file)
|
file = data.api.removePrefix(file)
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let request = new window.XMLHttpRequest()
|
let request = new window.XMLHttpRequest()
|
||||||
request.open('POST', `${data.store.state.baseURL}/api/hugo${file}`, true)
|
request.open('POST', `${data.store.state.baseURL}/api/hugo${file}`, true)
|
||||||
request.setRequestHeader('Authorization', `Bearer ${data.store.state.jwt}`)
|
request.setRequestHeader('Authorization', `Bearer ${data.store.state.jwt}`)
|
||||||
request.setRequestHeader('Archetype', encodeURIComponent(type))
|
request.setRequestHeader('Schedule', date)
|
||||||
|
|
||||||
request.onload = () => {
|
request.onload = () => {
|
||||||
if (request.status === 200) {
|
if (request.status === 200) {
|
||||||
@ -93,7 +115,8 @@
|
|||||||
data.store.state.req.metadata !== null)
|
data.store.state.req.metadata !== null)
|
||||||
},
|
},
|
||||||
click: function (event, data, route) {
|
click: function (event, data, route) {
|
||||||
console.log('Schedule')
|
document.getElementById('save-button').click()
|
||||||
|
data.store.commit('showHover', 'schedule')
|
||||||
},
|
},
|
||||||
id: 'schedule-button',
|
id: 'schedule-button',
|
||||||
icon: 'alarm',
|
icon: 'alarm',
|
||||||
@ -145,8 +168,6 @@
|
|||||||
submit: function (event, data, route) {
|
submit: function (event, data, route) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
console.log(event)
|
|
||||||
|
|
||||||
let file = event.currentTarget.querySelector('[name="file"]').value
|
let file = event.currentTarget.querySelector('[name="file"]').value
|
||||||
let type = event.currentTarget.querySelector('[name="archetype"]').value
|
let type = event.currentTarget.querySelector('[name="archetype"]').value
|
||||||
if (type === '') type = 'default'
|
if (type === '') type = 'default'
|
||||||
@ -161,6 +182,40 @@
|
|||||||
data.store.commit('showError', error)
|
data.store.commit('showError', error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'schedule',
|
||||||
|
title: 'Schedule',
|
||||||
|
description: 'Pick a date and time to schedule the publication of this post.',
|
||||||
|
inputs: [
|
||||||
|
{
|
||||||
|
type: 'datetime-local',
|
||||||
|
name: 'date',
|
||||||
|
placeholder: 'Date'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
ok: 'Schedule',
|
||||||
|
submit: function (event, data, route) {
|
||||||
|
event.preventDefault()
|
||||||
|
data.buttons.loading('schedule')
|
||||||
|
|
||||||
|
let date = event.currentTarget.querySelector('[name="date"]').value
|
||||||
|
if (date === '') {
|
||||||
|
data.buttons.done('schedule')
|
||||||
|
data.store.commit('showError', 'The date must not be empty.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
schedule(data, route.path, date)
|
||||||
|
.then(() => {
|
||||||
|
data.buttons.done('schedule')
|
||||||
|
data.store.commit('setReload', true)
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
data.buttons.done('schedule')
|
||||||
|
data.store.commit('showError', error)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
@ -18,7 +18,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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")
|
||||||
)
|
)
|
||||||
|
|
||||||
// setup configures a new FileManager middleware instance.
|
// setup configures a new FileManager middleware instance.
|
||||||
@ -125,10 +126,6 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
|
|||||||
Public: filepath.Join(directory, "public"),
|
Public: filepath.Join(directory, "public"),
|
||||||
Args: []string{},
|
Args: []string{},
|
||||||
CleanPublic: true,
|
CleanPublic: true,
|
||||||
Commands: map[string][]string{
|
|
||||||
"before_publish": []string{},
|
|
||||||
"after_publish": []string{},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to find the Hugo executable path.
|
// Try to find the Hugo executable path.
|
||||||
@ -141,6 +138,16 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = m.RegisterEventType("before_publish")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.RegisterEventType("after_publish")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
m.SetBaseURL(admin)
|
m.SetBaseURL(admin)
|
||||||
m.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, "/"))
|
m.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, "/"))
|
||||||
configs = append(configs, m)
|
configs = append(configs, m)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package hugo
|
package hugo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -9,7 +9,11 @@ import (
|
|||||||
func Run(command string, args []string, path string) error {
|
func Run(command string, args []string, path string) error {
|
||||||
cmd := exec.Command(command, args...)
|
cmd := exec.Command(command, args...)
|
||||||
cmd.Dir = path
|
cmd.Dir = path
|
||||||
cmd.Stdout = os.Stdout
|
out, err := cmd.CombinedOutput()
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
return cmd.Run()
|
if err != nil {
|
||||||
|
return errors.New(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -114,6 +114,7 @@ type Regexp struct {
|
|||||||
type Plugin interface {
|
type Plugin interface {
|
||||||
// The JavaScript that will be injected into the main page.
|
// The JavaScript that will be injected into the main page.
|
||||||
JavaScript() string
|
JavaScript() string
|
||||||
|
|
||||||
// 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)
|
BeforeAPI(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
@ -264,6 +265,17 @@ func (m *FileManager) RegisterPlugin(name string, plugin Plugin) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterEventType registers a new event type which can be triggered using Runner
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||||
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// TODO: Handle errors here and make it compatible with http.Handler
|
// TODO: Handle errors here and make it compatible with http.Handler
|
||||||
@ -322,7 +334,15 @@ func (r *Regexp) MatchString(s string) bool {
|
|||||||
|
|
||||||
// Runner runs the commands for a certain event type.
|
// Runner runs the commands for a certain event type.
|
||||||
func (m FileManager) Runner(event string, path string) error {
|
func (m FileManager) Runner(event string, path string) error {
|
||||||
for _, command := range m.Commands[event] {
|
commands := []string{}
|
||||||
|
|
||||||
|
// Get the commands from the File Manager instance itself.
|
||||||
|
if val, ok := m.Commands[event]; ok {
|
||||||
|
commands = append(commands, val...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the commands.
|
||||||
|
for _, command := range commands {
|
||||||
args := strings.Split(command, " ")
|
args := strings.Split(command, " ")
|
||||||
nonblock := false
|
nonblock := false
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user