Skip to content

Commit

Permalink
feat: Add option to change name of downloaded file in save-asparame…
Browse files Browse the repository at this point in the history
…ter (#415)
  • Loading branch information
arjankowski authored Oct 6, 2022
1 parent 2953216 commit 81fe64e
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/commands/files/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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?`);
Expand Down Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions src/commands/files/versions/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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?`);
Expand Down
7 changes: 6 additions & 1 deletion src/commands/files/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?`);
Expand Down Expand Up @@ -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 = [
Expand Down
205 changes: 195 additions & 10 deletions test/commands/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
Expand All @@ -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));
});
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 81fe64e

Please sign in to comment.