2017-08-14 17:35:25 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-20 08:31:24 +00:00
|
|
|
"github.com/asdine/storm"
|
2017-08-14 17:35:25 +00:00
|
|
|
"github.com/hacdias/filemanager"
|
2017-08-20 08:31:24 +00:00
|
|
|
"github.com/hacdias/filemanager/bolt"
|
|
|
|
"github.com/hacdias/filemanager/staticgen"
|
2017-08-14 17:35:25 +00:00
|
|
|
"github.com/hacdias/fileutils"
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2017-08-20 08:31:24 +00:00
|
|
|
"github.com/spf13/viper"
|
2017-08-14 17:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parse ...
|
|
|
|
func Parse(c *caddy.Controller, plugin string) ([]*filemanager.FileManager, error) {
|
|
|
|
var (
|
|
|
|
configs []*filemanager.FileManager
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for c.Next() {
|
2017-08-20 08:31:24 +00:00
|
|
|
u := &filemanager.User{
|
2017-08-14 17:35:25 +00:00
|
|
|
Locale: "en",
|
|
|
|
AllowCommands: true,
|
|
|
|
AllowEdit: true,
|
|
|
|
AllowNew: true,
|
|
|
|
AllowPublish: true,
|
|
|
|
Commands: []string{"git", "svn", "hg"},
|
|
|
|
CSS: "",
|
|
|
|
Rules: []*filemanager.Rule{{
|
|
|
|
Regex: true,
|
|
|
|
Allow: false,
|
|
|
|
Regexp: &filemanager.Regexp{Raw: "\\/\\..+"},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := "/"
|
|
|
|
scope := "."
|
|
|
|
database := ""
|
|
|
|
noAuth := false
|
|
|
|
|
|
|
|
if plugin != "" {
|
|
|
|
baseURL = "/admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the baseURL and scope
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
if plugin == "" {
|
|
|
|
if len(args) >= 1 {
|
|
|
|
baseURL = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
scope = args[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(args) >= 1 {
|
|
|
|
scope = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
baseURL = args[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "database":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
database = c.Val()
|
|
|
|
case "locale":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Locale = c.Val()
|
|
|
|
case "allow_commands":
|
|
|
|
if !c.NextArg() {
|
|
|
|
u.AllowCommands = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u.AllowCommands, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "allow_edit":
|
|
|
|
if !c.NextArg() {
|
|
|
|
u.AllowEdit = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u.AllowEdit, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "allow_new":
|
|
|
|
if !c.NextArg() {
|
|
|
|
u.AllowNew = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u.AllowNew, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "allow_publish":
|
|
|
|
if !c.NextArg() {
|
|
|
|
u.AllowPublish = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u.AllowPublish, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "commands":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Commands = strings.Split(c.Val(), " ")
|
|
|
|
case "css":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
file := c.Val()
|
|
|
|
css, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.CSS = string(css)
|
|
|
|
case "no_auth":
|
|
|
|
if !c.NextArg() {
|
|
|
|
noAuth = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
noAuth, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
caddyConf := httpserver.GetConfig(c)
|
|
|
|
|
|
|
|
path := filepath.Join(caddy.AssetsPath(), "filemanager")
|
|
|
|
err := os.MkdirAll(path, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a database path and it is not absolute,
|
|
|
|
// it will be relative to Caddy folder.
|
|
|
|
if !filepath.IsAbs(database) && database != "" {
|
|
|
|
database = filepath.Join(path, database)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no database path on the settings,
|
|
|
|
// store one in .caddy/filemanager/name.db.
|
|
|
|
if database == "" {
|
|
|
|
// The name of the database is the hashed value of a string composed
|
|
|
|
// by the host, address path and the baseurl of this File Manager
|
|
|
|
// instance.
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(caddyConf.Addr.Host + caddyConf.Addr.Path + baseURL))
|
|
|
|
sha := hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
database = filepath.Join(path, sha+".db")
|
|
|
|
|
|
|
|
fmt.Println("[WARNING] A database is going to be created for your File Manager instace at " + database +
|
|
|
|
". It is highly recommended that you set the 'database' option to '" + sha + ".db'\n")
|
|
|
|
}
|
|
|
|
|
2017-08-20 08:31:24 +00:00
|
|
|
u.Scope = scope
|
2017-08-14 17:35:25 +00:00
|
|
|
u.FileSystem = fileutils.Dir(scope)
|
2017-08-20 08:31:24 +00:00
|
|
|
|
|
|
|
db, err := storm.Open(database)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &filemanager.FileManager{
|
|
|
|
NoAuth: viper.GetBool("NoAuth"),
|
|
|
|
BaseURL: "",
|
|
|
|
PrefixURL: "",
|
|
|
|
DefaultUser: u,
|
|
|
|
Store: &filemanager.Store{
|
|
|
|
Config: bolt.ConfigStore{DB: db},
|
|
|
|
Users: bolt.UsersStore{DB: db},
|
|
|
|
Share: bolt.ShareStore{DB: db},
|
|
|
|
},
|
|
|
|
NewFS: func(scope string) filemanager.FileSystem {
|
|
|
|
return fileutils.Dir(scope)
|
|
|
|
},
|
|
|
|
}
|
2017-08-14 17:35:25 +00:00
|
|
|
|
2017-08-20 08:55:45 +00:00
|
|
|
err = m.Setup()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
switch plugin {
|
|
|
|
case "hugo":
|
|
|
|
// Initialize the default settings for Hugo.
|
2017-08-20 08:31:24 +00:00
|
|
|
hugo := &staticgen.Hugo{
|
2017-08-14 17:35:25 +00:00
|
|
|
Root: scope,
|
|
|
|
Public: filepath.Join(scope, "public"),
|
|
|
|
Args: []string{},
|
|
|
|
CleanPublic: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attaches Hugo plugin to this file manager instance.
|
2017-08-20 08:31:24 +00:00
|
|
|
err = m.Attach(hugo)
|
2017-08-14 17:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "jekyll":
|
|
|
|
// Initialize the default settings for Jekyll.
|
2017-08-20 08:31:24 +00:00
|
|
|
jekyll := &staticgen.Jekyll{
|
2017-08-14 17:35:25 +00:00
|
|
|
Root: scope,
|
|
|
|
Public: filepath.Join(scope, "_site"),
|
|
|
|
Args: []string{},
|
|
|
|
CleanPublic: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attaches Hugo plugin to this file manager instance.
|
2017-08-20 08:31:24 +00:00
|
|
|
err = m.Attach(jekyll)
|
2017-08-14 17:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.NoAuth = noAuth
|
|
|
|
m.SetBaseURL(baseURL)
|
|
|
|
m.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, "/"))
|
|
|
|
|
|
|
|
configs = append(configs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|