From 81fe64eb2891e7ab55564e2428f64f1129b468e8 Mon Sep 17 00:00:00 2001 From: Artur Jankowski Date: Thu, 6 Oct 2022 15:25:03 +0200 Subject: [PATCH] feat: Add option to change name of downloaded file in `save-as`parameter (#415) --- src/commands/files/download.js | 12 +- src/commands/files/versions/download.js | 4 +- src/commands/files/zip.js | 7 +- test/commands/files.test.js | 205 ++++++++++++++++++++++-- 4 files changed, 213 insertions(+), 15 deletions(-) diff --git a/src/commands/files/download.js b/src/commands/files/download.js index 0fdcf1e8..43da2e9c 100644 --- a/src/commands/files/download.js +++ b/src/commands/files/download.js @@ -13,7 +13,7 @@ class FilesDownloadCommand extends BoxCommand { const { flags, args } = this.parse(FilesDownloadCommand); let file = await this.client.files.get(args.id); - let fileName = file.name; + let fileName = flags['save-as'] ? flags['save-as'] : file.name; let filePath; @@ -25,7 +25,7 @@ class FilesDownloadCommand extends BoxCommand { } /* eslint-disable no-sync */ - if (fs.existsSync(filePath)) { + if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); @@ -96,6 +96,14 @@ FilesDownloadCommand.flags = { allowNo: true, default: true }), + overwrite: flags.boolean({ + description: 'Overwrite a file if it already exists', + allowNo: true, + default: false + }), + 'save-as': flags.string({ + description: 'The filename used when saving the file' + }) }; FilesDownloadCommand.args = [ diff --git a/src/commands/files/versions/download.js b/src/commands/files/versions/download.js index 5e41d431..ea894e3b 100644 --- a/src/commands/files/versions/download.js +++ b/src/commands/files/versions/download.js @@ -14,7 +14,7 @@ class FilesVersionsDownloadCommand extends BoxCommand { const { flags, args } = this.parse(FilesVersionsDownloadCommand); let file = await this.client.files.get(args.fileID); - let fileName = file.name; + let fileName = flags['save-as'] ? flags['save-as'] : file.name; let filePath; @@ -26,7 +26,7 @@ class FilesVersionsDownloadCommand extends BoxCommand { } /* eslint-disable no-sync */ - if (fs.existsSync(filePath)) { + if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); diff --git a/src/commands/files/zip.js b/src/commands/files/zip.js index da6439ef..8cb1266a 100644 --- a/src/commands/files/zip.js +++ b/src/commands/files/zip.js @@ -25,7 +25,7 @@ class FilesZipCommand extends BoxCommand { } /* eslint-disable no-sync */ - if (fs.existsSync(filePath)) { + if (!flags.overwrite && fs.existsSync(filePath)) { /* eslint-enable no-sync */ let shouldOverwrite = await this.confirm(`File ${filePath} already exists — overwrite?`); @@ -66,6 +66,11 @@ FilesZipCommand.flags = { allowNo: true, default: true, }), + overwrite: flags.boolean({ + description: 'Overwrite a zip file if it already exists', + allowNo: true, + default: false + }), }; FilesZipCommand.args = [ diff --git a/test/commands/files.test.js b/test/commands/files.test.js index a4c283c2..0162a784 100644 --- a/test/commands/files.test.js +++ b/test/commands/files.test.js @@ -1829,9 +1829,11 @@ describe('Files', () => { describe('files:download', () => { let fileId = '12345', fileName = 'test_file_download.txt', + saveAsFileName = 'new_file_name.txt', fileVersionID = '8764569', testFilePath = path.join(__dirname, '..', 'fixtures/files/epic-poem.txt'), fileDownloadPath = path.join(__dirname, '..', 'fixtures/files'), + destinationPath = `${fileDownloadPath}/temp`, getFileFixture = getFixture('files/get_files_id'); test @@ -1869,8 +1871,6 @@ describe('Files', () => { assert.equal(ctx.stderr, expectedMessage); }); - const destination = `${fileDownloadPath}/temp`; - test .nock(TEST_API_ROOT, api => api .get(`/2.0/files/${fileId}`) @@ -1889,17 +1889,17 @@ describe('Files', () => { .command([ 'files:download', fileId, - `--destination=${destination}`, + `--destination=${destinationPath}`, '-y', '--token=test' ]) .it('should download a file to a non-existing destination', () => { /* eslint-disable no-sync */ - let downloadedFilePath = path.join(destination, fileName); + let downloadedFilePath = path.join(destinationPath, fileName); let downloadContent = fs.readFileSync(downloadedFilePath); let expectedContent = fs.readFileSync(testFilePath); fs.unlinkSync(downloadedFilePath); - fs.rmdirSync(destination); + fs.rmdirSync(destinationPath); /* eslint-enable no-sync */ assert.ok(downloadContent.equals(expectedContent)); }); @@ -1935,6 +1935,77 @@ describe('Files', () => { assert.ok(downloadContent.equals(expectedContent)); }); + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + .get(`/2.0/files/${fileId}/content`) + .reply(302, '', { + Location: TEST_DOWNLOAD_ROOT + fileDownloadPath + }) + ) + .nock(TEST_DOWNLOAD_ROOT, api => api + .get(fileDownloadPath) + .reply(200, function() { return fs.createReadStream(testFilePath, 'utf8'); }) + ) + .stdout() + .stderr() + .command([ + 'files:download', + fileId, + `--save-as=${saveAsFileName}`, + `--destination=${destinationPath}`, + '-y', + '--token=test' + ]) + .it('should save downloaded file using provided filename in save-as parameter', () => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(destinationPath, saveAsFileName); + let downloadContent = fs.readFileSync(downloadedFilePath); + let expectedContent = fs.readFileSync(testFilePath); + fs.unlinkSync(downloadedFilePath); + fs.rmdirSync(destinationPath); + /* eslint-enable no-sync */ + assert.ok(downloadContent.equals(expectedContent)); + }); + + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + .get(`/2.0/files/${fileId}/content`) + .reply(302, '', { + Location: TEST_DOWNLOAD_ROOT + fileDownloadPath + }) + ) + .nock(TEST_DOWNLOAD_ROOT, api => api + .get(fileDownloadPath) + .reply(200, function() { return fs.createReadStream(testFilePath, 'utf8'); }) + ) + .do(() => { + /* eslint-disable no-sync */ + fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName), 'foo', 'utf8'); + /* eslint-enable no-sync */ + }) + .stdout() + .stderr() + .command([ + 'files:download', + fileId, + `--save-as=${saveAsFileName}`, + '--overwrite', + '--token=test' + ]) + .it('should overwrite downloaded file when --overwrite flag is used', () => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName); + let downloadContent = fs.readFileSync(downloadedFilePath); + let expectedContent = fs.readFileSync(testFilePath); + fs.unlinkSync(downloadedFilePath); + /* eslint-enable no-sync */ + assert.ok(downloadContent.equals(expectedContent)); + }); + test .nock(TEST_API_ROOT, api => api .get(`/2.0/files/${fileId}`) @@ -1977,9 +2048,11 @@ describe('Files', () => { let fileId = '12345', fileName = 'test_file_download.txt', + saveAsFileName = 'new_file_name.txt', fileVersionID = '8764569', testFilePath = path.join(__dirname, '..', 'fixtures/files/epic-poem.txt'), fileDownloadPath = path.join(__dirname, '..', 'fixtures/files'), + destinationPath = `${fileDownloadPath}/temp`, getFileFixture = getFixture('files/get_files_id'); test @@ -2052,8 +2125,6 @@ describe('Files', () => { assert.equal(ctx.stderr, 'Downloaded file test_file_download.txt\n'); }); - const destination = `${fileDownloadPath}/temp`; - test .nock(TEST_API_ROOT, api => api .get(`/2.0/files/${fileId}`) @@ -2106,18 +2177,93 @@ describe('Files', () => { .command([ 'files:versions:download', fileId, - `--destination=${destination}`, + `--destination=${destinationPath}`, '-y', fileVersionID, '--token=test' ]) .it('should download a file version to a non-existing destination', () => { /* eslint-disable no-sync */ - let downloadedFilePath = path.join(destination, fileName); + let downloadedFilePath = path.join(destinationPath, fileName); + let downloadContent = fs.readFileSync(downloadedFilePath); + let expectedContent = fs.readFileSync(testFilePath); + fs.unlinkSync(downloadedFilePath); + fs.rmdirSync(destinationPath); + /* eslint-enable no-sync */ + assert.ok(downloadContent.equals(expectedContent)); + }); + + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + .get(`/2.0/files/${fileId}/content`) + .query({ version: fileVersionID }) + .reply(302, '', { + Location: TEST_DOWNLOAD_ROOT + fileDownloadPath + }) + ) + .nock(TEST_DOWNLOAD_ROOT, api => api + .get(fileDownloadPath) + .reply(200, function() { return fs.createReadStream(testFilePath, 'utf8'); }) + ) + .stdout() + .stderr() + .command([ + 'files:versions:download', + fileId, + `--save-as=${saveAsFileName}`, + `--destination=${destinationPath}`, + '-y', + fileVersionID, + '--token=test' + ]) + .it('should save downloaded file version using provided filename in save-as parameter', () => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(destinationPath, saveAsFileName); + let downloadContent = fs.readFileSync(downloadedFilePath); + let expectedContent = fs.readFileSync(testFilePath); + fs.unlinkSync(downloadedFilePath); + fs.rmdirSync(destinationPath); + /* eslint-enable no-sync */ + assert.ok(downloadContent.equals(expectedContent)); + }); + + test + .nock(TEST_API_ROOT, api => api + .get(`/2.0/files/${fileId}`) + .reply(200, getFileFixture) + .get(`/2.0/files/${fileId}/content`) + .query({ version: fileVersionID }) + .reply(302, '', { + Location: TEST_DOWNLOAD_ROOT + fileDownloadPath + }) + ) + .nock(TEST_DOWNLOAD_ROOT, api => api + .get(fileDownloadPath) + .reply(200, function() { return fs.createReadStream(testFilePath, 'utf8'); }) + ) + .do(() => { + /* eslint-disable no-sync */ + fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName), 'foo', 'utf8'); + /* eslint-enable no-sync */ + }) + .stdout() + .stderr() + .command([ + 'files:versions:download', + fileId, + `--save-as=${saveAsFileName}`, + '--overwrite', + fileVersionID, + '--token=test' + ]) + .it('should overwrite downloaded file version when --overwrite flag is used', () => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, saveAsFileName); let downloadContent = fs.readFileSync(downloadedFilePath); let expectedContent = fs.readFileSync(testFilePath); fs.unlinkSync(downloadedFilePath); - fs.rmdirSync(destination); /* eslint-enable no-sync */ assert.ok(downloadContent.equals(expectedContent)); }); @@ -2250,5 +2396,44 @@ describe('Files', () => { assert.ok(downloadContent.equals(expectedContent)); assert.equal(ctx.stdout, downloadStatusFixture); }); + + test + .nock(TEST_API_ROOT, api => api + .post('/2.0/zip_downloads', expectedBody) + .reply(202, createFileFixture) + ) + .nock(TEST_DOWNLOAD_ROOT, api => api + .get(downloadUrl) + .reply(200, function() { return fs.createReadStream(testFilePath, 'utf8'); }) + ) + .nock(TEST_API_ROOT, api => api + .get(statusUrl) + .reply(200, downloadStatusFixture) + ) + .do(() => { + /* eslint-disable no-sync */ + fs.writeFileSync(path.join(DEFAULT_DOWNLOAD_PATH, fileName), 'foo', 'utf8'); + /* eslint-enable no-sync */ + }) + .stdout() + .command([ + 'files:zip', + fileName, + `--item=${items[0].type}:${items[0].id}`, + `--item=${items[1].type}:${items[1].id}`, + '--overwrite', + '--json', + '--token=test' + ]) + .it('should overwrite downloaded zip file when --overwrite flag is used', ctx => { + /* eslint-disable no-sync */ + let downloadedFilePath = path.join(DEFAULT_DOWNLOAD_PATH, fileName); + let downloadContent = fs.readFileSync(downloadedFilePath); + let expectedContent = fs.readFileSync(testFilePath); + fs.unlinkSync(downloadedFilePath); + /* eslint-enable no-sync */ + assert.ok(downloadContent.equals(expectedContent)); + assert.equal(ctx.stdout, downloadStatusFixture); + }); }); });