alist/drivers/native.go

120 lines
2.6 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-11-13 07:53:26 +00:00
"github.com/gin-gonic/gin"
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-31 13:27:47 +00:00
}
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-11-17 08:37:12 +00:00
func (n Native) Proxy(c *gin.Context, account *model.Account) {
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) {
2021-11-16 10:23:59 +00:00
account.Status = fmt.Sprintf("[%s] not exist", account.RootFolder)
_ = model.SaveAccount(account)
2021-10-29 06:50:26 +00:00
return fmt.Errorf("[%s] not exist", account.RootFolder)
}
2021-11-15 07:01:14 +00:00
account.Status = "work"
2021-11-17 09:20:57 +00:00
account.Proxy = true
2021-11-15 07:01:14 +00:00
err := model.SaveAccount(account)
if err != nil {
return err
}
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-11-23 07:58:03 +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-11-23 07:58:03 +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()
2021-11-23 07:58:03 +00:00
file := model.File{
2021-10-26 14:28:37 +00:00
Name: f.Name(),
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
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,
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
}