Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add support for 'skipIfExists' option for downloadMany #2526

Merged
merged 17 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/transfer-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from './file.js';
import pLimit from 'p-limit';
import * as path from 'path';
import {createReadStream, promises as fsp} from 'fs';
import {createReadStream, existsSync, promises as fsp} from 'fs';
import {CRC32C} from './crc32c.js';
import {GoogleAuth} from 'google-auth-library';
import {XMLParser, XMLBuilder} from 'fast-xml-parser';
Expand Down Expand Up @@ -108,6 +108,7 @@ export interface DownloadManyFilesOptions {
prefix?: string;
stripPrefix?: string;
passthroughOptions?: DownloadOptions;
skipIfExists?: boolean;
}

export interface DownloadFileInChunksOptions {
Expand Down Expand Up @@ -524,6 +525,8 @@ export class TransferManager {
* @property {string} [stripPrefix] A prefix to remove from all of the downloaded files.
* @property {object} [passthroughOptions] {@link DownloadOptions} Options to be passed through
* to each individual download operation.
* @property {boolean} [skipIfExists] Do not download the file if it already exists in
* the destination.
*
*/
/**
Expand Down Expand Up @@ -605,6 +608,12 @@ export class TransferManager {
if (options.stripPrefix) {
passThroughOptionsCopy.destination = file.name.replace(regex, '');
}
if (
options.skipIfExists &&
existsSync(passThroughOptionsCopy.destination || '')
) {
continue;
}

promises.push(
limit(async () => {
Expand Down
26 changes: 26 additions & 0 deletions test/transfer-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
MultiPartUploadError,
MultiPartUploadHelper,
UploadOptions,
UploadManyFilesOptions,

Check warning on line 29 in test/transfer-manager.ts

View workflow job for this annotation

GitHub Actions / lint

'UploadManyFilesOptions' is defined but never used
TransferManager,
Storage,
DownloadResponse,
DownloadManyFilesOptions,
} from '../src/index.js';
import assert from 'assert';
import * as path from 'path';
Expand Down Expand Up @@ -195,6 +196,10 @@
});

describe('downloadManyFiles', () => {
beforeEach(() => {
sandbox.stub(fs, 'existsSync').returns(true);
});

it('calls download for each provided file', async () => {
let count = 0;
const firstFile = new File(bucket, 'first.txt');
Expand Down Expand Up @@ -276,6 +281,27 @@
await transferManager.downloadManyFiles([file], {passthroughOptions});
});

it('does not download files that already exist locally when skipIfExists is true', async () => {
const firstFile = new File(bucket, 'first.txt');
sandbox.stub(firstFile, 'download').callsFake(options => {
assert.strictEqual(
(options as DownloadManyFilesOptions).skipIfExists,
0
);
});
const secondFile = new File(bucket, 'second.txt');
sandbox.stub(secondFile, 'download').callsFake(options => {
assert.strictEqual(
(options as DownloadManyFilesOptions).skipIfExists,
0
);
});

const files = [firstFile, secondFile];
const options = {skipIfExists: true};
await transferManager.downloadManyFiles(files, options);
});

it('does not set the destination when prefix, strip prefix and passthroughOptions.destination are not provided', async () => {
const options = {};
const filename = 'first.txt';
Expand Down
Loading