Skip to content

Commit

Permalink
fix: 修复 downloadFile 取消任务不能取消指定任务 (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
livehigh authored Apr 26, 2024
1 parent 5f8158e commit 18977cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-nodejs-sdk-v5",
"version": "2.13.4",
"version": "2.13.5",
"description": "cos nodejs sdk v5",
"main": "index.js",
"types": "index.d.ts",
Expand Down
13 changes: 8 additions & 5 deletions sdk/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ function downloadFile(params, callback) {
});

// 计算合适的分片大小
ep.on('calc_suitable_chunk_size', function (SourceHeaders) {
ep.on('calc_suitable_chunk_size', function () {
// 控制分片大小
var SIZE = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024 * 2, 1024 * 4, 1024 * 5];
var AutoChunkSize = 1024 * 1024;
Expand Down Expand Up @@ -1535,7 +1535,7 @@ function downloadFile(params, callback) {
});

// 准备要下载的空文件
ep.on('prepare_file', function (SourceHeaders) {
ep.on('prepare_file', function () {
fs.writeFile(FilePath, '', (err) => {
if (err) {
ep.emit('error', err.code === 'EISDIR' ? { code: 'exist_same_dir', message: FilePath } : err);
Expand All @@ -1547,7 +1547,7 @@ function downloadFile(params, callback) {

// 计算合适的分片大小
var result;
ep.on('start_download_chunks', function (SourceHeaders) {
ep.on('start_download_chunks', function () {
onProgress({ loaded: 0, total: FileSize }, true);
var maxPartNumber = PartList.length;
Async.eachLimit(
Expand Down Expand Up @@ -1649,8 +1649,11 @@ function downloadFile(params, callback) {
});

// 监听 取消任务
var killTask = function () {
aborted = true;
var killTask = function (info) {
var killingTaskId = info.TaskId || '';
if (killingTaskId === TaskId) {
aborted = true;
}
};
TaskId && self.on('inner-kill-task', killTask);

Expand Down
36 changes: 36 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6079,6 +6079,42 @@ group('downloadFile', function () {
}
);
});
test('downloadFile() 下载文件时取消下载', function (done, assert) {
var Key = '10mb.zip';
var fileSize = 1024 * 1024 * 10;
var filePath = createFileSync(path.resolve(__dirname, Key), fileSize);
cos.sliceUploadFile(
{
Bucket: config.Bucket,
Region: config.Region,
Key: Key,
FilePath: filePath,
},
function (err, data) {
if (err) {
done();
} else {
cos.downloadFile({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: Key,
FilePath: './' + Key, // 本地保存路径
ChunkSize: 1024 * 1024 * 8, // 分块大小
ParallelLimit: 5, // 分块并发数
RetryTimes: 3, // 分块失败重试次数
TaskId: 'downloadFile-123', // 可以自己生成TaskId,用于取消下载
onProgress: function (progressData) {
if (progressData.percent >= 0.1) {
cos.emit('inner-kill-task', {TaskId: 'downloadFile-123'});
assert.ok(err);
done();
}
},
});
}
}
);
});
});

// group('数据万象', function () {
Expand Down

0 comments on commit 18977cf

Please sign in to comment.