🎨 change link interface

This commit is contained in:
微凉 2021-12-19 20:00:53 +08:00
parent f5b8815a84
commit d00f75c814
14 changed files with 96 additions and 73 deletions

View File

@ -125,8 +125,8 @@ func (driver Pan123) Files(path string, account *model.Account) ([]model.File, e
return files, nil
}
func (driver Pan123) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(utils.ParsePath(path), account)
func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(utils.ParsePath(args.Path), account)
if err != nil {
return nil, err
}
@ -160,9 +160,9 @@ func (driver Pan123) Link(path string, account *model.Account) (*base.Link, erro
link := base.Link{
Url: resp.Data.DownloadUrl,
}
//if res.StatusCode() == 302 {
// link.Url = res.Header().Get("location")
//}
if res.StatusCode() == 302 {
link.Url = res.Header().Get("location")
}
return &link, nil
}
@ -174,7 +174,7 @@ func (driver Pan123) Path(path string, account *model.Account) (*model.File, []m
return nil, nil, err
}
if !file.IsDir() {
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
return nil, nil, err
}

View File

@ -138,8 +138,8 @@ func (driver Cloud189) Files(path string, account *model.Account) ([]model.File,
return files, nil
}
func (driver Cloud189) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(utils.ParsePath(path), account)
func (driver Cloud189) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(utils.ParsePath(args.Path), account)
if err != nil {
return nil, err
}
@ -167,7 +167,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (*base.Link, er
if err != nil {
return nil, err
}
return driver.Link(path, account)
return driver.Link(args, account)
}
}
if resp.ResCode != 0 {
@ -194,7 +194,7 @@ func (driver Cloud189) Path(path string, account *model.Account) (*model.File, [
return nil, nil, err
}
if !file.IsDir() {
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
return nil, nil, err
}
@ -229,7 +229,7 @@ func (driver Cloud189) MakeDir(path string, account *model.Account) error {
"parentFolderId": parent.Id,
"folderName": name,
}
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form,nil, account)
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form, nil, account)
if err == nil {
_ = base.DeleteCache(dir, account)
}
@ -257,7 +257,7 @@ func (driver Cloud189) Move(src string, dst string, account *model.Account) erro
idKey: srcFile.Id,
nameKey: dstName,
}
_, err = driver.Request(url, "POST", form,nil, account)
_, err = driver.Request(url, "POST", form, nil, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
@ -284,7 +284,7 @@ func (driver Cloud189) Move(src string, dst string, account *model.Account) erro
"targetFolderId": dstDirFile.Id,
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
}
if err == nil {
_ = base.DeleteCache(srcDir, account)
@ -323,7 +323,7 @@ func (driver Cloud189) Copy(src string, dst string, account *model.Account) erro
"targetFolderId": dstDirFile.Id,
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
if err == nil {
_ = base.DeleteCache(dstDir, account)
}
@ -356,7 +356,7 @@ func (driver Cloud189) Delete(path string, account *model.Account) error {
"targetFolderId": "",
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
@ -377,11 +377,11 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
}
res, err := driver.UploadRequest("/person/initMultiUpload", map[string]string{
"parentFolderId": parentFile.Id,
"fileName": file.Name,
"fileSize": strconv.FormatInt(int64(file.Size),10),
"sliceSize": strconv.FormatInt(int64(DEFAULT),10),
"lazyCheck": "1",
},account)
"fileName": file.Name,
"fileSize": strconv.FormatInt(int64(file.Size), 10),
"sliceSize": strconv.FormatInt(int64(DEFAULT), 10),
"lazyCheck": "1",
}, account)
if err != nil {
return err
}
@ -409,21 +409,21 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
md5s = append(md5s, md5Str)
md5Sum.Write(byteData)
res, err = driver.UploadRequest("/person/getMultiUploadUrls", map[string]string{
"partInfo": fmt.Sprintf("%s-%s",strconv.FormatInt(i,10),md5Base64),
"partInfo": fmt.Sprintf("%s-%s", strconv.FormatInt(i, 10), md5Base64),
"uploadFileId": uploadFileId,
},account)
}, account)
if err != nil {
return err
}
uploadData := jsoniter.Get(res,"uploadUrls.partNumber_"+strconv.FormatInt(i,10))
headers := strings.Split(uploadData.Get("requestHeader").ToString(),"&")
uploadData := jsoniter.Get(res, "uploadUrls.partNumber_"+strconv.FormatInt(i, 10))
headers := strings.Split(uploadData.Get("requestHeader").ToString(), "&")
req, err := http.NewRequest("PUT", uploadData.Get("requestURL").ToString(), bytes.NewBuffer(byteData))
if err != nil {
return err
}
for _,header := range headers{
for _, header := range headers {
kv := strings.Split(header, "=")
req.Header.Set(kv[0],strings.Join(kv[1:],"="))
req.Header.Set(kv[0], strings.Join(kv[1:], "="))
}
res, err := base.HttpClient.Do(req)
if err != nil {
@ -432,12 +432,12 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
log.Debugf("%+v", res)
}
id := md5Sum.Sum(nil)
res,err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
res, err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
"uploadFileId": uploadFileId,
"fileMd5": hex.EncodeToString(id),
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s,"\n")),
"lazyCheck":"1",
},account)
"fileMd5": hex.EncodeToString(id),
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s, "\n")),
"lazyCheck": "1",
}, account)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}

View File

@ -158,8 +158,8 @@ func (driver AliDrive) Files(path string, account *model.Account) ([]model.File,
return files, nil
}
func (driver AliDrive) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(path, account)
func (driver AliDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
@ -183,7 +183,7 @@ func (driver AliDrive) Link(path string, account *model.Account) (*base.Link, er
return nil, err
} else {
_ = model.SaveAccount(account)
return driver.Link(path, account)
return driver.Link(args, account)
}
}
return nil, fmt.Errorf("%s", e.Message)
@ -201,7 +201,7 @@ func (driver AliDrive) Path(path string, account *model.Account) (*model.File, [
return nil, nil, err
}
if !file.IsDir() {
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
return nil, nil, err
}

View File

@ -101,7 +101,8 @@ func (driver Alist) Files(path string, account *model.Account) ([]model.File, er
return files, nil
}
func (driver Alist) Link(path string, account *model.Account) (*base.Link, error) {
func (driver Alist) Link(args base.Args, account *model.Account) (*base.Link, error) {
path := args.Path
path = utils.ParsePath(path)
name := utils.Base(path)
flag := "d"

View File

@ -12,25 +12,45 @@ type DriverConfig struct {
Name string
OnlyProxy bool
NoLink bool // 必须本机返回的
ApiProxy bool // 使用API中转的
}
type Args struct {
Path string
IP string
}
type Driver interface {
// Config 配置
Config() DriverConfig
// Items 账号所需参数
Items() []Item
// Save 保存时处理
Save(account *model.Account, old *model.Account) error
// File 取文件
File(path string, account *model.Account) (*model.File, error)
// Files 取文件夹
Files(path string, account *model.Account) ([]model.File, error)
Link(path string, account *model.Account) (*Link, error)
// Link 取链接
Link(args Args, account *model.Account) (*Link, error)
// Path 取路径(文件或文件夹)
Path(path string, account *model.Account) (*model.File, []model.File, error)
// Proxy 代理处理
Proxy(c *gin.Context, account *model.Account)
// Preview 预览
Preview(path string, account *model.Account) (interface{}, error)
// MakeDir 创建文件夹
MakeDir(path string, account *model.Account) error
// Move 移动/改名
Move(src string, dst string, account *model.Account) error
// Copy 拷贝
Copy(src string, dst string, account *model.Account) error
// Delete 删除
Delete(path string, account *model.Account) error
// Upload 上传
Upload(file *model.FileStream, account *model.Account) error
// TODO
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
MakeDir(path string, account *model.Account) error
Move(src string, dst string, account *model.Account) error
Copy(src string, dst string, account *model.Account) error
Delete(path string, account *model.Account) error
Upload(file *model.FileStream, account *model.Account) error
}
type Item struct {
@ -90,12 +110,14 @@ func GetDrivers() map[string][]Item {
Label: "down_proxy_url",
Type: TypeString,
},
{
}, res[k]...)
if v.Config().ApiProxy {
res[k] = append(res[k], Item{
Name: "api_proxy_url",
Label: "api_proxy_url",
Type: TypeString,
},
}, res[k]...)
})
}
}
return res
}

View File

@ -146,7 +146,8 @@ func (driver FTP) Files(path string, account *model.Account) ([]model.File, erro
return res, nil
}
func (driver FTP) Link(path string, account *model.Account) (*base.Link, error) {
func (driver FTP) Link(args base.Args, account *model.Account) (*base.Link, error) {
path := args.Path
path = utils.ParsePath(path)
realPath := utils.Join(account.RootFolder, path)
conn, err := driver.Login(account)

View File

@ -15,8 +15,8 @@ type GoogleDrive struct{}
func (driver GoogleDrive) Config() base.DriverConfig {
return base.DriverConfig{
Name: "GoogleDrive",
OnlyProxy: true,
Name: "GoogleDrive",
OnlyProxy: true,
}
}
@ -116,8 +116,8 @@ func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.Fi
return files, nil
}
func (driver GoogleDrive) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(path, account)
func (driver GoogleDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
@ -136,7 +136,7 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (*base.Link,
_ = model.SaveAccount(account)
return nil, err
}
return driver.Link(path, account)
return driver.Link(args, account)
}
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
}
@ -144,7 +144,7 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (*base.Link,
Url: url + "&alt=media",
Headers: []base.Header{
{
Name: "Authorization",
Name: "Authorization",
Value: "Bearer " + account.AccessToken,
},
},
@ -198,4 +198,4 @@ func (driver GoogleDrive) Upload(file *model.FileStream, account *model.Account)
return base.ErrNotImplement
}
var _ base.Driver = (*GoogleDrive)(nil)
var _ base.Driver = (*GoogleDrive)(nil)

View File

@ -114,8 +114,8 @@ func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, e
return files, nil
}
func (driver Lanzou) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(path, account)
func (driver Lanzou) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
@ -145,7 +145,7 @@ func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []m
return nil, nil, err
}
if !file.IsDir() {
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
return nil, nil, err
}

View File

@ -123,8 +123,8 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
return files, nil
}
func (driver Native) Link(path string, account *model.Account) (*base.Link, error) {
fullPath := filepath.Join(account.RootFolder, path)
func (driver Native) Link(args base.Args, account *model.Account) (*base.Link, error) {
fullPath := filepath.Join(account.RootFolder, args.Path)
s, err := os.Stat(fullPath)
if err != nil {
return nil, err

View File

@ -14,10 +14,9 @@ import (
type Onedrive struct{}
func (driver Onedrive) Config() base.DriverConfig {
return base.DriverConfig{
Name: "Onedrive",
Name: "Onedrive",
OnlyProxy: false,
}
}
@ -173,8 +172,8 @@ func (driver Onedrive) Files(path string, account *model.Account) ([]model.File,
return files, nil
}
func (driver Onedrive) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(account, path)
func (driver Onedrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(account, args.Path)
if err != nil {
return nil, err
}
@ -232,4 +231,4 @@ func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) er
return base.ErrNotImplement
}
var _ base.Driver = (*Onedrive)(nil)
var _ base.Driver = (*Onedrive)(nil)

View File

@ -99,8 +99,8 @@ func (driver PikPak) Files(path string, account *model.Account) ([]model.File, e
return files, nil
}
func (driver PikPak) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(path, account)
func (driver PikPak) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}
@ -123,7 +123,7 @@ func (driver PikPak) Path(path string, account *model.Account) (*model.File, []m
return nil, nil, err
}
if !file.IsDir() {
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
return nil, nil, err
}

View File

@ -30,7 +30,7 @@ func Down(c *gin.Context) {
Proxy(c)
return
}
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -71,7 +71,7 @@ func Proxy(c *gin.Context) {
c.Redirect(302, link)
return
}
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -94,7 +94,7 @@ func Link(c *gin.Context) {
})
return
}
link, err := driver.Link(path, account)
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -123,7 +123,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
link += "?sign" + sign
}
} else {
link_, err := driver.Link(path_, account)
link_, err := driver.Link(base.Args{Path: path_}, account)
if err != nil {
return "", err
}
@ -145,7 +145,7 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error
if err != nil {
return err
}
return driver.MakeDir(path_,account)
return driver.MakeDir(path_, account)
}
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
@ -218,7 +218,7 @@ func moveFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
if srcAccount.Name != dstAccount.Name {
return http.StatusMethodNotAllowed, errInvalidDestination
}
err = driver.Move(srcPath,dstPath,srcAccount)
err = driver.Move(srcPath, dstPath, srcAccount)
if err != nil {
log.Debug(err)
return http.StatusInternalServerError, err
@ -248,7 +248,7 @@ func copyFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
// TODO 跨账号复制
return http.StatusMethodNotAllowed, errInvalidDestination
}
err = driver.Copy(srcPath,dstPath,srcAccount)
err = driver.Copy(srcPath, dstPath, srcAccount)
if err != nil {
return http.StatusInternalServerError, err
}