2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-08 16:57:24 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-08 04:24:23 +08:00
|
|
|
"log"
|
2019-01-06 06:44:33 +08:00
|
|
|
"os"
|
2019-01-08 16:57:24 +08:00
|
|
|
"path/filepath"
|
2020-10-05 15:52:27 +08:00
|
|
|
"strings"
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
"github.com/asdine/storm"
|
|
|
|
"github.com/spf13/cobra"
|
2019-01-06 13:11:15 +08:00
|
|
|
"github.com/spf13/pflag"
|
2019-01-08 16:57:24 +08:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-06-01 07:12:36 +08:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage/bolt"
|
2019-01-06 06:44:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
2019-01-07 05:06:56 +08:00
|
|
|
log.Fatal(err)
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
func mustGetString(flags *pflag.FlagSet, flag string) string {
|
|
|
|
s, err := flags.GetString(flag)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
func mustGetBool(flags *pflag.FlagSet, flag string) bool {
|
|
|
|
b, err := flags.GetBool(flag)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
func mustGetUint(flags *pflag.FlagSet, flag string) uint {
|
|
|
|
b, err := flags.GetUint(flag)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:25:39 +08:00
|
|
|
func generateKey() []byte {
|
|
|
|
k, err := settings.GenerateKey()
|
2019-01-06 20:26:48 +08:00
|
|
|
checkErr(err)
|
2019-01-12 04:25:39 +08:00
|
|
|
return k
|
2019-01-06 20:26:48 +08:00
|
|
|
}
|
2019-01-08 04:24:23 +08:00
|
|
|
|
|
|
|
type cobraFunc func(cmd *cobra.Command, args []string)
|
|
|
|
type pythonFunc func(cmd *cobra.Command, args []string, data pythonData)
|
|
|
|
|
|
|
|
type pythonConfig struct {
|
2019-01-08 04:34:44 +08:00
|
|
|
noDB bool
|
|
|
|
allowNoDB bool
|
2019-01-08 04:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type pythonData struct {
|
|
|
|
hadDB bool
|
|
|
|
store *storage.Storage
|
|
|
|
}
|
|
|
|
|
2019-01-10 05:37:47 +08:00
|
|
|
func dbExists(path string) (bool, error) {
|
|
|
|
stat, err := os.Stat(path)
|
2019-02-03 22:51:17 +08:00
|
|
|
if err == nil {
|
|
|
|
return stat.Size() != 0, nil
|
2019-01-10 05:37:47 +08:00
|
|
|
}
|
|
|
|
|
2019-02-03 22:51:17 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
d := filepath.Dir(path)
|
|
|
|
_, err = os.Stat(d)
|
|
|
|
if os.IsNotExist(err) {
|
2021-07-26 18:00:05 +08:00
|
|
|
if err := os.MkdirAll(d, 0700); err != nil { //nolint:govet,gomnd
|
2020-06-01 07:12:36 +08:00
|
|
|
return false, err
|
|
|
|
}
|
2019-02-03 22:51:17 +08:00
|
|
|
return false, nil
|
|
|
|
}
|
2019-01-10 05:37:47 +08:00
|
|
|
}
|
|
|
|
|
2019-02-03 22:51:17 +08:00
|
|
|
return false, err
|
2019-01-10 05:37:47 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
func python(fn pythonFunc, cfg pythonConfig) cobraFunc {
|
|
|
|
return func(cmd *cobra.Command, args []string) {
|
|
|
|
data := pythonData{hadDB: true}
|
|
|
|
|
2019-01-09 04:10:27 +08:00
|
|
|
path := getParam(cmd.Flags(), "database")
|
2019-01-10 05:37:47 +08:00
|
|
|
exists, err := dbExists(path)
|
2019-01-08 04:24:23 +08:00
|
|
|
|
2019-01-10 05:37:47 +08:00
|
|
|
if err != nil {
|
2019-01-08 04:24:23 +08:00
|
|
|
panic(err)
|
2019-01-10 05:37:47 +08:00
|
|
|
} else if exists && cfg.noDB {
|
2019-01-08 04:24:23 +08:00
|
|
|
log.Fatal(path + " already exists")
|
2019-01-10 05:37:47 +08:00
|
|
|
} else if !exists && !cfg.noDB && !cfg.allowNoDB {
|
|
|
|
log.Fatal(path + " does not exist. Please run 'filebrowser config init' first.")
|
2019-01-08 04:24:23 +08:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:37:47 +08:00
|
|
|
data.hadDB = exists
|
2019-01-08 04:24:23 +08:00
|
|
|
db, err := storm.Open(path)
|
|
|
|
checkErr(err)
|
|
|
|
defer db.Close()
|
2019-01-10 05:37:47 +08:00
|
|
|
data.store, err = bolt.NewStorage(db)
|
|
|
|
checkErr(err)
|
2019-01-08 04:24:23 +08:00
|
|
|
fn(cmd, args, data)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 16:57:24 +08:00
|
|
|
|
|
|
|
func marshal(filename string, data interface{}) error {
|
|
|
|
fd, err := os.Create(filename)
|
|
|
|
checkErr(err)
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(filename); ext {
|
|
|
|
case ".json":
|
|
|
|
encoder := json.NewEncoder(fd)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
return encoder.Encode(data)
|
2020-06-01 07:12:36 +08:00
|
|
|
case ".yml", ".yaml": //nolint:goconst
|
2019-01-08 16:57:24 +08:00
|
|
|
encoder := yaml.NewEncoder(fd)
|
|
|
|
return encoder.Encode(data)
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshal(filename string, data interface{}) error {
|
|
|
|
fd, err := os.Open(filename)
|
|
|
|
checkErr(err)
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(filename); ext {
|
|
|
|
case ".json":
|
|
|
|
return json.NewDecoder(fd).Decode(data)
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
return yaml.NewDecoder(fd).Decode(data)
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonYamlArg(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ext := filepath.Ext(args[0]); ext {
|
|
|
|
case ".json", ".yml", ".yaml":
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid format: " + ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
for k, v := range in {
|
|
|
|
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpInterfaceArray(in []interface{}) []interface{} {
|
|
|
|
result := make([]interface{}, len(in))
|
|
|
|
for i, v := range in {
|
|
|
|
result[i] = cleanUpMapValue(v)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpMapValue(v interface{}) interface{} {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
return cleanUpInterfaceArray(v)
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
return cleanUpInterfaceMap(v)
|
|
|
|
default:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 15:52:27 +08:00
|
|
|
|
|
|
|
// convertCmdStrToCmdArray checks if cmd string is blank (whitespace included)
|
|
|
|
// then returns empty string array, else returns the splitted word array of cmd.
|
|
|
|
// This is to ensure the result will never be []string{""}
|
|
|
|
func convertCmdStrToCmdArray(cmd string) []string {
|
|
|
|
var cmdArray []string
|
|
|
|
trimmedCmdStr := strings.TrimSpace(cmd)
|
|
|
|
if trimmedCmdStr != "" {
|
|
|
|
cmdArray = strings.Split(trimmedCmdStr, " ")
|
|
|
|
}
|
|
|
|
return cmdArray
|
|
|
|
}
|