alist/drivers/lanzou/driver.go

197 lines
4.4 KiB
Go
Raw Normal View History

2021-12-06 07:55:05 +00:00
package lanzou
2021-11-30 13:30:50 +00:00
import (
"github.com/Xhofe/alist/conf"
2021-12-06 07:55:05 +00:00
"github.com/Xhofe/alist/drivers/base"
2021-11-30 13:30:50 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"path/filepath"
)
type Lanzou struct{}
2021-12-06 07:55:05 +00:00
func (driver Lanzou) Config() base.DriverConfig {
return base.DriverConfig{
Name: "Lanzou",
NoCors: true,
2021-11-30 13:30:50 +00:00
}
}
2021-12-06 07:55:05 +00:00
func (driver Lanzou) Items() []base.Item {
return []base.Item{
2021-11-30 13:30:50 +00:00
{
2021-12-16 07:44:18 +00:00
Name: "internal_type",
2021-11-30 13:30:50 +00:00
Label: "lanzou type",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-30 13:30:50 +00:00
Required: true,
Values: "cookie,url",
},
{
Name: "access_token",
Label: "cookie",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-30 13:30:50 +00:00
Description: "about 15 days valid",
2022-01-15 12:10:42 +00:00
Required: true,
2021-11-30 13:30:50 +00:00
},
{
2022-01-15 12:10:42 +00:00
Name: "site_url",
Label: "share url",
Type: base.TypeString,
Required: true,
2021-11-30 13:30:50 +00:00
},
{
2022-01-15 12:10:42 +00:00
Name: "root_folder",
Label: "root folder file_id",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-30 13:30:50 +00:00
},
{
Name: "password",
Label: "share password",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-30 13:30:50 +00:00
},
}
}
func (driver Lanzou) Save(account *model.Account, old *model.Account) error {
if account == nil {
return nil
}
2021-12-16 07:44:18 +00:00
if account.InternalType == "cookie" {
2021-11-30 13:30:50 +00:00
if account.RootFolder == "" {
account.RootFolder = "-1"
}
}
account.Status = "work"
_ = model.SaveAccount(account)
return nil
}
func (driver Lanzou) File(path string, account *model.Account) (*model.File, error) {
path = utils.ParsePath(path)
if path == "/" {
return &model.File{
Id: account.RootFolder,
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
Driver: driver.Config().Name,
UpdatedAt: account.UpdatedAt,
}, nil
}
dir, name := filepath.Split(path)
files, err := driver.Files(dir, account)
if err != nil {
return nil, err
}
for _, file := range files {
if file.Name == name {
return &file, nil
}
}
2021-12-06 07:55:05 +00:00
return nil, base.ErrPathNotFound
2021-11-30 13:30:50 +00:00
}
func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
var rawFiles []LanZouFile
2021-12-08 14:58:44 +00:00
cache, err := base.GetCache(path, account)
2021-11-30 13:30:50 +00:00
if err == nil {
rawFiles, _ = cache.([]LanZouFile)
} else {
file, err := driver.File(path, account)
if err != nil {
return nil, err
}
rawFiles, err = driver.GetFiles(file.Id, account)
if err != nil {
return nil, err
}
if len(rawFiles) > 0 {
2021-12-08 14:58:44 +00:00
_ = base.SetCache(path, rawFiles, account)
2021-11-30 13:30:50 +00:00
}
}
files := make([]model.File, 0)
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
return files, nil
}
2021-12-19 12:00:53 +00:00
func (driver Lanzou) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
2021-11-30 13:30:50 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-30 13:30:50 +00:00
}
log.Debugf("down file: %+v", file)
downId := file.Id
2021-12-16 07:44:18 +00:00
if account.InternalType == "cookie" {
2021-11-30 13:30:50 +00:00
downId, err = driver.GetDownPageId(file.Id, account)
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-30 13:30:50 +00:00
}
}
2022-01-15 12:10:42 +00:00
url, err := driver.GetLink(downId, account)
2021-11-30 13:30:50 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-30 13:30:50 +00:00
}
2021-12-09 11:24:34 +00:00
link := base.Link{
Url: url,
2022-02-13 09:46:07 +00:00
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
},
2021-12-09 11:24:34 +00:00
}
return &link, nil
2021-11-30 13:30:50 +00:00
}
func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
log.Debugf("lanzou 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-30 13:30:50 +00:00
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
2022-02-02 10:03:54 +00:00
//func (driver Lanzou) Proxy(r *http.Request, account *model.Account) {
// r.Header.Del("Origin")
//}
2021-11-30 13:30:50 +00:00
func (driver Lanzou) 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 *Lanzou) MakeDir(path string, account *model.Account) error {
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-12-05 07:22:19 +00:00
}
func (driver *Lanzou) Move(src string, dst string, account *model.Account) error {
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-12-05 07:22:19 +00:00
}
2022-01-03 12:06:36 +00:00
func (driver *Lanzou) Rename(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}
2021-12-05 07:22:19 +00:00
func (driver *Lanzou) Copy(src string, dst string, account *model.Account) error {
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-12-05 07:22:19 +00:00
}
func (driver *Lanzou) Delete(path string, account *model.Account) error {
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-12-05 07:22:19 +00:00
}
func (driver *Lanzou) Upload(file *model.FileStream, account *model.Account) error {
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-11-30 13:30:50 +00:00
}
2021-12-06 07:55:05 +00:00
var _ base.Driver = (*Lanzou)(nil)