From ae81b599263116b237dc6b5cc0e2ec2ab600bf35 Mon Sep 17 00:00:00 2001 From: tickstep Date: Sat, 16 Mar 2024 17:02:33 +0800 Subject: [PATCH] fix download url expired issue for sync --- internal/file/downloader/worker.go | 5 +++++ internal/syncdrive/file_action_task.go | 28 +++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/file/downloader/worker.go b/internal/file/downloader/worker.go index b9bb361..9012e9f 100644 --- a/internal/file/downloader/worker.go +++ b/internal/file/downloader/worker.go @@ -146,6 +146,11 @@ func (wer *Worker) SetDownloadStatus(downloadStatus *transfer.DownloadStatus) { wer.downloadStatus = downloadStatus } +// SetUrl 更新新的下载路径 +func (wer *Worker) SetUrl(newUrl string) { + wer.url = newUrl +} + // GetStatus 返回下载状态 func (wer *Worker) GetStatus() WorkerStatuser { // 空接口与空指针不等价 diff --git a/internal/syncdrive/file_action_task.go b/internal/syncdrive/file_action_task.go index 8c018ce..aa5a328 100644 --- a/internal/syncdrive/file_action_task.go +++ b/internal/syncdrive/file_action_task.go @@ -383,8 +383,34 @@ func (f *FileActionTask) downloadFile(ctx context.Context) error { // 存储状态 f.syncFileDb.Update(f.syncItem) + } else if worker.GetStatus().StatusCode() == downloader.StatusCodeDownloadUrlExpired { + logger.Verboseln("download url expired: ", f.syncItem.PanFile.Path) + // 下载链接过期,获取新的链接 + newUrl, apierr1 := f.panClient.OpenapiPanClient().GetFileDownloadUrl(&aliyunpan.GetFileDownloadUrlParam{ + DriveId: f.syncItem.PanFile.DriveId, + FileId: f.syncItem.PanFile.FileId, + }) + time.Sleep(time.Duration(3) * time.Second) + if apierr1 != nil { + if apierr1.Code == apierror.ApiCodeFileNotFoundCode || apierr1.Code == apierror.ApiCodeForbiddenFileInTheRecycleBin { + f.syncItem.Status = SyncFileStatusNotExisted + f.syncItem.StatusUpdateTime = utils.NowTimeStr() + f.syncFileDb.Update(f.syncItem) + return fmt.Errorf("文件不存在") + } + logger.Verbosef("ERROR: get download url error: %s, %s\n", f.syncItem.PanFile.Path, apierr.Error()) + return apierr + } + if newUrl == nil || newUrl.Url == "" { + logger.Verbosef("无法获取有效的下载链接: %+v\n", durl) + f.syncItem.Status = SyncFileStatusFailed + f.syncItem.StatusUpdateTime = utils.NowTimeStr() + f.syncFileDb.Update(f.syncItem) + return fmt.Errorf("无法获取有效的下载链接") + } + logger.Verboseln("query new download url: ", newUrl.Url) + worker.SetUrl(newUrl.Url) } - // TODO: 下载链接过期处理 } } }