alist/drivers/native/driver.go

266 lines
6.3 KiB
Go
Raw Normal View History

2021-12-06 07:55:05 +00:00
package native
2021-11-27 12:07:32 +00:00
import (
"fmt"
"github.com/Xhofe/alist/conf"
2021-12-06 07:55:05 +00:00
"github.com/Xhofe/alist/drivers/base"
2021-11-27 12:07:32 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
2021-12-05 08:09:39 +00:00
"io"
2021-11-27 12:07:32 +00:00
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Native struct{}
2021-12-06 07:55:05 +00:00
func (driver Native) Config() base.DriverConfig {
return base.DriverConfig{
Name: "Native",
OnlyProxy: true,
2021-12-30 13:39:17 +00:00
OnlyLocal: true,
NoNeedSetLink: true,
LocalSort: true,
2021-11-29 08:42:46 +00:00
}
}
2021-12-06 07:55:05 +00:00
func (driver Native) Items() []base.Item {
return []base.Item{
2021-11-27 12:07:32 +00:00
{
Name: "root_folder",
Label: "root folder path",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 12:07:32 +00:00
Required: true,
},
}
}
func (driver Native) Save(account *model.Account, old *model.Account) error {
if account == nil {
return nil
}
2021-11-27 12:07:32 +00:00
log.Debugf("save a account: [%s]", account.Name)
if !utils.Exists(account.RootFolder) {
account.Status = fmt.Sprintf("[%s] not exist", account.RootFolder)
_ = model.SaveAccount(account)
return fmt.Errorf("[%s] not exist", account.RootFolder)
}
account.Status = "work"
account.Proxy = true
err := model.SaveAccount(account)
if err != nil {
return err
}
return nil
}
func (driver Native) File(path string, account *model.Account) (*model.File, error) {
if utils.IsContain(strings.Split(path, "/"), "..") {
return nil, base.ErrRelativePath
}
2021-11-27 12:07:32 +00:00
fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) {
2021-12-06 07:55:05 +00:00
return nil, base.ErrPathNotFound
2021-11-27 12:07:32 +00:00
}
f, err := os.Stat(fullPath)
if err != nil {
return nil, err
}
time := f.ModTime()
file := &model.File{
Name: f.Name(),
UpdatedAt: &time,
2021-11-30 01:37:51 +00:00
Driver: driver.Config().Name,
2021-11-27 12:07:32 +00:00
}
if f.IsDir() {
file.Type = conf.FOLDER
} else {
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
2022-04-06 07:11:03 +00:00
file.Size = f.Size()
2021-11-27 12:07:32 +00:00
}
return file, nil
}
func (driver Native) Files(path string, account *model.Account) ([]model.File, error) {
if utils.IsContain(strings.Split(path, "/"), "..") {
return nil, base.ErrRelativePath
}
2021-11-27 12:07:32 +00:00
fullPath := filepath.Join(account.RootFolder, path)
if !utils.Exists(fullPath) {
2021-12-06 07:55:05 +00:00
return nil, base.ErrPathNotFound
2021-11-27 12:07:32 +00:00
}
files := make([]model.File, 0)
rawFiles, err := ioutil.ReadDir(fullPath)
if err != nil {
return nil, err
}
for _, f := range rawFiles {
if strings.HasPrefix(f.Name(), ".") {
continue
}
time := f.ModTime()
file := model.File{
Name: f.Name(),
Size: f.Size(),
Type: 0,
UpdatedAt: &time,
2021-11-30 01:37:51 +00:00
Driver: driver.Config().Name,
2021-11-27 12:07:32 +00:00
}
if f.IsDir() {
file.Type = conf.FOLDER
} else {
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
}
files = append(files, file)
}
return files, nil
}
2021-12-19 12:00:53 +00:00
func (driver Native) Link(args base.Args, account *model.Account) (*base.Link, error) {
_, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
2021-12-19 12:00:53 +00:00
fullPath := filepath.Join(account.RootFolder, args.Path)
2021-11-27 12:07:32 +00:00
s, err := os.Stat(fullPath)
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-27 12:07:32 +00:00
}
if s.IsDir() {
2021-12-09 11:24:34 +00:00
return nil, base.ErrNotFile
}
link := base.Link{
2022-02-13 07:57:42 +00:00
FilePath: fullPath,
2021-11-27 12:07:32 +00:00
}
2021-12-09 11:24:34 +00:00
return &link, nil
2021-11-27 12:07:32 +00:00
}
func (driver Native) Path(path string, account *model.Account) (*model.File, []model.File, error) {
log.Debugf("native path: %s", path)
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
2021-12-09 11:24:34 +00:00
if !file.IsDir() {
2021-11-27 12:07:32 +00:00
//file.Url, _ = driver.Link(path, account)
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
//model.SortFiles(files, account)
2021-11-27 12:07:32 +00:00
return nil, files, nil
}
2022-02-02 10:03:54 +00:00
//func (driver Native) Proxy(r *http.Request, account *model.Account) {
// // unnecessary
//}
2021-11-27 12:07:32 +00:00
func (driver Native) Preview(path string, account *model.Account) (interface{}, error) {
2021-12-06 07:55:05 +00:00
return nil, base.ErrNotSupport
2021-12-05 07:22:19 +00:00
}
func (driver Native) MakeDir(path string, account *model.Account) error {
if utils.IsContain(strings.Split(path, "/"), "..") {
return base.ErrRelativePath
}
2021-12-05 08:09:39 +00:00
fullPath := filepath.Join(account.RootFolder, path)
err := os.MkdirAll(fullPath, 0700)
return err
2021-12-05 07:22:19 +00:00
}
func (driver Native) Move(src string, dst string, account *model.Account) error {
if utils.IsContain(strings.Split(src+"/"+dst, "/"), "..") {
return base.ErrRelativePath
}
2021-12-05 08:09:39 +00:00
fullSrc := filepath.Join(account.RootFolder, src)
fullDst := filepath.Join(account.RootFolder, dst)
return os.Rename(fullSrc, fullDst)
2021-11-27 12:07:32 +00:00
}
2022-01-03 12:06:36 +00:00
func (driver Native) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
2021-12-05 07:22:19 +00:00
func (driver Native) Copy(src string, dst string, account *model.Account) error {
if utils.IsContain(strings.Split(src+"/"+dst, "/"), "..") {
return base.ErrRelativePath
}
2021-12-05 08:09:39 +00:00
fullSrc := filepath.Join(account.RootFolder, src)
fullDst := filepath.Join(account.RootFolder, dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
dstFile, err := driver.File(dst, account)
if err == nil {
if !dstFile.IsDir() {
2021-12-06 07:55:05 +00:00
return base.ErrNotSupport
2021-12-05 08:09:39 +00:00
}
}
if srcFile.IsDir() {
return driver.CopyDir(fullSrc, fullDst)
}
return driver.CopyFile(fullSrc, fullDst)
2021-12-05 07:22:19 +00:00
}
func (driver Native) Delete(path string, account *model.Account) error {
if utils.IsContain(strings.Split(path, "/"), "..") {
return base.ErrRelativePath
}
2021-12-05 08:09:39 +00:00
fullPath := filepath.Join(account.RootFolder, path)
file, err := driver.File(path, account)
if err != nil {
return err
}
if file.IsDir() {
return os.RemoveAll(fullPath)
}
return os.Remove(fullPath)
2021-12-05 07:22:19 +00:00
}
func (driver Native) Upload(file *model.FileStream, account *model.Account) error {
2021-12-31 06:05:35 +00:00
if file == nil {
return base.ErrEmptyFile
}
if utils.IsContain(strings.Split(file.ParentPath, "/"), "..") {
return base.ErrRelativePath
}
fullPath := filepath.Join(account.RootFolder, file.ParentPath, file.Name)
2021-12-09 11:24:34 +00:00
_, err := driver.File(filepath.Join(file.ParentPath, file.Name), account)
2021-12-05 08:09:39 +00:00
if err == nil {
// TODO overwrite?
}
basePath := filepath.Dir(fullPath)
if !utils.Exists(basePath) {
err := os.MkdirAll(basePath, 0744)
if err != nil {
return err
}
}
out, err := os.Create(fullPath)
if err != nil {
return err
}
defer func() {
_ = out.Close()
}()
2022-01-27 15:48:29 +00:00
//var buf bytes.Buffer
//reader := io.TeeReader(file, &buf)
//h := md5.New()
//_, err = io.Copy(h, reader)
//if err != nil {
// return err
//}
//hash := hex.EncodeToString(h.Sum(nil))
//log.Debugln("md5:", hash)
//_, err = io.Copy(out, &buf)
2021-12-05 08:09:39 +00:00
_, err = io.Copy(out, file)
return err
2021-12-05 07:22:19 +00:00
}
2021-12-05 08:09:39 +00:00
2021-12-06 07:55:05 +00:00
var _ base.Driver = (*Native)(nil)