From fced60c2b53cc88504c2f9782d201de8522cc4b6 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 6 Jun 2022 21:48:53 +0800 Subject: [PATCH] feat: basic structure --- .gitignore | 3 +- bootstrap/README.md | 3 ++ bootstrap/config.go | 72 +++++++++++++++++++++++++++++ bootstrap/log.go | 48 ++++++++++++++++++++ cmd/alist.go | 29 +++++++++++- cmd/args/config.go | 9 ++++ conf/config.go | 68 ++++++++++++++++++++++++++++ conf/const.go | 1 + conf/var.go | 14 ++++++ drivers/README.md | 1 + go.mod | 27 +++++++++++ go.sum | 95 +++++++++++++++++++++++++++++++++++++++ internal/driver/driver.go | 32 +++++++++++++ internal/driver/error.go | 8 ++++ internal/driver/file.go | 18 ++++++++ internal/driver/link.go | 20 +++++++++ internal/model/account.go | 9 ++++ internal/server/README.md | 0 internal/store/account.go | 11 +++++ pkg/utils/file.go | 30 +++++++++++++ pkg/utils/json.go | 24 ++++++++++ 21 files changed, 519 insertions(+), 3 deletions(-) create mode 100644 bootstrap/README.md create mode 100644 bootstrap/config.go create mode 100644 bootstrap/log.go create mode 100644 cmd/args/config.go create mode 100644 conf/config.go create mode 100644 conf/const.go create mode 100644 conf/var.go create mode 100644 drivers/README.md create mode 100644 go.sum create mode 100644 internal/driver/driver.go create mode 100644 internal/driver/error.go create mode 100644 internal/driver/file.go create mode 100644 internal/driver/link.go create mode 100644 internal/model/account.go create mode 100644 internal/server/README.md create mode 100644 internal/store/account.go create mode 100644 pkg/utils/file.go create mode 100644 pkg/utils/json.go diff --git a/.gitignore b/.gitignore index 576dca8b4f9..2aba0ec56ca 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ bin/* public/*.html public/assets/ public/public/ -data/ \ No newline at end of file +data/ +log/ \ No newline at end of file diff --git a/bootstrap/README.md b/bootstrap/README.md new file mode 100644 index 00000000000..817d3abb46a --- /dev/null +++ b/bootstrap/README.md @@ -0,0 +1,3 @@ +What to do at startup, such as: +- parse config +- init store \ No newline at end of file diff --git a/bootstrap/config.go b/bootstrap/config.go new file mode 100644 index 00000000000..b1fa8c3d101 --- /dev/null +++ b/bootstrap/config.go @@ -0,0 +1,72 @@ +package bootstrap + +import ( + "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/conf" + "github.com/alist-org/alist/v3/pkg/utils" + "github.com/caarlos0/env/v6" + log "github.com/sirupsen/logrus" + "io/ioutil" + "os" + "path/filepath" +) + +func InitConfig() { + log.Infof("reading config file: %s", args.Config) + if !utils.Exists(args.Config) { + log.Infof("config file not exists, creating default config file") + _, err := utils.CreatNestedFile(args.Config) + if err != nil { + log.Fatalf("failed to create config file") + } + conf.Conf = conf.DefaultConfig() + if !utils.WriteToJson(args.Config, conf.Conf) { + log.Fatalf("failed to create default config file") + } + } else { + configBytes, err := ioutil.ReadFile(args.Config) + if err != nil { + log.Fatalf("reading config file error:%s", err.Error()) + } + conf.Conf = conf.DefaultConfig() + err = utils.Json.Unmarshal(configBytes, conf.Conf) + if err != nil { + log.Fatalf("load config error: %s", err.Error()) + } + log.Debugf("config:%+v", conf.Conf) + // update config.json struct + confBody, err := utils.Json.MarshalIndent(conf.Conf, "", " ") + if err != nil { + log.Fatalf("marshal config error:%s", err.Error()) + } + err = ioutil.WriteFile(args.Config, confBody, 0777) + if err != nil { + log.Fatalf("update config struct error: %s", err.Error()) + } + } + if !conf.Conf.Force { + confFromEnv() + } + err := os.RemoveAll(filepath.Join(conf.Conf.TempDir)) + if err != nil { + log.Errorln("failed delete temp file:", err) + } + err = os.MkdirAll(conf.Conf.TempDir, 0700) + if err != nil { + log.Fatalf("create temp dir error: %s", err.Error()) + } + log.Debugf("config: %+v", conf.Conf) +} + +func confFromEnv() { + prefix := "ALIST_" + if args.NoPrefix { + prefix = "" + } + log.Infof("load config from env with prefix: %s", prefix) + if err := env.Parse(conf.Conf, env.Options{ + Prefix: prefix, + }); err != nil { + log.Fatalf("load config from env error: %s", err.Error()) + } +} diff --git a/bootstrap/log.go b/bootstrap/log.go new file mode 100644 index 00000000000..bfa42f198f9 --- /dev/null +++ b/bootstrap/log.go @@ -0,0 +1,48 @@ +package bootstrap + +import ( + "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/conf" + rotatelogs "github.com/lestrrat-go/file-rotatelogs" + log "github.com/sirupsen/logrus" + "time" +) + +func Log() { + if args.Debug { + log.SetLevel(log.DebugLevel) + log.SetReportCaller(true) + } + log.SetFormatter(&log.TextFormatter{ + ForceColors: true, + EnvironmentOverrideColors: true, + TimestampFormat: "2006-01-02 15:04:05", + FullTimestamp: true, + }) + logConfig := conf.Conf.Log + if !args.Debug && logConfig.Path != "" { + var ( + writer *rotatelogs.RotateLogs + err error + ) + if logConfig.Name != "" { + writer, err = rotatelogs.New( + logConfig.Path, + rotatelogs.WithLinkName(logConfig.Name), + rotatelogs.WithRotationCount(logConfig.RotationCount), + rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour), + ) + } else { + writer, err = rotatelogs.New( + logConfig.Path, + rotatelogs.WithRotationCount(logConfig.RotationCount), + rotatelogs.WithRotationTime(time.Duration(logConfig.RotationTime)*time.Hour), + ) + } + if err != nil { + log.Fatalf("failed to create rotate log: %s", err) + } + log.SetOutput(writer) + } + log.Infof("init log...") +} diff --git a/cmd/alist.go b/cmd/alist.go index 91cca4fb7ac..a2e51c3e5db 100644 --- a/cmd/alist.go +++ b/cmd/alist.go @@ -1,7 +1,32 @@ package main -import "fmt" +import ( + "flag" + "fmt" + "github.com/alist-org/alist/v3/bootstrap" + "github.com/alist-org/alist/v3/cmd/args" + "github.com/alist-org/alist/v3/conf" + "os" +) +func init() { + flag.StringVar(&args.Config, "conf", "data/config.json", "config file") + flag.BoolVar(&args.Debug, "debug", false, "start with debug mode") + flag.BoolVar(&args.Version, "version", false, "print version info") + flag.BoolVar(&args.Password, "password", false, "print current password") + flag.BoolVar(&args.NoPrefix, "no-prefix", false, "disable env prefix") + flag.Parse() +} + +func Init() { + if args.Version { + fmt.Printf("Built At: %s\nGo Version: %s\nAuthor: %s\nCommit ID: %s\nVersion: %s\nWebVersion: %s\n", + conf.BuiltAt, conf.GoVersion, conf.GitAuthor, conf.GitCommit, conf.Version, conf.WebVersion) + os.Exit(0) + } + bootstrap.InitConfig() + bootstrap.Log() +} func main() { - fmt.Println("Hello, world.") + Init() } diff --git a/cmd/args/config.go b/cmd/args/config.go new file mode 100644 index 00000000000..70a99a2958f --- /dev/null +++ b/cmd/args/config.go @@ -0,0 +1,9 @@ +package args + +var ( + Config string // config file + Debug bool + Version bool + Password bool + NoPrefix bool +) diff --git a/conf/config.go b/conf/config.go new file mode 100644 index 00000000000..01fabe9666a --- /dev/null +++ b/conf/config.go @@ -0,0 +1,68 @@ +package conf + +type Database struct { + Type string `json:"type" env:"DB_TYPE"` + Host string `json:"host" env:"DB_HOST"` + Port int `json:"port" env:"DB_PORT"` + User string `json:"user" env:"DB_USER"` + Password string `json:"password" env:"DB_PASS"` + Name string `json:"name" env:"DB_NAME"` + DBFile string `json:"db_file" env:"DB_FILE"` + TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"` + SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"` +} + +type Scheme struct { + Https bool `json:"https" env:"HTTPS"` + CertFile string `json:"cert_file" env:"CERT_FILE"` + KeyFile string `json:"key_file" env:"KEY_FILE"` +} + +type CacheConfig struct { + Expiration int64 `json:"expiration" env:"CACHE_EXPIRATION"` + CleanupInterval int64 `json:"cleanup_interval" env:"CLEANUP_INTERVAL"` +} + +type LogConfig struct { + Path string `json:"path" env:"LOG_PATH"` + Name string `json:"name" env:"LOG_NAME"` + RotationTime uint `json:"rotation_time" env:"LOG_TIME"` + RotationCount uint `json:"rotation_count" env:"LOG_COUNT"` +} + +type Config struct { + Force bool `json:"force"` + Address string `json:"address" env:"ADDR"` + Port int `json:"port" env:"PORT"` + Assets string `json:"assets" env:"ASSETS"` + Database Database `json:"database"` + Scheme Scheme `json:"scheme"` + Cache CacheConfig `json:"cache"` + TempDir string `json:"temp_dir" env:"TEMP_DIR"` + Log LogConfig `json:"log"` +} + +func DefaultConfig() *Config { + return &Config{ + Address: "0.0.0.0", + Port: 5244, + Assets: "https://npm.elemecdn.com/alist-web@$version/dist", + TempDir: "data/temp", + Database: Database{ + Type: "sqlite3", + Port: 0, + TablePrefix: "x_", + DBFile: "data/data.db", + }, + Cache: CacheConfig{ + Expiration: 60, + CleanupInterval: 120, + }, + Log: LogConfig{ + Path: "log/%Y-%m-%d-%H:%M.log", + Name: "log/log.log", + RotationTime: 24, + RotationCount: 5, + }, + } +} diff --git a/conf/const.go b/conf/const.go new file mode 100644 index 00000000000..8f52a955753 --- /dev/null +++ b/conf/const.go @@ -0,0 +1 @@ +package conf diff --git a/conf/var.go b/conf/var.go new file mode 100644 index 00000000000..951ff63f260 --- /dev/null +++ b/conf/var.go @@ -0,0 +1,14 @@ +package conf + +var ( + BuiltAt string + GoVersion string + GitAuthor string + GitCommit string + Version string = "dev" + WebVersion string +) + +var ( + Conf *Config +) diff --git a/drivers/README.md b/drivers/README.md new file mode 100644 index 00000000000..c810dfb98dc --- /dev/null +++ b/drivers/README.md @@ -0,0 +1 @@ +storage driver \ No newline at end of file diff --git a/go.mod b/go.mod index 38d321092e7..d0cf700e11a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,30 @@ module github.com/alist-org/alist/v3 go 1.18 + +require ( + github.com/caarlos0/env/v6 v6.9.3 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/gin-gonic/gin v1.8.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.11.0 // indirect + github.com/goccy/go-json v0.9.7 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect + github.com/lestrrat-go/strftime v1.0.6 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect + github.com/ugorji/go/codec v1.2.7 // indirect + golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect + golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/protobuf v1.28.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000000..a05d036e3e8 --- /dev/null +++ b/go.sum @@ -0,0 +1,95 @@ +github.com/caarlos0/env/v6 v6.9.3 h1:Tyg69hoVXDnpO5Qvpsu8EoquarbPyQb+YwExWHP8wWU= +github.com/caarlos0/env/v6 v6.9.3/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.8.0 h1:4WFH5yycBMA3za5Hnl425yd9ymdw1XPm4666oab+hv4= +github.com/gin-gonic/gin v1.8.0/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= +github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw= +github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= +github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM= +github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= +github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4= +github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= +github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ= +github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU= +github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= +github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= +github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= +github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= +golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220531201128-c960675eff93 h1:MYimHLfoXEpOhqd/zgoA/uoXzHB86AEky4LAx5ij9xA= +golang.org/x/net v0.0.0-20220531201128-c960675eff93/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/driver/driver.go b/internal/driver/driver.go new file mode 100644 index 00000000000..ab3e6953445 --- /dev/null +++ b/internal/driver/driver.go @@ -0,0 +1,32 @@ +package driver + +import ( + "context" +) + +type Driver interface { + Reader + Writer + Other +} + +type Reader interface { + File(ctx context.Context, path string) (*FileInfo, error) + List(ctx context.Context, path string) ([]FileInfo, error) + Link(ctx context.Context, args LinkArgs) (*Link, error) +} + +type Writer interface { + MakeDir(ctx context.Context, path string) error + Move(ctx context.Context, src, dst string) error + Rename(ctx context.Context, src, dst string) error + Copy(ctx context.Context, src, dst string) error + Remove(ctx context.Context, path string) error + Put(ctx context.Context, stream FileStream, parent string) error +} + +type Other interface { + Init(ctx context.Context) error + Update(ctx context.Context) error + Drop(ctx context.Context) error +} diff --git a/internal/driver/error.go b/internal/driver/error.go new file mode 100644 index 00000000000..a88f2ae382f --- /dev/null +++ b/internal/driver/error.go @@ -0,0 +1,8 @@ +package driver + +import "errors" + +var ( + ErrorDirNotFound = errors.New("directory not found") + ErrorObjectNotFound = errors.New("object not found") +) diff --git a/internal/driver/file.go b/internal/driver/file.go new file mode 100644 index 00000000000..910beee89f4 --- /dev/null +++ b/internal/driver/file.go @@ -0,0 +1,18 @@ +package driver + +import ( + "io" + "time" +) + +type FileInfo interface { + GetName() string + GetModTime() time.Time + GetSize() int64 +} + +type FileStream interface { + io.ReadCloser + FileInfo + GetMimetype() string +} diff --git a/internal/driver/link.go b/internal/driver/link.go new file mode 100644 index 00000000000..1bdc4d08844 --- /dev/null +++ b/internal/driver/link.go @@ -0,0 +1,20 @@ +package driver + +import ( + "io" + "net/http" +) + +type LinkArgs struct { + Path string + IP string + Header http.Header +} + +type Link struct { + URL string + Header http.Header + Data io.ReadCloser + Status int + FilePath string +} diff --git a/internal/model/account.go b/internal/model/account.go new file mode 100644 index 00000000000..e34db667158 --- /dev/null +++ b/internal/model/account.go @@ -0,0 +1,9 @@ +package model + +type Account struct { + ID uint `json:"id" gorm:"primaryKey"` + VirtualPath string `json:"virtual_path"` + Index int `json:"index"` + Type string `json:"type"` + Status string `json:"status"` +} diff --git a/internal/server/README.md b/internal/server/README.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/store/account.go b/internal/store/account.go new file mode 100644 index 00000000000..c21d93c3e2a --- /dev/null +++ b/internal/store/account.go @@ -0,0 +1,11 @@ +package store + +import "github.com/alist-org/alist/v3/internal/model" + +type Account interface { + Create(account model.Account) error + Update(account model.Account) error + Delete(id uint) error + GetByID(id uint) (*model.Account, error) + List() ([]model.Account, error) +} diff --git a/pkg/utils/file.go b/pkg/utils/file.go new file mode 100644 index 00000000000..cedf9cb8a73 --- /dev/null +++ b/pkg/utils/file.go @@ -0,0 +1,30 @@ +package utils + +import ( + log "github.com/sirupsen/logrus" + "os" + "path/filepath" +) + +// Exists determine whether the file exists +func Exists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// CreatNestedFile create nested file +func CreatNestedFile(path string) (*os.File, error) { + basePath := filepath.Dir(path) + if !Exists(basePath) { + err := os.MkdirAll(basePath, 0700) + if err != nil { + log.Errorf("can't create foler,%s", err) + return nil, err + } + } + return os.Create(path) +} diff --git a/pkg/utils/json.go b/pkg/utils/json.go new file mode 100644 index 00000000000..3f67e1bb68b --- /dev/null +++ b/pkg/utils/json.go @@ -0,0 +1,24 @@ +package utils + +import ( + json "github.com/json-iterator/go" + log "github.com/sirupsen/logrus" + "io/ioutil" +) + +var Json = json.ConfigCompatibleWithStandardLibrary + +// WriteToJson write struct to json file +func WriteToJson(src string, conf interface{}) bool { + data, err := Json.MarshalIndent(conf, "", " ") + if err != nil { + log.Errorf("failed convert Conf to []byte:%s", err.Error()) + return false + } + err = ioutil.WriteFile(src, data, 0777) + if err != nil { + log.Errorf("failed to write json file:%s", err.Error()) + return false + } + return true +}