From 7f5e19812eda86337cf2c5fce41110523495cab3 Mon Sep 17 00:00:00 2001 From: sowevo Date: Fri, 26 Jan 2024 11:40:04 +0800 Subject: [PATCH 1/3] add skip args --- internal/command/upload.go | 27 ++++++++++------- .../functions/panupload/upload_task_unit.go | 29 ++++++++++++++----- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/internal/command/upload.go b/internal/command/upload.go index d5b234b..31fbc13 100644 --- a/internal/command/upload.go +++ b/internal/command/upload.go @@ -62,6 +62,7 @@ type ( NoRapidUpload bool ShowProgress bool IsOverwrite bool // 覆盖已存在的文件,如果同名文件已存在则移到回收站里 + IsSkipSameName bool // 跳过已存在的文件,即使文件内容不一致(不检查SHA1) DriveId string ExcludeNames []string // 排除的文件名,包括文件夹和文件。即这些文件/文件夹不进行上传,支持正则表达式 BlockSize int64 // 分片大小 @@ -92,6 +93,10 @@ var UploadFlags = []cli.Flag{ Name: "ow", Usage: "overwrite, 覆盖已存在的同名文件,注意已存在的文件会被移到回收站", }, + cli.BoolFlag{ + Name: "skip", + Usage: "skip same name, 跳过已存在的同名文件,即使文件内容不一致(不检查SHA1)", + }, cli.BoolFlag{ Name: "norapid", Usage: "不检测秒传。跳过费时的SHA1计算直接上传", @@ -186,16 +191,17 @@ func CmdUpload() cli.Command { //} RunUpload(subArgs[:c.NArg()-1], subArgs[c.NArg()-1], &UploadOptions{ - AllParallel: c.Int("p"), // 多文件上传的时候,允许同时并行上传的文件数量 - Parallel: 1, // 一个文件同时多少个线程并发上传的数量。阿里云盘只支持单线程按顺序进行文件part数据上传,所以只能是1 - MaxRetry: c.Int("retry"), - MaxTimeoutSec: timeout, - NoRapidUpload: c.Bool("norapid"), - ShowProgress: !c.Bool("np"), - IsOverwrite: c.Bool("ow"), - DriveId: parseDriveId(c), - ExcludeNames: c.StringSlice("exn"), - BlockSize: int64(c.Int("bs") * 1024), + AllParallel: c.Int("p"), // 多文件上传的时候,允许同时并行上传的文件数量 + Parallel: 1, // 一个文件同时多少个线程并发上传的数量。阿里云盘只支持单线程按顺序进行文件part数据上传,所以只能是1 + MaxRetry: c.Int("retry"), + MaxTimeoutSec: timeout, + NoRapidUpload: c.Bool("norapid"), + ShowProgress: !c.Bool("np"), + IsOverwrite: c.Bool("ow"), + IsSkipSameName: c.Bool("skip"), + DriveId: parseDriveId(c), + ExcludeNames: c.StringSlice("exn"), + BlockSize: int64(c.Int("bs") * 1024), }) // 释放文件锁 @@ -441,6 +447,7 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) { UploadStatistic: statistic, ShowProgress: opt.ShowProgress, IsOverwrite: opt.IsOverwrite, + IsSkipSameName: opt.IsSkipSameName, UseInternalUrl: opt.UseInternalUrl, GlobalSpeedsStat: globalSpeedsStat, FileRecorder: fileRecorder, diff --git a/internal/functions/panupload/upload_task_unit.go b/internal/functions/panupload/upload_task_unit.go index 80b9275..4a5ea30 100644 --- a/internal/functions/panupload/upload_task_unit.go +++ b/internal/functions/panupload/upload_task_unit.go @@ -64,8 +64,9 @@ type ( panFile string state *uploader.InstanceState - ShowProgress bool - IsOverwrite bool // 覆盖已存在的文件,如果同名文件已存在则移到回收站里 + ShowProgress bool + IsOverwrite bool // 覆盖已存在的文件,如果同名文件已存在则移到回收站里 + IsSkipSameName bool // 跳过已存在的文件,即使文件内容不一致(不检查SHA1) // 是否使用内置链接 UseInternalUrl bool @@ -327,6 +328,7 @@ func (utu *UploadTaskUnit) Run() (result *taskframework.TaskUnitRunResult) { var apierr *apierror.ApiError var rs *aliyunpan.MkdirResult + var efi *aliyunpan.FileEntity var appCreateUploadFileParam *aliyunpan.CreateFileUploadParam var sha1Str string var contentHashName string @@ -398,6 +400,23 @@ StepUploadPrepareUpload: proofCode = "" contentHashName = "sha1" checkNameMode = "auto_rename" + // 如果启用了 覆盖/跳过 已存在的文件,则需要提前检查文件是否存在 + if utu.IsOverwrite || utu.IsSkipSameName { + efi, apierr = utu.PanClient.FileInfoByPath(utu.DriveId, utu.SavePath) + if apierr != nil && apierr.Code != apierror.ApiCodeFileNotFoundCode { + result.Err = apierr + result.ResultMessage = "检测同名文件失败" + return + } + } + if utu.IsSkipSameName { + if efi != nil && efi.FileId != "" { + result.Succeed = true + result.Extra = efi + fmt.Printf("[%s] %s 检测到同名文件,跳过上传: %s\n", utu.taskInfo.Id(), time.Now().Format("2006-01-02 15:04:06"), utu.SavePath) + return + } + } if !utu.NoRapidUpload { // 计算文件SHA1 fmt.Printf("[%s] %s 正在计算文件SHA1: %s\n", utu.taskInfo.Id(), time.Now().Format("2006-01-02 15:04:06"), utu.LocalFileChecksum.Path.LogicPath) @@ -421,12 +440,6 @@ StepUploadPrepareUpload: if utu.IsOverwrite { // 标记覆盖旧同名文件 // 检查同名文件是否存在 - efi, apierr := utu.PanClient.FileInfoByPath(utu.DriveId, utu.SavePath) - if apierr != nil && apierr.Code != apierror.ApiCodeFileNotFoundCode { - result.Err = apierr - result.ResultMessage = "检测同名文件失败" - return - } if efi != nil && efi.FileId != "" { if strings.ToUpper(efi.ContentHash) == strings.ToUpper(sha1Str) { result.Succeed = true From f838ebfe24ca818e5971e1cfa94309a506644c93 Mon Sep 17 00:00:00 2001 From: sowevo Date: Fri, 26 Jan 2024 15:01:27 +0800 Subject: [PATCH 2/3] add skip args --- internal/functions/panupload/upload_task_unit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/functions/panupload/upload_task_unit.go b/internal/functions/panupload/upload_task_unit.go index 4a5ea30..e586cd2 100644 --- a/internal/functions/panupload/upload_task_unit.go +++ b/internal/functions/panupload/upload_task_unit.go @@ -457,7 +457,7 @@ StepUploadPrepareUpload: return } time.Sleep(time.Duration(500) * time.Millisecond) - fmt.Printf("[%s] %s 检测到同名文件,已移动到回收站: %s\n", utu.taskInfo.Id(), time.Now().Format("2006-01-02 15:04:06"), utu.SavePath) + fmt.Printf("[%s] %s 检测到同名文件,文件内容不一致,已将旧文件移动到回收站: %s\n", utu.taskInfo.Id(), time.Now().Format("2006-01-02 15:04:06"), utu.SavePath) } } From 1b78dff61e88a5a2ed0dd5f87ac900c526615326 Mon Sep 17 00:00:00 2001 From: sowevo Date: Fri, 26 Jan 2024 15:10:27 +0800 Subject: [PATCH 3/3] add skip args --- docs/manual.md | 1 + internal/command/upload.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/docs/manual.md b/docs/manual.md index 60521bc..ce51861 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -239,6 +239,7 @@ aliyunpan d <网盘文件或目录的路径1> <文件或目录2> <文件或目 ### 可选参数 ``` --ow overwrite, 覆盖已存在的文件 + --skip skip same name, 跳过已存在的同名文件,即使文件内容不一致(不检查SHA1) --status 输出所有线程的工作状态 --save 将下载的文件直接保存到当前工作目录 --saveto value 将下载的文件直接保存到指定的目录 diff --git a/internal/command/upload.go b/internal/command/upload.go index 31fbc13..449a6f8 100644 --- a/internal/command/upload.go +++ b/internal/command/upload.go @@ -156,6 +156,9 @@ func CmdUpload() cli.Command { 9. 将本地的 C:\Users\Administrator\Video 整个目录上传到网盘 /视频 目录,但是排除所有的 @eadir 文件夹 aliyunpan upload -exn "^@eadir$" C:/Users/Administrator/Video /视频 + 10. 跳过已存在的同名文件,即使文件内容不一致(不检查SHA1) + aliyunpan upload -skip 1.mp4 /视频 + 参考: 以下是典型的排除特定文件或者文件夹的例子,注意:参数值必须是正则表达式。在正则表达式中,^表示匹配开头,$表示匹配结尾。 1)排除@eadir文件或者文件夹:-exn "^@eadir$"