alist/drivers/native.go

112 lines
2.3 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package drivers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
2021-10-28 16:02:02 +00:00
"github.com/gofiber/fiber/v2"
2021-10-26 14:28:37 +00:00
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Native struct {
2021-10-29 06:50:26 +00:00
}
2021-10-26 14:28:37 +00:00
2021-10-31 13:27:47 +00:00
func (n Native) Preview(path string, account *model.Account) (interface{}, error) {
return nil,fmt.Errorf("no need")
}
2021-10-29 06:50:26 +00:00
func (n Native) Items() []Item {
return []Item{
{
Name: "root_folder",
Label: "root folder path",
Type: "string",
Required: true,
},
}
2021-10-26 14:28:37 +00:00
}
2021-10-28 16:02:02 +00:00
func (n Native) Proxy(ctx *fiber.Ctx) {
2021-10-29 06:50:26 +00:00
// unnecessary
2021-10-28 16:02:02 +00:00
}
2021-10-27 10:59:03 +00:00
func (n Native) Save(account *model.Account, old *model.Account) error {
2021-10-29 06:50:26 +00:00
log.Debugf("save a account: [%s]", account.Name)
if !utils.Exists(account.RootFolder) {
return fmt.Errorf("[%s] not exist", account.RootFolder)
}
2021-10-27 10:59:03 +00:00
return nil
2021-10-26 14:28:37 +00:00
}
2021-10-27 10:59:03 +00:00
// TODO sort files
2021-10-26 14:28:37 +00:00
func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
2021-10-29 06:50:26 +00:00
fullPath := filepath.Join(account.RootFolder, path)
log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath)
2021-10-26 14:28:37 +00:00
if !utils.Exists(fullPath) {
2021-10-29 06:50:26 +00:00
return nil, nil, fmt.Errorf("path not found")
2021-10-26 14:28:37 +00:00
}
if utils.IsDir(fullPath) {
2021-10-29 06:50:26 +00:00
result := make([]*model.File, 0)
2021-10-26 14:28:37 +00:00
files, err := ioutil.ReadDir(fullPath)
if err != nil {
return nil, nil, err
}
2021-10-29 06:50:26 +00:00
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") {
2021-10-26 14:28:37 +00:00
continue
}
time := f.ModTime()
file := &model.File{
Name: f.Name(),
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
2021-10-30 16:36:17 +00:00
Driver: "Native",
2021-10-26 14:28:37 +00:00
}
if f.IsDir() {
file.Type = conf.FOLDER
2021-10-29 06:50:26 +00:00
} else {
2021-10-26 14:28:37 +00:00
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
}
result = append(result, file)
}
return nil, result, nil
}
2021-10-29 06:50:26 +00:00
f, err := os.Stat(fullPath)
2021-10-26 14:28:37 +00:00
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,
2021-10-30 16:36:17 +00:00
Driver: "Native",
2021-10-26 14:28:37 +00:00
}
return file, nil, nil
}
2021-10-29 06:50:26 +00:00
func (n Native) Link(path string, account *model.Account) (string, error) {
fullPath := filepath.Join(account.RootFolder, path)
2021-10-26 14:28:37 +00:00
s, err := os.Stat(fullPath)
if err != nil {
return "", err
}
if s.IsDir() {
return "", fmt.Errorf("can't down folder")
}
2021-10-29 06:50:26 +00:00
return fullPath, nil
2021-10-26 14:28:37 +00:00
}
var _ Driver = (*Native)(nil)
func init() {
2021-10-27 10:59:03 +00:00
RegisterDriver("Native", &Native{})
2021-10-29 06:50:26 +00:00
}