diff --git a/drivers/123/driver.go b/drivers/123/driver.go index 989c9c77..d999d895 100644 --- a/drivers/123/driver.go +++ b/drivers/123/driver.go @@ -218,50 +218,46 @@ 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 - } - parentFileId, _ := strconv.Atoi(dstDirFile.Id) - data := base.Json{ - "fileId": fileId, - "parentFileId": parentFileId, - } - _, 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) - } + + dstDirFile, err := driver.File(dstDir, account) if err != nil { - _ = base.DeleteCache(srcDir, account) - _ = base.DeleteCache(dstDir, account) + return err } + parentFileId, _ := strconv.Atoi(dstDirFile.Id) + data := base.Json{ + "fileId": fileId, + "parentFileId": parentFileId, + } + _, err = driver.Request("https://www.123pan.com/api/file/mod_pid", + base.Post, nil, nil, &data, nil, false, 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 { + 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 } diff --git a/drivers/189/driver.go b/drivers/189/driver.go index 87f4bc9b..eea3cdac 100644 --- a/drivers/189/driver.go +++ b/drivers/189/driver.go @@ -224,66 +224,63 @@ 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 - } - isFolder := 0 - if srcFile.IsDir() { - isFolder = 1 - } - taskInfos := []base.Json{ - { - "fileId": srcFile.Id, - "fileName": dstName, - "isFolder": isFolder, - }, - } - taskInfosBytes, err := json.Marshal(taskInfos) - if err != nil { - return err - } - form := map[string]string{ - "type": "MOVE", - "targetFolderId": dstDirFile.Id, - "taskInfos": string(taskInfosBytes), - } - _, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account) + + dstDirFile, err := driver.File(dstDir, account) + if err != nil { + return err } - if err == nil { - _ = base.DeleteCache(srcDir, account) - _ = base.DeleteCache(dstDir, account) + isFolder := 0 + if srcFile.IsDir() { + isFolder = 1 } + taskInfos := []base.Json{ + { + "fileId": srcFile.Id, + "fileName": dstName, + "isFolder": isFolder, + }, + } + taskInfosBytes, err := json.Marshal(taskInfos) + if err != nil { + return err + } + form := map[string]string{ + "type": "MOVE", + "targetFolderId": dstDirFile.Id, + "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 + } + 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 } diff --git a/drivers/alidrive/alidrive.go b/drivers/alidrive/alidrive.go index 58a367dd..a0b6fdd3 100644 --- a/drivers/alidrive/alidrive.go +++ b/drivers/alidrive/alidrive.go @@ -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{ - "drive_id": account.DriveId, - "file_id":srcId, - "to_drive_id":account.DriveId, - "to_parent_file_id":dstId, + "method": "POST", + "id": srcId, + "body": base.Json{ + "drive_id": account.DriveId, + "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) diff --git a/drivers/alidrive/driver.go b/drivers/alidrive/driver.go index f5f5c052..4fb3b4ff 100644 --- a/drivers/alidrive/driver.go +++ b/drivers/alidrive/driver.go @@ -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) - } + dstDirFile, err := driver.File(dstDir, account) if err != nil { - _ = base.DeleteCache(srcDir, account) - _ = base.DeleteCache(dstDir, account) + return err } + 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 { + 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) diff --git a/drivers/alist/driver.go b/drivers/alist/driver.go index f36c92fc..c915e3ac 100644 --- a/drivers/alist/driver.go +++ b/drivers/alist/driver.go @@ -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 } diff --git a/drivers/base/driver.go b/drivers/base/driver.go index 6fa3bd28..d88a47cf 100644 --- a/drivers/base/driver.go +++ b/drivers/base/driver.go @@ -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 删除 diff --git a/drivers/ftp/driver.go b/drivers/ftp/driver.go index 1708bb46..1527c22d 100644 --- a/drivers/ftp/driver.go +++ b/drivers/ftp/driver.go @@ -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 } diff --git a/drivers/google/driver.go b/drivers/google/driver.go index 50559304..f3caebb6 100644 --- a/drivers/google/driver.go +++ b/drivers/google/driver.go @@ -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,29 +203,28 @@ 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 - } - query := map[string]string{ - "addParents": dstParentFile.Id, - "removeParents": "root", - } - _, err = driver.Request(url, base.Patch, nil, query, nil, nil, nil, account) + dstParentFile, err := driver.File(utils.Dir(dst), 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) - } + query := map[string]string{ + "addParents": dstParentFile.Id, + "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 + } + 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 } diff --git a/drivers/lanzou/driver.go b/drivers/lanzou/driver.go index 760ec1d2..edcbf069 100644 --- a/drivers/lanzou/driver.go +++ b/drivers/lanzou/driver.go @@ -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 } diff --git a/drivers/native/driver.go b/drivers/native/driver.go index 88e82b06..9598c276 100644 --- a/drivers/native/driver.go +++ b/drivers/native/driver.go @@ -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) diff --git a/drivers/onedrive/driver.go b/drivers/onedrive/driver.go index ebb8c695..5fa0d96e 100644 --- a/drivers/onedrive/driver.go +++ b/drivers/onedrive/driver.go @@ -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 } diff --git a/drivers/operate/operate.go b/drivers/operate/operate.go new file mode 100644 index 00000000..0c34582d --- /dev/null +++ b/drivers/operate/operate.go @@ -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 +} diff --git a/drivers/pikpak/driver.go b/drivers/pikpak/driver.go index be3a9446..d6bd4051 100644 --- a/drivers/pikpak/driver.go +++ b/drivers/pikpak/driver.go @@ -159,41 +159,37 @@ 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 - } - _, err = driver.Request("https://api-drive.mypikpak.com/drive/v1/files:batchMove", base.Post, nil, &base.Json{ - "ids": []string{srcFile.Id}, - "to": base.Json{ - "parent_id": dstDirFile.Id, - }, - }, nil, account) + dstDirFile, err := driver.File(dstDir, 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:batchMove", base.Post, nil, &base.Json{ + "ids": []string{srcFile.Id}, + "to": base.Json{ + "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 } + _, 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 } diff --git a/drivers/s3/driver.go b/drivers/s3/driver.go index 8c0fceb6..870e83de 100644 --- a/drivers/s3/driver.go +++ b/drivers/s3/driver.go @@ -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 } diff --git a/drivers/shandian/driver.go b/drivers/shandian/driver.go index ea16d0db..d44b8f3c 100644 --- a/drivers/shandian/driver.go +++ b/drivers/shandian/driver.go @@ -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,30 +177,28 @@ 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 - } - data := map[string]interface{}{ - "id": srcFile.Id, - "to_id": dstParentFile.Id, - } - _, err = driver.Post("https://shandianpan.com/api/pan/move", data, nil, account) + dstParentFile, err := driver.File(utils.Dir(dst), 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, + "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 + } + 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) diff --git a/drivers/webdav/driver.go b/drivers/webdav/driver.go index 3c14db36..f3305317 100644 --- a/drivers/webdav/driver.go +++ b/drivers/webdav/driver.go @@ -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 } diff --git a/server/controllers/file.go b/server/controllers/file.go index b71035c7..39380834 100644 --- a/server/controllers/file.go +++ b/server/controllers/file.go @@ -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 diff --git a/server/webdav/file.go b/server/webdav/file.go index fdb64d03..8a68de80 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -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 }