fix forbidden file download issue #35

This commit is contained in:
tickstep 2022-01-16 16:00:01 +08:00
parent d4873b94f7
commit 69189a8305
3 changed files with 60 additions and 18 deletions

View File

@ -20,12 +20,12 @@ import (
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
"github.com/tickstep/aliyunpan/cmder/cmdutil"
"github.com/tickstep/aliyunpan/internal/waitgroup"
"github.com/tickstep/aliyunpan/library/requester/transfer"
"github.com/tickstep/library-go/cachepool"
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/prealloc"
"github.com/tickstep/library-go/requester"
"github.com/tickstep/library-go/requester/rio/speeds"
"github.com/tickstep/aliyunpan/library/requester/transfer"
"io"
"net/http"
"sync"
@ -42,6 +42,7 @@ type (
Downloader struct {
onExecuteEvent requester.Event //开始下载事件
onSuccessEvent requester.Event //成功下载事件
onFailedEvent requester.Event //成功下载事件
onFinishEvent requester.Event //结束下载事件
onPauseEvent requester.Event //暂停下载事件
onResumeEvent requester.Event //恢复下载事件
@ -80,7 +81,6 @@ func NewDownloader(writer io.WriterAt, config *Config, p *aliyunpan.PanClient, g
panClient: p,
globalSpeedsStat: globalSpeedsStat,
}
return
}
@ -369,23 +369,34 @@ func (der *Downloader) Execute() error {
var (
writeMu = &sync.Mutex{}
)
// 获取下载链接
var apierr *apierror.ApiError
durl, apierr := der.panClient.GetFileDownloadUrl(&aliyunpan.GetFileDownloadUrlParam{
DriveId: der.driveId,
FileId: der.fileInfo.FileId,
})
time.Sleep(time.Duration(200) * time.Millisecond)
if apierr != nil {
logger.Verbosef("ERROR: get download url error: %s\n", der.fileInfo.FileId)
cmdutil.Trigger(der.onCancelEvent)
return apierr
}
if durl == nil || durl.Url == "" {
logger.Verbosef("无法获取有效的下载链接: %+v\n", durl)
cmdutil.Trigger(der.onCancelEvent)
der.removeInstanceState() // 移除断点续传文件
cmdutil.Trigger(der.onFailedEvent)
return ErrFileDownloadForbidden
}
// 初始化下载worker
for k, r := range bii.Ranges {
loadBalancer := loadBalancerResponseList.SequentialGet()
if loadBalancer == nil {
continue
}
// 获取下载链接
var apierr *apierror.ApiError
durl, apierr := der.panClient.GetFileDownloadUrl(&aliyunpan.GetFileDownloadUrlParam{
DriveId: der.driveId,
FileId: der.fileInfo.FileId,
})
time.Sleep(time.Duration(200) * time.Millisecond)
if apierr != nil {
logger.Verbosef("ERROR: get download url error: %s\n", der.fileInfo.FileId)
continue
}
logger.Verbosef("work id: %d, download url: %s\n", k, durl)
client := requester.NewHTTPClient()
client.SetKeepAlive(true)
@ -488,6 +499,15 @@ func (der *Downloader) Cancel() {
cmdutil.Trigger(der.monitorCancelFunc)
}
//Failed 失败
func (der *Downloader) Failed() {
if der.monitor == nil {
return
}
cmdutil.Trigger(der.onFailedEvent)
cmdutil.Trigger(der.monitorCancelFunc)
}
//OnExecute 设置开始下载事件
func (der *Downloader) OnExecute(onExecuteEvent requester.Event) {
der.onExecuteEvent = onExecuteEvent
@ -498,6 +518,11 @@ func (der *Downloader) OnSuccess(onSuccessEvent requester.Event) {
der.onSuccessEvent = onSuccessEvent
}
//OnFailed 设置失败事件
func (der *Downloader) OnFailed(onFailedEvent requester.Event) {
der.onFailedEvent = onFailedEvent
}
//OnFinish 设置结束下载事件
func (der *Downloader) OnFinish(onFinishEvent requester.Event) {
der.onFinishEvent = onFinishEvent

View File

@ -14,6 +14,7 @@
package downloader
import (
"errors"
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/requester"
mathrand "math/rand"
@ -34,6 +35,9 @@ var (
// ran 一个随机数实例
ran = mathrand.New(ranSource)
// 文件被禁止下载
ErrFileDownloadForbidden = errors.New("文件被禁止下载")
)
// RandomNumber 生成指定区间随机数

View File

@ -188,14 +188,21 @@ func (dtu *DownloadTaskUnit) download() (err error) {
})
err = der.Execute()
isComplete = true
fmt.Print("\n")
if err != nil {
// check zero size file
if err == downloader.ErrNoWokers && dtu.fileInfo.FileSize == 0 {
// success for 0 size file
dtu.verboseInfof("download success for zero size file")
} else if err == downloader.ErrFileDownloadForbidden {
// 文件被禁止下载
isComplete = false
// 删除本地文件
removeErr := os.Remove(dtu.SavePath)
if removeErr != nil {
dtu.verboseInfof("[%s] remove file error: %s\n", dtu.taskInfo.Id(), removeErr)
}
fmt.Printf("[%s] 下载失败,文件不合法或者被禁止下载: %s\n", dtu.taskInfo.Id(), dtu.SavePath)
return err
} else {
// 下载发生错误
// 下载失败, 删去空文件
@ -211,6 +218,8 @@ func (dtu *DownloadTaskUnit) download() (err error) {
}
return err
}
} else {
isComplete = true
}
// 下载成功
@ -253,8 +262,12 @@ func (dtu *DownloadTaskUnit) handleError(result *taskframework.TaskUnitRunResult
// 系统级别的错误, 可能是权限问题
result.NeedRetry = false
default:
// 其他错误, 需要重试
result.NeedRetry = true
if result.Err == downloader.ErrFileDownloadForbidden {
result.NeedRetry = false
} else {
// 其他错误, 尝试重试
result.NeedRetry = true
}
}
time.Sleep(1*time.Second)
}