Skip to content

Commit

Permalink
feat(download): create directories for saveAs (#3249)
Browse files Browse the repository at this point in the history
This is to match the behavior for screenshots path added in #3247.
  • Loading branch information
rwoll authored Jul 31, 2020
1 parent 93056ed commit ce0ddd2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,7 @@ Returns download error if any.
Returns path to the downloaded file in case of successful download.

#### download.saveAs(path)
- `path` <[string]> Path where the download should be saved. The directory structure MUST exist as `saveAs` will not create it.
- `path` <[string]> Path where the download should be saved.
- returns: <[Promise]>

Saves the download to a user-specified path.
Expand Down
2 changes: 2 additions & 0 deletions src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export class Download {

async _saveAs(downloadPath: string) {
const fileName = path.join(this._downloadsPath, this._uuid);
// This will harmlessly throw on windows if the dirname is the root directory.
await util.promisify(fs.mkdir)(path.dirname(downloadPath), {recursive: true}).catch(() => {});
await util.promisify(fs.copyFile)(fileName, downloadPath);
}

Expand Down
12 changes: 5 additions & 7 deletions test/download.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,17 @@ describe('Download', function() {
expect(fs.readFileSync(userPath).toString()).toBe('Hello world');
await page.close();
});
it('should error when saving to non-existent user-specified path', async({persistentDirectory, browser, server}) => {
it('should create subdirectories when saving to non-existent user-specified path', async({persistentDirectory, browser, server}) => {
const page = await browser.newPage({ acceptDownloads: true });
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
const [ download ] = await Promise.all([
page.waitForEvent('download'),
page.click('a')
]);
const nonExistentUserPath = path.join(persistentDirectory, "does-not-exist","download.txt");
const { message } = await download.saveAs(nonExistentUserPath).catch(e => e);
expect(message).toContain('ENOENT');
expect(message).toContain('copyfile');
expect(message).toContain('no such file or directory');
expect(message).toContain('does-not-exist');
const nestedPath = path.join(persistentDirectory, "these", "are", "directories", "download.txt");
await download.saveAs(nestedPath)
expect(fs.existsSync(nestedPath)).toBeTruthy();
expect(fs.readFileSync(nestedPath).toString()).toBe('Hello world');
await page.close();
});
it('should error when saving with downloads disabled', async({persistentDirectory, browser, server}) => {
Expand Down

0 comments on commit ce0ddd2

Please sign in to comment.