basic structure

This commit is contained in:
微凉 2021-10-26 22:28:37 +08:00
parent d68a4048da
commit fb7e67724d
25 changed files with 615 additions and 45 deletions

3
.gitignore vendored
View File

@ -22,4 +22,5 @@ dist/
# vendor/
*.yml
bin/*
alist
alist
*.json

10
conf/const.go Normal file
View File

@ -0,0 +1,10 @@
package conf
const (
UNKNOWN = iota
FOLDER
OFFICE
VIDEO
AUDIO
TEXT
)

View File

@ -4,8 +4,15 @@ import "gorm.io/gorm"
var (
ConfigFile string // config file
Conf *Config
Debug bool
Conf *Config
Debug bool
DB *gorm.DB
)
)
var (
TextTypes = []string{"txt", "go"}
OfficeTypes = []string{"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"}
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb"}
AudioTypes = []string{"mp3", "flac"}
)

View File

@ -1,14 +0,0 @@
{
"address": "0.0.0.0",
"port": 5244,
"database": {
"type": "sqlite3",
"user": "",
"password": "",
"host": "",
"port": 0,
"name": "",
"table_prefix": "a_list_",
"db_file": "data.db"
}
}

28
drivers/driver.go Normal file
View File

@ -0,0 +1,28 @@
package drivers
import "github.com/Xhofe/alist/model"
type Driver interface {
Path(path string, account *model.Account) (*model.File, []*model.File, error)
Link(path string, account *model.Account) (string,error)
Save(account model.Account)
}
var driversMap = map[string]Driver{}
func RegisterDriver(name string, driver Driver) {
driversMap[name] = driver
}
func GetDriver(name string) (driver Driver, ok bool) {
driver, ok = driversMap[name]
return
}
func GetDriverNames() []string {
names := make([]string, 0)
for k, _ := range driversMap {
names = append(names, k)
}
return names
}

85
drivers/native.go Normal file
View File

@ -0,0 +1,85 @@
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Native struct {
}
func (n Native) Save(account model.Account) {
log.Debugf("save a account: [%s]",account.Name)
}
func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
fullPath := filepath.Join(account.RootFolder,path)
log.Debugf("%s-%s-%s",account.RootFolder,path,fullPath)
if !utils.Exists(fullPath) {
return nil,nil,fmt.Errorf("path not found")
}
if utils.IsDir(fullPath) {
result := make([]*model.File,0)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
return nil, nil, err
}
for _,f := range files {
if strings.HasPrefix(f.Name(),".") {
continue
}
time := f.ModTime()
file := &model.File{
Name: f.Name(),
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
}
if f.IsDir() {
file.Type = conf.FOLDER
}else {
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
}
result = append(result, file)
}
return nil, result, nil
}
f,err := os.Stat(fullPath)
if err != nil {
return nil, nil, err
}
time := f.ModTime()
file := &model.File{
Name: f.Name(),
Size: f.Size(),
Type: utils.GetFileType(filepath.Ext(f.Name())),
UpdatedAt: &time,
}
return file, nil, nil
}
func (n Native) Link(path string, account *model.Account) (string,error) {
fullPath := filepath.Join(account.RootFolder,path)
s, err := os.Stat(fullPath)
if err != nil {
return "", err
}
if s.IsDir() {
return "", fmt.Errorf("can't down folder")
}
return fullPath,nil
}
var _ Driver = (*Native)(nil)
func init() {
RegisterDriver("native", &Native{})
}

4
go.mod
View File

@ -6,6 +6,9 @@ require github.com/gofiber/fiber/v2 v2.20.2
require (
github.com/andybalholm/brotli v1.0.3 // 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.9.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
@ -18,6 +21,7 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-sqlite3 v1.14.9 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect

20
go.sum
View File

@ -7,10 +7,18 @@ github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMe
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
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/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
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.9.0 h1:NgTtmN58D0m8+UuxtYmGztBJB7VnPgjj221I1QHci2A=
github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@ -76,9 +84,14 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
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/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
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/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@ -91,9 +104,12 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.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.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
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/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
@ -111,6 +127,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
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/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
@ -167,6 +184,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/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-20211023085530-d6a326fbbf70 h1:SeSEfdIxyvwGJliREIJhRPPXvW6sDlLT+UQ3B0hD0NA=
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@ -193,10 +211,12 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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=
gorm.io/driver/mysql v1.1.2 h1:OofcyE2lga734MxwcCW9uB4mWNXMr50uaGRVwQL2B0M=
gorm.io/driver/mysql v1.1.2/go.mod h1:4P/X9vSc3WTrhTLZ259cpFd6xKNYiSSdSZngkSBGIMM=
gorm.io/driver/postgres v1.1.2 h1:Amy3hCvLqM+/ICzjCnQr8wKFLVJTeOTdlMT7kCP+J1Q=

43
main.go
View File

@ -6,13 +6,32 @@ import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/public"
"github.com/Xhofe/alist/server"
"github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)
// initConf init config
// initLog init log
func initLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
log.SetFormatter(&log.TextFormatter{
//DisableColors: true,
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
// InitConf init config
func initConf() {
log.Infof("reading config file: %s", conf.ConfigFile)
if !utils.Exists(conf.ConfigFile) {
@ -35,20 +54,6 @@ func initConf() {
log.Debugf("config:%+v", conf.Conf)
}
// initLog init log
func initLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
log.SetFormatter(&log.TextFormatter{
//DisableColors: true,
ForceColors: true,
EnvironmentOverrideColors: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
func init() {
flag.StringVar(&conf.ConfigFile, "conf", "config.json", "config file")
@ -61,9 +66,11 @@ func init() {
func main() {
app := fiber.New()
app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendString("Hello, World 👋!")
})
app.Use("/",filesystem.New(filesystem.Config{
Root: http.FS(public.Public),
//NotFoundFile: "index.html",
}))
server.InitApiRouter(app)
log.Info("starting server")
_ = app.Listen(fmt.Sprintf(":%d", conf.Conf.Port))
}

89
model/account.go Normal file
View File

@ -0,0 +1,89 @@
package model
import (
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
)
type Account struct {
Name string `json:"name" gorm:"primaryKey" validate:"required"`
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RootFolder string `json:"root_folder"`
Status int `json:"status"`
CronId int `json:"cron_id"`
}
var accountsMap = map[string]Account{}
func SaveAccount(account Account) error {
if err := conf.DB.Save(account).Error; err != nil {
return err
}
RegisterAccount(account)
return nil
}
func DeleteAccount(name string) error {
account := Account{
Name: name,
}
if err := conf.DB.Delete(&account).Error; err != nil {
return err
}
delete(accountsMap, name)
return nil
}
func AccountsCount() int {
return len(accountsMap)
}
func RegisterAccount(account Account) {
accountsMap[account.Name] = account
}
func GetAccount(name string) (Account, bool) {
if len(accountsMap) == 1 {
for _, v := range accountsMap {
return v, true
}
}
account, ok := accountsMap[name]
return account, ok
}
func GetAccountFiles() []*File {
files := make([]*File, 0)
for _, v := range accountsMap {
files = append(files, &File{
Name: v.Name,
Size: 0,
Type: conf.FOLDER,
UpdatedAt: nil,
})
}
return files
}
func GetAccounts() []*Account {
accounts := make([]*Account, 0)
for _, v := range accountsMap {
accounts = append(accounts, &v)
}
return accounts
}
func initAccounts() {
var accounts []Account
if err := conf.DB.Find(&accounts).Error; err != nil {
log.Fatalf("failed sync init accounts")
}
for _, account := range accounts {
RegisterAccount(account)
}
log.Debugf("accounts:%+v", accountsMap)
}

View File

@ -1,7 +0,0 @@
package model
type ConfigItem struct {
Key string `json:"key"`
Value string `json:"value"`
Type int `json:"type"`
}

10
model/file.go Normal file
View File

@ -0,0 +1,10 @@
package model
import "time"
type File struct {
Name string `json:"name"`
Size int64 `json:"size"`
Type int `json:"type"`
UpdatedAt *time.Time `json:"updated_at"`
}

View File

@ -64,8 +64,12 @@ func InitModel() {
log.Fatalf("not supported database type: %s", config.Type)
}
log.Infof("auto migrate model")
err := conf.DB.AutoMigrate(&ConfigItem{})
err := conf.DB.AutoMigrate(&SettingItem{},&Account{})
if err != nil {
log.Fatalf("failed to auto migrate")
}
}
// TODO init accounts and filetype
initAccounts()
}

8
model/meta.go Normal file
View File

@ -0,0 +1,8 @@
package model
type Meta struct {
Path string `json:"path" gorm:"primaryKey"`
Password string `json:"password"`
Hide bool `json:"hide"`
Ignore bool `json:"ignore"`
}

27
model/setting.go Normal file
View File

@ -0,0 +1,27 @@
package model
import "github.com/Xhofe/alist/conf"
type SettingItem struct {
Key string `json:"key" gorm:"primaryKey" validate:"required"`
Value string `json:"value"`
Description string `json:"description"`
Type int `json:"type"`
}
func SaveSettings(items []SettingItem) error {
//tx := conf.DB.Begin()
//for _,item := range items{
// tx.Save(item)
//}
//tx.Commit()
return conf.DB.Save(items).Error
}
func GetSettingByType(t int) (*[]SettingItem, error) {
var items []SettingItem
if err := conf.DB.Where("type = ?", t).Find(&items).Error; err != nil {
return nil, err
}
return &items, nil
}

6
public/public.go Normal file
View File

@ -0,0 +1,6 @@
package public
import "embed"
//go:embed *
var Public embed.FS

40
server/account.go Normal file
View File

@ -0,0 +1,40 @@
package server
import (
"fmt"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
)
func GetAccounts(ctx *fiber.Ctx) error {
return SuccessResp(ctx, model.GetAccounts())
}
func SaveAccount(ctx *fiber.Ctx) error {
var req model.Account
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
if err := validate.Struct(req); err != nil {
return ErrorResp(ctx, err, 400)
}
driver, ok := drivers.GetDriver(req.Type)
if !ok {
return ErrorResp(ctx, fmt.Errorf("no [%s] driver", req.Type), 400)
}
if err := model.SaveAccount(req); err != nil {
return ErrorResp(ctx, err, 500)
} else {
driver.Save(req)
return SuccessResp(ctx)
}
}
func DeleteAccount(ctx *fiber.Ctx) error {
name := ctx.Query("name")
if err := model.DeleteAccount(name); err != nil {
return ErrorResp(ctx, err, 500)
}
return SuccessResp(ctx)
}

65
server/common.go Normal file
View File

@ -0,0 +1,65 @@
package server
import (
"fmt"
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"strings"
)
var validate = validator.New()
type Resp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) {
var path,name string
switch model.AccountsCount() {
case 0:
return nil,"",nil,fmt.Errorf("no accounts,please add one first")
case 1:
path = rawPath
break
default:
paths := strings.Split(rawPath,"/")
path = strings.Join(paths[1:],"/")
name = paths[0]
}
account,ok := model.GetAccount(name)
if !ok {
return nil,"",nil,fmt.Errorf("")
}
driver,ok := drivers.GetDriver(account.Type)
if !ok {
return nil,"",nil,fmt.Errorf("no [%s] driver",account.Type)
}
return &account,path,driver,nil
}
func ErrorResp(ctx *fiber.Ctx,err error,code int) error {
return ctx.JSON(Resp{
Code: code,
Msg: err.Error(),
Data: nil,
})
}
func SuccessResp(ctx *fiber.Ctx, data ...interface{}) error {
if len(data) == 0 {
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Data: nil,
})
}
return ctx.JSON(Resp{
Code: 200,
Msg: "success",
Data: data[0],
})
}

20
server/down.go Normal file
View File

@ -0,0 +1,20 @@
package server
import "github.com/gofiber/fiber/v2"
func Down(ctx *fiber.Ctx) error {
rawPath := ctx.Params("*")
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
link, err := driver.Link(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if account.Type == "native" {
return ctx.SendFile(link)
} else {
return ctx.Redirect(link, 302)
}
}

10
server/driver.go Normal file
View File

@ -0,0 +1,10 @@
package server
import (
"github.com/Xhofe/alist/drivers"
"github.com/gofiber/fiber/v2"
)
func GetDrivers(ctx *fiber.Ctx) error {
return SuccessResp(ctx, drivers.GetDriverNames())
}

46
server/path.go Normal file
View File

@ -0,0 +1,46 @@
package server
import (
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
)
type PathReq struct {
Path string `json:"Path"`
Password string `json:"Password"`
}
func Path(ctx *fiber.Ctx) error {
var req PathReq
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
if model.AccountsCount() > 1 && req.Path == "" {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Data: model.GetAccountFiles(),
})
}
account, path, driver, err := ParsePath(req.Path)
if err != nil {
return ErrorResp(ctx, err, 500)
}
file, files, err := driver.Path(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if file != nil {
return ctx.JSON(Resp{
Code: 200,
Msg: "file",
Data: []*model.File{file},
})
} else {
return ctx.JSON(Resp{
Code: 200,
Msg: "folder",
Data: files,
})
}
}

25
server/router.go Normal file
View File

@ -0,0 +1,25 @@
package server
import "github.com/gofiber/fiber/v2"
func InitApiRouter(app *fiber.App) {
app.Get("/d/*", Down)
public := app.Group("/api/public")
{
// TODO check accounts
public.Post("/path", Path)
}
admin := app.Group("/api/admin")
{
// TODO auth
admin.Get("/settings", GetSettingsByType)
admin.Post("/settings", SaveSettings)
admin.Post("/account", SaveAccount)
admin.Get("/accounts", GetAccounts)
admin.Delete("/account", DeleteAccount)
admin.Get("/drivers", GetDrivers)
}
}

38
server/setting.go Normal file
View File

@ -0,0 +1,38 @@
package server
import (
"github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
"strconv"
)
func SaveSettings(ctx *fiber.Ctx) error {
var req []model.SettingItem
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx,err,400)
}
if err := validate.Struct(req); err != nil {
return ErrorResp(ctx,err,400)
}
if err := model.SaveSettings(req); err != nil {
return ctx.JSON(Resp{
Code: 500,
Msg: err.Error(),
Data: nil,
})
} else {
return SuccessResp(ctx)
}
}
func GetSettingsByType(ctx *fiber.Ctx) error {
t, err := strconv.Atoi(ctx.Query("type"))
if err != nil {
return ErrorResp(ctx, err, 400)
}
settings, err := model.GetSettingByType(t)
if err != nil {
return ErrorResp(ctx, err, 400)
}
return SuccessResp(ctx,settings)
}

View File

@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
@ -18,6 +19,36 @@ func Exists(name string) bool {
return true
}
// IsDir determine whether the file is dir
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// GetFileType get file type
func GetFileType(ext string) int {
if ext == "" {
return conf.UNKNOWN
}
ext = ext[1:]
if IsContain(conf.OfficeTypes,ext) {
return conf.OFFICE
}
if IsContain(conf.AudioTypes,ext) {
return conf.AUDIO
}
if IsContain(conf.VideoTypes,ext) {
return conf.VIDEO
}
if IsContain(conf.TextTypes,ext) {
return conf.TEXT
}
return conf.UNKNOWN
}
// CreatNestedFile create nested file
func CreatNestedFile(path string) (*os.File, error) {
basePath := filepath.Dir(path)

10
utils/slice.go Normal file
View File

@ -0,0 +1,10 @@
package utils
func IsContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}