Skip to content

Commit 729efb2

Browse files
thiyaguk09gcf-owl-bot[bot]harsha-accenture
authored
feat(storage): add support for 'skipIfExists' option for downloadMany (#2526)
* feature added * fix * Added system test and sample test cases * copyright fix * build: fix path-to-regexp to older version due to node 14 requirement * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Remove unnecessary sample code * Manually modify README regards remove unnecessary sample code * added 'skipIfExists' option for downloadMany Node Transfer Manager: add support for 'skipIfExists' option for downloadMany --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: harsha-accenture <[email protected]>
1 parent c75513a commit 729efb2

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/transfer-manager.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
} from './file.js';
2525
import pLimit from 'p-limit';
2626
import * as path from 'path';
27-
import {createReadStream, promises as fsp} from 'fs';
27+
import {createReadStream, existsSync, promises as fsp} from 'fs';
2828
import {CRC32C} from './crc32c.js';
2929
import {GoogleAuth} from 'google-auth-library';
3030
import {XMLParser, XMLBuilder} from 'fast-xml-parser';
@@ -108,6 +108,7 @@ export interface DownloadManyFilesOptions {
108108
prefix?: string;
109109
stripPrefix?: string;
110110
passthroughOptions?: DownloadOptions;
111+
skipIfExists?: boolean;
111112
}
112113

113114
export interface DownloadFileInChunksOptions {
@@ -524,6 +525,8 @@ export class TransferManager {
524525
* @property {string} [stripPrefix] A prefix to remove from all of the downloaded files.
525526
* @property {object} [passthroughOptions] {@link DownloadOptions} Options to be passed through
526527
* to each individual download operation.
528+
* @property {boolean} [skipIfExists] Do not download the file if it already exists in
529+
* the destination.
527530
*
528531
*/
529532
/**
@@ -605,6 +608,12 @@ export class TransferManager {
605608
if (options.stripPrefix) {
606609
passThroughOptionsCopy.destination = file.name.replace(regex, '');
607610
}
611+
if (
612+
options.skipIfExists &&
613+
existsSync(passThroughOptionsCopy.destination || '')
614+
) {
615+
continue;
616+
}
608617

609618
promises.push(
610619
limit(async () => {

test/transfer-manager.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
TransferManager,
3131
Storage,
3232
DownloadResponse,
33+
DownloadManyFilesOptions,
3334
} from '../src/index.js';
3435
import assert from 'assert';
3536
import * as path from 'path';
@@ -195,6 +196,10 @@ describe('Transfer Manager', () => {
195196
});
196197

197198
describe('downloadManyFiles', () => {
199+
beforeEach(() => {
200+
sandbox.stub(fs, 'existsSync').returns(true);
201+
});
202+
198203
it('calls download for each provided file', async () => {
199204
let count = 0;
200205
const firstFile = new File(bucket, 'first.txt');
@@ -276,6 +281,27 @@ describe('Transfer Manager', () => {
276281
await transferManager.downloadManyFiles([file], {passthroughOptions});
277282
});
278283

284+
it('does not download files that already exist locally when skipIfExists is true', async () => {
285+
const firstFile = new File(bucket, 'first.txt');
286+
sandbox.stub(firstFile, 'download').callsFake(options => {
287+
assert.strictEqual(
288+
(options as DownloadManyFilesOptions).skipIfExists,
289+
0
290+
);
291+
});
292+
const secondFile = new File(bucket, 'second.txt');
293+
sandbox.stub(secondFile, 'download').callsFake(options => {
294+
assert.strictEqual(
295+
(options as DownloadManyFilesOptions).skipIfExists,
296+
0
297+
);
298+
});
299+
300+
const files = [firstFile, secondFile];
301+
const options = {skipIfExists: true};
302+
await transferManager.downloadManyFiles(files, options);
303+
});
304+
279305
it('does not set the destination when prefix, strip prefix and passthroughOptions.destination are not provided', async () => {
280306
const options = {};
281307
const filename = 'first.txt';

0 commit comments

Comments
 (0)