add remove file plugin callback #70

This commit is contained in:
tickstep
2025-03-03 11:46:08 +08:00
parent fdafcaed42
commit 87f951b5f5
5 changed files with 217 additions and 1 deletions
+50 -1
View File
@@ -19,10 +19,12 @@ import (
"github.com/tickstep/aliyunpan/cmder"
"github.com/tickstep/aliyunpan/cmder/cmdtable"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/tickstep/aliyunpan/internal/plugins"
"github.com/urfave/cli"
"os"
"path"
"strconv"
"strings"
)
func CmdRm() cli.Command {
@@ -75,6 +77,8 @@ func CmdRm() cli.Command {
// RunRemove 执行 批量删除文件/目录
func RunRemove(driveId string, paths ...string) {
activeUser := GetActiveUser()
pluginManger := plugins.NewPluginManager(config.GetPluginDir())
plugin, _ := pluginManger.GetPlugin()
cacheCleanDirs := []string{}
failedRmPaths := make([]string, 0, len(paths))
@@ -88,11 +92,54 @@ func RunRemove(driveId string, paths ...string) {
continue
}
if fileList == nil || len(fileList) == 0 {
// 失败,文件不存在
// 跳过,没有满足条件的文件
failedRmPaths = append(failedRmPaths, absolutePath)
continue
}
// 调用插件
approvedToRemoveFiles := []*aliyunpan.FileEntity{}
pluginParam := &plugins.RemoveFilePrepareParams{
Count: len(fileList),
Items: make([]*plugins.RemoveFilePrepareItem, 0),
}
for _, f := range fileList {
pluginParam.Items = append(pluginParam.Items, &plugins.RemoveFilePrepareItem{
DriveId: f.DriveId,
DriveFileId: f.FileId,
DriveFileName: f.FileName,
DriveFilePath: f.Path,
DriveFileSize: f.FileSize,
DriveFileType: f.FileType,
DriveFileUpdatedAt: f.UpdatedAt,
DriveFileCreatedAt: f.CreatedAt,
})
}
if removeFilePrepareResult, er := plugin.RemoveFilePrepareCallback(plugins.GetContext(config.Config.ActiveUser()), pluginParam); er == nil && removeFilePrepareResult != nil {
for _, f := range fileList {
matchResult := false
for _, r := range removeFilePrepareResult.Result {
if strings.Compare(f.FileId, r.DriveFileId) == 0 {
matchResult = true
if strings.Compare("yes", r.RemoveApproved) != 0 {
// skip this file
fmt.Printf("插件不允许删除该文件: %s\n", f.Path)
} else {
approvedToRemoveFiles = append(approvedToRemoveFiles, f)
}
break
}
}
if !matchResult {
// 该文件没有确认结果,则默认删除
approvedToRemoveFiles = append(approvedToRemoveFiles, f)
}
}
} else {
// 默认删除全部文件
approvedToRemoveFiles = fileList
}
for _, f := range approvedToRemoveFiles {
// 删除匹配的文件
fdr, err := activeUser.PanClient().OpenapiPanClient().FileDelete(&aliyunpan.FileBatchActionParam{
DriveId: driveId,
@@ -127,5 +174,7 @@ func RunRemove(driveId string, paths ...string) {
fmt.Println("操作成功, 以下文件/目录已删除, 可在云盘文件回收站找回: ")
pnt()
activeUser.DeleteCache(cacheCleanDirs)
} else {
fmt.Println("本次操作没有删除任何文件")
}
}
+4
View File
@@ -51,6 +51,10 @@ func (p *IdlePlugin) UserTokenRefreshFinishCallback(context *Context, params *Us
return nil
}
func (p *IdlePlugin) RemoveFilePrepareCallback(context *Context, params *RemoveFilePrepareParams) (*RemoveFilePrepareResult, error) {
return nil, nil
}
func (p *IdlePlugin) Stop() error {
return nil
}
+19
View File
@@ -261,6 +261,25 @@ func (js *JsPlugin) UserTokenRefreshFinishCallback(context *Context, params *Use
return nil
}
// RemoveFilePrepareCallback 删除文件前的回调函数
func (js *JsPlugin) RemoveFilePrepareCallback(context *Context, params *RemoveFilePrepareParams) (*RemoveFilePrepareResult, error) {
var fn func(*Context, *RemoveFilePrepareParams) (*RemoveFilePrepareResult, error)
if !js.isHandlerFuncExisted("removeFilePrepareCallback") {
return nil, nil
}
err := js.vm.ExportTo(js.vm.Get("removeFilePrepareCallback"), &fn)
if err != nil {
logger.Verboseln("Js函数映射到 Go 函数失败!")
return nil, nil
}
r, er := fn(context, params)
if er != nil {
logger.Verboseln(er)
return nil, er
}
return r, nil
}
func (js *JsPlugin) Stop() error {
return nil
}
+39
View File
@@ -166,6 +166,42 @@ type (
UpdatedAt string `json:"updatedAt"`
}
// RemoveFilePrepareParams 删除文件前的回调函数-参数
RemoveFilePrepareParams struct {
Count int `json:"count"`
Items []*RemoveFilePrepareItem `json:"items"`
}
RemoveFilePrepareItem struct {
// DriveId 网盘ID
DriveId string `json:"driveId"`
// DriveFileId 网盘文件的ID
DriveFileId string `json:"driveFileId"`
// DriveFileName 网盘文件名
DriveFileName string `json:"driveFileName"`
// DriveFilePath 网盘文件路径
DriveFilePath string `json:"driveFilePath"`
// DriveFileSize 网盘文件大小
DriveFileSize int64 `json:"driveFileSize"`
// DriveFileType 网盘文件类型,file-文件,folder-文件夹
DriveFileType string `json:"driveFileType"`
// DriveFileUpdatedAt 网盘文件修改时间,格式:2025-03-03 10:39:14
DriveFileUpdatedAt string `json:"driveFileUpdatedAt"`
// DriveFileCreatedAt 网盘文件创建时间,格式:2025-03-03 10:39:14
DriveFileCreatedAt string `json:"driveFileCreatedAt"`
}
// RemoveFilePrepareResult 删除文件前的回调函数-返回结果
RemoveFilePrepareResult struct {
Result []*RemoveFilePrepareResultItem `json:"result"`
}
RemoveFilePrepareResultItem struct {
// DriveId 网盘ID
DriveId string `json:"driveId"`
// DriveFileId 网盘文件的ID
DriveFileId string `json:"driveFileId"`
// RemoveApproved 确认该文件是否删除。yes-删除 no-不删除
RemoveApproved string `json:"removeApproved"`
}
// Plugin 插件接口
Plugin interface {
// Start 启动
@@ -198,6 +234,9 @@ type (
// UserTokenRefreshFinishCallback 用户Token刷新完成后回调函数
UserTokenRefreshFinishCallback(context *Context, params *UserTokenRefreshFinishParams) error
// RemoveFilePrepareCallback 删除文件前的回调函数
RemoveFilePrepareCallback(context *Context, params *RemoveFilePrepareParams) (*RemoveFilePrepareResult, error)
// Stop 停止
Stop() error
}