🎨 Optimize code structure

pull/548/head
微凉 2022-01-03 20:06:36 +08:00
parent e7ba289d06
commit 7cf30836bf
18 changed files with 269 additions and 342 deletions

View File

@ -218,33 +218,17 @@ func (driver Pan123) MakeDir(path string, account *model.Account) error {
}
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
base.Post, nil, nil, &data, nil, false, account)
//_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
if err == nil {
_ = base.DeleteCache(dir, account)
}
return err
}
func (driver Pan123) Move(src string, dst string, account *model.Account) error {
srcDir, _ := filepath.Split(src)
dstDir, dstName := filepath.Split(dst)
dstDir, _ := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
fileId, _ := strconv.Atoi(srcFile.Id)
// rename
if srcDir == dstDir {
data := base.Json{
"driveId": 0,
"fileId": fileId,
"fileName": dstName,
}
_, err = driver.Request("https://www.123pan.com/api/file/rename",
base.Post, nil, nil, &data, nil, false, account)
//_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
@ -256,12 +240,24 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error
}
_, err = driver.Request("https://www.123pan.com/api/file/mod_pid",
base.Post, nil, nil, &data, nil, false, account)
//_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
}
return err
}
func (driver Pan123) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
_ = base.DeleteCache(srcDir, account)
_ = base.DeleteCache(dstDir, account)
return err
}
fileId, _ := strconv.Atoi(srcFile.Id)
data := base.Json{
"driveId": 0,
"fileId": fileId,
"fileName": dstName,
}
_, err = driver.Request("https://www.123pan.com/api/file/rename",
base.Post, nil, nil, &data, nil, false, account)
return err
}
@ -281,10 +277,6 @@ func (driver Pan123) Delete(path string, account *model.Account) error {
}
_, err = driver.Request("https://www.123pan.com/api/file/trash",
base.Post, nil, nil, &data, nil, false, account)
//_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}

View File

@ -224,36 +224,16 @@ func (driver Cloud189) MakeDir(path string, account *model.Account) error {
"folderName": name,
}
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form, nil, account)
if err == nil {
_ = base.DeleteCache(dir, account)
}
return err
}
func (driver Cloud189) Move(src string, dst string, account *model.Account) error {
srcDir, _ := filepath.Split(src)
dstDir, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
// rename
if srcDir == dstDir {
url := "https://cloud.189.cn/api/open/file/renameFile.action"
idKey := "fileId"
nameKey := "destFileName"
if srcFile.IsDir() {
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
idKey = "folderId"
nameKey = "destFolderName"
}
form := map[string]string{
idKey: srcFile.Id,
nameKey: dstName,
}
_, err = driver.Request(url, "POST", form, nil, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
@ -279,11 +259,28 @@ func (driver Cloud189) Move(src string, dst string, account *model.Account) erro
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
return err
}
func (driver Cloud189) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
if err == nil {
_ = base.DeleteCache(srcDir, account)
_ = base.DeleteCache(dstDir, account)
url := "https://cloud.189.cn/api/open/file/renameFile.action"
idKey := "fileId"
nameKey := "destFileName"
if srcFile.IsDir() {
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
idKey = "folderId"
nameKey = "destFolderName"
}
form := map[string]string{
idKey: srcFile.Id,
nameKey: dstName,
}
_, err = driver.Request(url, "POST", form, nil, account)
return err
}
@ -318,9 +315,6 @@ func (driver Cloud189) Copy(src string, dst string, account *model.Account) erro
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
if err == nil {
_ = base.DeleteCache(dstDir, account)
}
return err
}
@ -351,9 +345,6 @@ func (driver Cloud189) Delete(path string, account *model.Account) error {
"taskInfos": string(taskInfosBytes),
}
_, 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)
}
return err
}

View File

@ -158,7 +158,7 @@ func (driver AliDrive) RefreshToken(account *model.Account) error {
return nil
}
func (driver AliDrive) Rename(fileId, name string, account *model.Account) error {
func (driver AliDrive) rename(fileId, name string, account *model.Account) error {
var resp base.Json
var e AliRespError
_, err := aliClient.R().SetResult(&resp).SetError(&e).
@ -179,7 +179,7 @@ func (driver AliDrive) Rename(fileId, name string, account *model.Account) error
return err
} else {
_ = model.SaveAccount(account)
return driver.Rename(fileId, name, account)
return driver.rename(fileId, name, account)
}
}
return fmt.Errorf("%s", e.Message)
@ -190,7 +190,7 @@ func (driver AliDrive) Rename(fileId, name string, account *model.Account) error
return fmt.Errorf("%+v", resp)
}
func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
func (driver AliDrive) batch(srcId, dstId string, account *model.Account) error {
var e AliRespError
res, err := aliClient.R().SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
@ -200,13 +200,13 @@ func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
"headers": base.Json{
"Content-Type": "application/json",
},
"method":"POST",
"id":srcId,
"body":base.Json{
"method": "POST",
"id": srcId,
"body": base.Json{
"drive_id": account.DriveId,
"file_id":srcId,
"to_drive_id":account.DriveId,
"to_parent_file_id":dstId,
"file_id": srcId,
"to_drive_id": account.DriveId,
"to_parent_file_id": dstId,
},
},
},
@ -222,7 +222,7 @@ func (driver AliDrive) Batch(srcId,dstId string, account *model.Account) error {
return err
} else {
_ = model.SaveAccount(account)
return driver.Batch(srcId, dstId, account)
return driver.batch(srcId, dstId, account)
}
}
return fmt.Errorf("%s", e.Message)

View File

@ -286,34 +286,32 @@ func (driver AliDrive) MakeDir(path string, account *model.Account) error {
return fmt.Errorf("%s", e.Message)
}
if resp["file_name"] == name {
_ = base.DeleteCache(dir, account)
return nil
}
return fmt.Errorf("%+v", resp)
}
func (driver AliDrive) Move(src string, dst string, account *model.Account) error {
srcDir, _ := filepath.Split(src)
dstDir, dstName := filepath.Split(dst)
dstDir, _ := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
// rename
if srcDir == dstDir {
err = driver.Rename(srcFile.Id, dstName, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
}
err = driver.Batch(srcFile.Id, dstDirFile.Id, account)
}
err = driver.batch(srcFile.Id, dstDirFile.Id, account)
return err
}
func (driver AliDrive) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
_ = base.DeleteCache(srcDir, account)
_ = base.DeleteCache(dstDir, account)
return err
}
err = driver.rename(srcFile.Id, dstName, account)
return err
}
@ -349,7 +347,6 @@ func (driver AliDrive) Delete(path string, account *model.Account) error {
return fmt.Errorf("%s", e.Message)
}
if res.StatusCode() == 204 {
_ = base.DeleteCache(utils.Dir(path), account)
return nil
}
return errors.New(res.String())
@ -471,7 +468,6 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return fmt.Errorf("%s", e.Message)
}
if resp2["file_id"] == resp.FileId {
_ = base.DeleteCache(file.ParentPath, account)
return nil
}
return fmt.Errorf("%+v", resp2)

View File

@ -171,6 +171,10 @@ func (driver Alist) Move(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}
func (driver Alist) Rename(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}
func (driver Alist) Copy(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}

View File

@ -46,6 +46,8 @@ type Driver interface {
MakeDir(path string, account *model.Account) error
// Move 移动/改名
Move(src string, dst string, account *model.Account) error
// Rename 改名
Rename(src string, dst string, account *model.Account) error
// Copy 拷贝
Copy(src string, dst string, account *model.Account) error
// Delete 删除

View File

@ -193,16 +193,10 @@ func (driver FTP) MakeDir(path string, account *model.Account) error {
}
defer func() { _ = conn.Quit() }()
err = conn.MakeDir(realPath)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func (driver FTP) Move(src string, dst string, account *model.Account) error {
//if utils.Dir(src) != utils.Dir(dst) {
// return base.ErrNotSupport
//}
realSrc := utils.Join(account.RootFolder, src)
realDst := utils.Join(account.RootFolder, dst)
conn, err := driver.Login(account)
@ -211,15 +205,13 @@ func (driver FTP) Move(src string, dst string, account *model.Account) error {
}
defer func() { _ = conn.Quit() }()
err = conn.Rename(realSrc, realDst)
if err != nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func (driver FTP) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
func (driver FTP) Copy(src string, dst string, account *model.Account) error {
return base.ErrNotSupport
}
@ -233,9 +225,6 @@ func (driver FTP) Delete(path string, account *model.Account) error {
}
defer func() { _ = conn.Quit() }()
err = conn.Delete(realPath)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -250,9 +239,6 @@ func (driver FTP) Upload(file *model.FileStream, account *model.Account) error {
}
defer func() { _ = conn.Quit() }()
err = conn.Stor(realPath, file)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -194,9 +194,6 @@ func (driver GoogleDrive) MakeDir(path string, account *model.Account) error {
"mimeType": "application/vnd.google-apps.folder",
}
_, err = driver.Request("https://www.googleapis.com/drive/v3/files", base.Post, nil, nil, nil, &data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -206,13 +203,6 @@ func (driver GoogleDrive) Move(src string, dst string, account *model.Account) e
if err != nil {
return err
}
if utils.Dir(src) == utils.Dir(dst) {
// rename
data := base.Json{
"name": utils.Base(dst),
}
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
} else {
dstParentFile, err := driver.File(utils.Dir(dst), account)
if err != nil {
return err
@ -222,13 +212,19 @@ func (driver GoogleDrive) Move(src string, dst string, account *model.Account) e
"removeParents": "root",
}
_, err = driver.Request(url, base.Patch, nil, query, nil, nil, nil, account)
return err
}
func (driver GoogleDrive) Rename(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
url := "https://www.googleapis.com/drive/v3/files/" + srcFile.Id
if err != nil {
return err
}
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
data := base.Json{
"name": utils.Base(dst),
}
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
return err
}
@ -243,9 +239,6 @@ func (driver GoogleDrive) Delete(path string, account *model.Account) error {
return err
}
_, err = driver.Request(url, base.Delete, nil, nil, nil, nil, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -286,9 +279,6 @@ func (driver GoogleDrive) Upload(file *model.FileStream, account *model.Account)
putUrl := res.Header().Get("location")
byteData, _ := ioutil.ReadAll(file)
_, err = driver.Request(putUrl, base.Put, nil, nil, nil, byteData, nil, account)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -170,6 +170,10 @@ func (driver *Lanzou) Move(src string, dst string, account *model.Account) error
return base.ErrNotImplement
}
func (driver *Lanzou) Rename(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}
func (driver *Lanzou) Copy(src string, dst string, account *model.Account) error {
return base.ErrNotImplement
}

View File

@ -164,6 +164,10 @@ func (driver Native) Move(src string, dst string, account *model.Account) error
return os.Rename(fullSrc, fullDst)
}
func (driver Native) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
func (driver Native) Copy(src string, dst string, account *model.Account) error {
fullSrc := filepath.Join(account.RootFolder, src)
fullDst := filepath.Join(account.RootFolder, dst)

View File

@ -218,14 +218,10 @@ func (driver Onedrive) MakeDir(path string, account *model.Account) error {
"@microsoft.graph.conflictBehavior": "rename",
}
_, err := driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func (driver Onedrive) Move(src string, dst string, account *model.Account) error {
log.Debugf("onedrive move")
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
if err != nil {
return err
@ -238,15 +234,13 @@ func (driver Onedrive) Move(src string, dst string, account *model.Account) erro
}
url := driver.GetMetaUrl(account, false, src)
_, err = driver.Request(url, base.Patch, nil, nil, nil, &data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func (driver Onedrive) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
func (driver Onedrive) Copy(src string, dst string, account *model.Account) error {
dstParentFile, err := driver.GetFile(account, utils.Dir(dst))
if err != nil {
@ -261,21 +255,12 @@ func (driver Onedrive) Copy(src string, dst string, account *model.Account) erro
}
url := driver.GetMetaUrl(account, false, src) + "/copy"
_, err = driver.Request(url, base.Post, nil, nil, nil, &data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func (driver Onedrive) Delete(path string, account *model.Account) error {
url := driver.GetMetaUrl(account, false, path)
_, err := driver.Request(url, base.Delete, nil, nil, nil, nil, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -289,9 +274,6 @@ func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) er
} else {
err = driver.UploadBig(file, account)
}
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -0,0 +1,59 @@
package operate
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
)
func MakeDir(driver base.Driver, account *model.Account, path string, clearCache bool) error {
err := driver.MakeDir(path, account)
if err == nil && clearCache {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func Move(driver base.Driver, account *model.Account, src, dst string, clearCache bool) error {
rename := false
if utils.Dir(src) == utils.Dir(dst) {
rename = true
}
var err error
if rename {
err = driver.Rename(src, dst, account)
} else {
err = driver.Move(src, dst, account)
}
if err == nil && clearCache {
_ = base.DeleteCache(utils.Dir(src), account)
if !rename {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func Copy(driver base.Driver, account *model.Account, src, dst string, clearCache bool) error {
err := driver.Copy(src, dst, account)
if err == nil && clearCache {
_ = base.DeleteCache(utils.Dir(dst), account)
}
return err
}
func Delete(driver base.Driver, account *model.Account, path string, clearCache bool) error {
err := driver.Delete(path, account)
if err == nil && clearCache {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func Upload(driver base.Driver, account *model.Account, file *model.FileStream, clearCache bool) error {
err := driver.Upload(file, account)
if err == nil && clearCache {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -159,26 +159,15 @@ func (driver PikPak) MakeDir(path string, account *model.Account) error {
"parent_id": parentFile.Id,
"name": name,
}, nil, account)
if err == nil {
_ = base.DeleteCache(dir, account)
}
return err
}
func (driver PikPak) Move(src string, dst string, account *model.Account) error {
srcDir, _ := filepath.Split(src)
dstDir, dstName := filepath.Split(dst)
dstDir, _ := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
// rename
if srcDir == dstDir {
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files/"+srcFile.Id, base.Patch, nil, &base.Json{
"name": dstName,
}, nil, account)
} else {
// move
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
@ -189,11 +178,18 @@ func (driver PikPak) Move(src string, dst string, account *model.Account) error
"parent_id": dstDirFile.Id,
},
}, nil, account)
return err
}
func (driver PikPak) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
if err == nil {
_ = base.DeleteCache(srcDir, account)
_ = base.DeleteCache(dstDir, account)
}
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files/"+srcFile.Id, base.Patch, nil, &base.Json{
"name": dstName,
}, nil, account)
return err
}
@ -212,9 +208,6 @@ func (driver PikPak) Copy(src string, dst string, account *model.Account) error
"parent_id": dstDirFile.Id,
},
}, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(dst), account)
}
return err
}
@ -226,9 +219,6 @@ func (driver PikPak) Delete(path string, account *model.Account) error {
_, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files:batchTrash", base.Post, nil, &base.Json{
"ids": []string{file.Id},
}, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -274,9 +264,6 @@ func (driver PikPak) Upload(file *model.FileStream, account *model.Account) erro
return err
}
err = bucket.PutObjectWithURL(signedURL, file)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -193,6 +193,10 @@ func (driver S3) Move(src string, dst string, account *model.Account) error {
return driver.Delete(src, account)
}
func (driver S3) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
func (driver S3) Copy(src string, dst string, account *model.Account) error {
client, err := driver.GetClient(account)
if err != nil {
@ -210,9 +214,6 @@ func (driver S3) Copy(src string, dst string, account *model.Account) error {
Key: &dstKey,
}
_, err = client.CopyObject(input)
if err == nil {
_ = base.DeleteCache(dst, account)
}
return err
}
@ -231,9 +232,6 @@ func (driver S3) Delete(path string, account *model.Account) error {
Key: &key,
}
_, err = client.DeleteObject(input)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -253,9 +251,6 @@ func (driver S3) Upload(file *model.FileStream, account *model.Account) error {
Body: file,
}
_, err := uploader.Upload(input)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -169,9 +169,6 @@ func (driver Shandian) MakeDir(path string, account *model.Account) error {
"name": utils.Base(path),
}
_, err = driver.Post("https://shandianpan.com/api/pan/mkdir", data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -180,14 +177,6 @@ func (driver Shandian) Move(src string, dst string, account *model.Account) erro
if err != nil {
return err
}
if utils.Dir(src) == utils.Dir(dst) {
// rename
data := map[string]interface{}{
"id": srcFile.Id,
"name": utils.Base(dst),
}
_, err = driver.Post("https://shandianpan.com/api/pan/change", data, nil, account)
} else {
dstParentFile, err := driver.File(utils.Dir(dst), account)
if err != nil {
return err
@ -197,13 +186,19 @@ func (driver Shandian) Move(src string, dst string, account *model.Account) erro
"to_id": dstParentFile.Id,
}
_, err = driver.Post("https://shandianpan.com/api/pan/move", data, nil, account)
return err
}
func (driver Shandian) Rename(src string, dst string, account *model.Account) error {
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
data := map[string]interface{}{
"id": srcFile.Id,
"name": utils.Base(dst),
}
_, err = driver.Post("https://shandianpan.com/api/pan/change", data, nil, account)
return err
}
@ -220,9 +215,6 @@ func (driver Shandian) Delete(path string, account *model.Account) error {
"id": file.Id,
}
_, err = driver.Post("https://shandianpan.com/api/pan/recycle-in", data, nil, account)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -272,7 +264,6 @@ func (driver Shandian) Upload(file *model.FileStream, account *model.Account) er
return err
}
if r.Code == 0 {
_ = base.DeleteCache(file.ParentPath, account)
return nil
}
return errors.New(r.Msg)

View File

@ -145,39 +145,28 @@ func (driver WebDav) Preview(path string, account *model.Account) (interface{},
func (driver WebDav) MakeDir(path string, account *model.Account) error {
c := driver.NewClient(account)
err := c.MkdirAll(driver.WebDavPath(path), 0644)
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
func (driver WebDav) Move(src string, dst string, account *model.Account) error {
c := driver.NewClient(account)
err := c.Rename(driver.WebDavPath(src), driver.WebDavPath(dst), true)
if err == nil {
_ = base.DeleteCache(utils.Dir(src), account)
if utils.Dir(src) != utils.Dir(dst) {
_ = base.DeleteCache(utils.Dir(dst), account)
}
}
return err
}
func (driver WebDav) Rename(src string, dst string, account *model.Account) error {
return driver.Move(src, dst, account)
}
func (driver WebDav) Copy(src string, dst string, account *model.Account) error {
c := driver.NewClient(account)
err := c.Copy(driver.WebDavPath(src), driver.WebDavPath(dst), true)
if err == nil {
_ = base.DeleteCache(utils.Dir(dst), account)
}
return err
}
func (driver WebDav) Delete(path string, account *model.Account) error {
c := driver.NewClient(account)
err := c.RemoveAll(driver.WebDavPath(path))
if err == nil {
_ = base.DeleteCache(utils.Dir(path), account)
}
return err
}
@ -188,9 +177,6 @@ func (driver WebDav) Upload(file *model.FileStream, account *model.Account) erro
c := driver.NewClient(account)
path := utils.Join(file.ParentPath, file.Name)
err := c.WriteStream(driver.WebDavPath(path), file, 0644)
if err == nil {
_ = base.DeleteCache(file.ParentPath, account)
}
return err
}

View File

@ -3,6 +3,7 @@ package controllers
import (
"errors"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
@ -48,7 +49,7 @@ func UploadFile(c *gin.Context) {
Name: file.Filename,
MIMEType: file.Header.Get("Content-Type"),
}
err = driver.Upload(&fileStream, account)
err = operate.Upload(driver, account, &fileStream, true)
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -9,7 +9,9 @@ import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"net"
@ -22,30 +24,6 @@ import (
type FileSystem struct{}
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
var internalPath, name string
switch model.AccountsCount() {
case 0:
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
case 1:
internalPath = rawPath
break
default:
paths := strings.Split(rawPath, "/")
internalPath = "/" + strings.Join(paths[2:], "/")
name = paths[1]
}
account, ok := model.GetAccount(name)
if !ok {
return nil, "", nil, fmt.Errorf("no [%s] account", name)
}
driver, ok := base.GetDriver(account.Type)
if !ok {
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
}
return &account, internalPath, driver, nil
}
func (fs *FileSystem) File(rawPath string) (*model.File, error) {
rawPath = utils.ParsePath(rawPath)
if model.AccountsCount() > 1 && rawPath == "/" {
@ -58,7 +36,7 @@ func (fs *FileSystem) File(rawPath string) (*model.File, error) {
UpdatedAt: &now,
}, nil
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return nil, err
}
@ -74,34 +52,13 @@ func (fs *FileSystem) Files(rawPath string) ([]model.File, error) {
}
return files, nil
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return nil, err
}
return driver.Files(path_, account)
}
//func GetPW(path string, name string) string {
// if !conf.CheckDown {
// return ""
// }
// meta, err := model.GetMetaByPath(path)
// if err == nil {
// if meta.Password != "" {
// return utils.SignWithPassword(name, meta.Password)
// }
// return ""
// } else {
// if !conf.CheckParent {
// return ""
// }
// if path == "/" {
// return ""
// }
// return GetPW(utils.Dir(path), name)
// }
//}
func ClientIP(r *http.Request) string {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
@ -127,7 +84,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
if model.AccountsCount() > 1 && rawPath == "/" {
// error
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return "", err
}
@ -161,12 +118,12 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error
if model.AccountsCount() > 1 && len(strings.Split(rawPath, "/")) < 2 {
return ErrNotImplemented
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return err
}
log.Debugf("mkdir: %s", path_)
return driver.MakeDir(path_, account)
return operate.MakeDir(driver, account, path_, true)
}
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
@ -174,7 +131,7 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin
if model.AccountsCount() > 1 && rawPath == "/" {
return ErrNotImplemented
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return err
}
@ -191,7 +148,7 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin
Name: fileName,
ParentPath: filePath,
}
return driver.Upload(&fileData, account)
return operate.Upload(driver, account, &fileData, true)
}
func (fs *FileSystem) Delete(rawPath string) error {
@ -202,11 +159,11 @@ func (fs *FileSystem) Delete(rawPath string) error {
if model.AccountsCount() > 1 && len(strings.Split(rawPath, "/")) < 2 {
return ErrNotImplemented
}
account, path_, driver, err := ParsePath(rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
return err
}
return driver.Delete(path_, account)
return operate.Delete(driver, account, path_, true)
}
// slashClean is equivalent to but slightly more efficient than
@ -228,18 +185,18 @@ func moveFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
if src == dst {
return http.StatusMethodNotAllowed, errDestinationEqualsSource
}
srcAccount, srcPath, driver, err := ParsePath(src)
srcAccount, srcPath, driver, err := common.ParsePath(src)
if err != nil {
return http.StatusMethodNotAllowed, err
}
dstAccount, dstPath, _, err := ParsePath(dst)
dstAccount, dstPath, _, err := common.ParsePath(dst)
if err != nil {
return http.StatusMethodNotAllowed, err
}
if srcAccount.Name != dstAccount.Name {
return http.StatusMethodNotAllowed, errInvalidDestination
}
err = driver.Move(srcPath, dstPath, srcAccount)
err = operate.Move(driver, srcAccount, srcPath, dstPath, true)
if err != nil {
log.Debug(err)
return http.StatusInternalServerError, err
@ -257,11 +214,11 @@ func copyFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
if src == dst {
return http.StatusMethodNotAllowed, errDestinationEqualsSource
}
srcAccount, srcPath, driver, err := ParsePath(src)
srcAccount, srcPath, driver, err := common.ParsePath(src)
if err != nil {
return http.StatusMethodNotAllowed, err
}
dstAccount, dstPath, _, err := ParsePath(dst)
dstAccount, dstPath, _, err := common.ParsePath(dst)
if err != nil {
return http.StatusMethodNotAllowed, err
}
@ -269,7 +226,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 = operate.Copy(driver, srcAccount, srcPath, dstPath, true)
if err != nil {
return http.StatusInternalServerError, err
}