mirror of
https://github.com/tickstep/aliyunpan.git
synced 2025-01-23 14:32:14 +08:00
add upload file finish plugin hook
This commit is contained in:
parent
0887b9f160
commit
5572f844ed
@ -75,4 +75,54 @@ function uploadFilePrepareCallback(context, params) {
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// 函数说明:上传文件结束的回调函数
|
||||
//
|
||||
// 参数说明
|
||||
// context - 当前调用的上下文信息
|
||||
// {
|
||||
// "appName": "aliyunpan",
|
||||
// "version": "v0.1.3",
|
||||
// "userId": "11001d48564f43b3bc5662874f04bb11",
|
||||
// "nickname": "tickstep",
|
||||
// "fileDriveId": "19519111",
|
||||
// "albumDriveId": "29519122"
|
||||
// }
|
||||
// appName - 应用名称,当前固定为aliyunpan
|
||||
// version - 版本号
|
||||
// userId - 当前登录用户的ID
|
||||
// nickname - 用户昵称
|
||||
// fileDriveId - 用户文件网盘ID
|
||||
// albumDriveId - 用户相册网盘ID
|
||||
//
|
||||
// params - 文件上传前参数
|
||||
// {
|
||||
// "localFilePath": "D:\\Program Files\\aliyunpan\\Downloads\\token.bat",
|
||||
// "localFileName": "token.bat",
|
||||
// "localFileSize": 125330,
|
||||
// "localFileType": "file",
|
||||
// "localFileUpdatedAt": "2022-04-14 07:05:12",
|
||||
// "localFileSha1": "08FBE28A5B8791A2F50225E2EC5CEEC3C7955A11",
|
||||
// "uploadResult": "success",
|
||||
// "driveId": "19519221",
|
||||
// "driveFilePath": "/tmp/test/aliyunpan/Downloads/token.bat"
|
||||
// }
|
||||
// localFilePath - 本地文件绝对完整路径
|
||||
// localFileName - 本地文件名
|
||||
// localFileSize - 本地文件大小,单位B
|
||||
// localFileType - 本地文件类型,file-文件,folder-文件夹
|
||||
// localFileUpdatedAt - 文件修改时间
|
||||
// localFileSha1 - 本地文件的SHA1。这个值不一定会有
|
||||
// uploadResult - 上传结果,success-成功,fail-失败
|
||||
// driveId - 目标网盘ID
|
||||
// driveFilePath - 文件网盘保存的绝对路径
|
||||
//
|
||||
// 返回值说明
|
||||
// (没有返回值)
|
||||
// ------------------------------------------------------------------------------------------
|
||||
function uploadFileFinishCallback(context, params) {
|
||||
console.log(params)
|
||||
}
|
@ -15,6 +15,7 @@ package panupload
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tickstep/aliyunpan/internal/plugins"
|
||||
"github.com/tickstep/library-go/logger"
|
||||
"os"
|
||||
"path"
|
||||
@ -233,6 +234,9 @@ func (utu *UploadTaskUnit) OnRetry(lastRunResult *taskframework.TaskUnitRunResul
|
||||
}
|
||||
|
||||
func (utu *UploadTaskUnit) OnSuccess(lastRunResult *taskframework.TaskUnitRunResult) {
|
||||
// 执行插件
|
||||
utu.pluginCallback("success")
|
||||
|
||||
//文件上传成功
|
||||
if utu.FolderSyncDb == nil || lastRunResult == ResultLocalFileNotUpdated { //不需要更新数据库
|
||||
return
|
||||
@ -259,13 +263,36 @@ func (utu *UploadTaskUnit) OnSuccess(lastRunResult *taskframework.TaskUnitRunRes
|
||||
|
||||
func (utu *UploadTaskUnit) OnFailed(lastRunResult *taskframework.TaskUnitRunResult) {
|
||||
// 失败
|
||||
utu.pluginCallback("fail")
|
||||
}
|
||||
|
||||
func (utu *UploadTaskUnit) pluginCallback(result string) {
|
||||
pluginManger := plugins.NewPluginManager(config.GetPluginDir())
|
||||
plugin, _ := pluginManger.GetPlugin()
|
||||
_, fileName := filepath.Split(utu.LocalFileChecksum.Path)
|
||||
pluginParam := &plugins.UploadFileFinishParams{
|
||||
LocalFilePath: utu.LocalFileChecksum.Path,
|
||||
LocalFileName: fileName,
|
||||
LocalFileSize: utu.LocalFileChecksum.LocalFileMeta.Length,
|
||||
LocalFileType: "file",
|
||||
LocalFileUpdatedAt: time.Unix(utu.LocalFileChecksum.LocalFileMeta.ModTime, 0).Format("2006-01-02 15:04:05"),
|
||||
LocalFileSha1: utu.LocalFileChecksum.LocalFileMeta.SHA1,
|
||||
UploadResult: result,
|
||||
DriveId: utu.DriveId,
|
||||
DriveFilePath: utu.panDir + "/" + utu.panFile,
|
||||
}
|
||||
if er := plugin.UploadFileFinishCallback(plugins.GetContext(config.Config.ActiveUser()), pluginParam); er != nil {
|
||||
logger.Verboseln("插件UploadFileFinishCallback调用失败: {}", er)
|
||||
} else {
|
||||
logger.Verboseln("插件UploadFileFinishCallback调用成功")
|
||||
}
|
||||
}
|
||||
|
||||
var ResultLocalFileNotUpdated = &taskframework.TaskUnitRunResult{ResultCode: 1, Succeed: true, ResultMessage: "本地文件未更新,无需上传!"}
|
||||
var ResultUpdateLocalDatabase = &taskframework.TaskUnitRunResult{ResultCode: 2, Succeed: true, ResultMessage: "本地文件和云端文件MD5一致,无需上传!"}
|
||||
|
||||
func (utu *UploadTaskUnit) OnComplete(lastRunResult *taskframework.TaskUnitRunResult) {
|
||||
|
||||
// 任务结束,可能成功也可能失败
|
||||
}
|
||||
|
||||
func (utu *UploadTaskUnit) RetryWait() time.Duration {
|
||||
|
@ -86,6 +86,25 @@ func (js *JsPlugin) UploadFilePrepareCallback(context *Context, params *UploadFi
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// UploadFileFinishCallback 上传文件完成的回调函数
|
||||
func (js *JsPlugin) UploadFileFinishCallback(context *Context, params *UploadFileFinishParams) error {
|
||||
var fn func(*Context, *UploadFileFinishParams) error
|
||||
if !js.isHandlerFuncExisted("uploadFileFinishCallback") {
|
||||
return nil
|
||||
}
|
||||
err := js.vm.ExportTo(js.vm.Get("uploadFileFinishCallback"), &fn)
|
||||
if err != nil {
|
||||
logger.Verboseln("Js函数映射到 Go 函数失败!")
|
||||
return nil
|
||||
}
|
||||
er := fn(context, params)
|
||||
if er != nil {
|
||||
logger.Verboseln(er)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (js *JsPlugin) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
@ -43,6 +43,19 @@ type (
|
||||
DriveFilePath string `json:"driveFilePath"`
|
||||
}
|
||||
|
||||
// UploadFileFinishParams 上传文件结束的回调函数-参数
|
||||
UploadFileFinishParams struct {
|
||||
LocalFilePath string `json:"localFilePath"`
|
||||
LocalFileName string `json:"localFileName"`
|
||||
LocalFileSize int64 `json:"localFileSize"`
|
||||
LocalFileType string `json:"localFileType"`
|
||||
LocalFileUpdatedAt string `json:"localFileUpdatedAt"`
|
||||
LocalFileSha1 string `json:"localFileSha1"`
|
||||
UploadResult string `json:"uploadResult"`
|
||||
DriveId string `json:"driveId"`
|
||||
DriveFilePath string `json:"driveFilePath"`
|
||||
}
|
||||
|
||||
// Plugin 插件接口
|
||||
Plugin interface {
|
||||
// Start 启动
|
||||
@ -51,6 +64,9 @@ type (
|
||||
// UploadFilePrepareCallback 上传文件前的回调函数
|
||||
UploadFilePrepareCallback(context *Context, params *UploadFilePrepareParams) (*UploadFilePrepareResult, error)
|
||||
|
||||
// UploadFileFinishCallback 上传文件完成的回调函数
|
||||
UploadFileFinishCallback(context *Context, params *UploadFileFinishParams) error
|
||||
|
||||
// Stop 停止
|
||||
Stop() error
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user