2020-04-02 17:56:14 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 06:36:15 -08:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
2020-04-02 17:56:14 -07:00
|
|
|
import * as util from 'util';
|
|
|
|
import { Page } from './page';
|
2020-08-26 12:46:30 -07:00
|
|
|
import { assert } from '../utils/utils';
|
|
|
|
|
|
|
|
type SaveCallback = (localPath: string, error?: string) => Promise<void>;
|
2020-04-02 17:56:14 -07:00
|
|
|
|
2021-02-09 14:44:48 -08:00
|
|
|
export class Download {
|
2020-04-02 17:56:14 -07:00
|
|
|
private _downloadsPath: string;
|
|
|
|
private _uuid: string;
|
|
|
|
private _finishedCallback: () => void;
|
|
|
|
private _finishedPromise: Promise<void>;
|
2020-08-26 12:46:30 -07:00
|
|
|
private _saveCallbacks: SaveCallback[] = [];
|
2020-07-22 15:59:11 -07:00
|
|
|
private _finished: boolean = false;
|
2020-04-02 17:56:14 -07:00
|
|
|
private _page: Page;
|
|
|
|
private _acceptDownloads: boolean;
|
|
|
|
private _failure: string | null = null;
|
|
|
|
private _deleted = false;
|
|
|
|
private _url: string;
|
2020-05-12 19:23:08 -07:00
|
|
|
private _suggestedFilename: string | undefined;
|
2020-04-02 17:56:14 -07:00
|
|
|
|
2020-05-12 19:23:08 -07:00
|
|
|
constructor(page: Page, downloadsPath: string, uuid: string, url: string, suggestedFilename?: string) {
|
2020-04-02 17:56:14 -07:00
|
|
|
this._page = page;
|
|
|
|
this._downloadsPath = downloadsPath;
|
|
|
|
this._uuid = uuid;
|
|
|
|
this._url = url;
|
2020-05-12 19:23:08 -07:00
|
|
|
this._suggestedFilename = suggestedFilename;
|
2020-04-02 17:56:14 -07:00
|
|
|
this._finishedCallback = () => {};
|
|
|
|
this._finishedPromise = new Promise(f => this._finishedCallback = f);
|
|
|
|
page._browserContext._downloads.add(this);
|
|
|
|
this._acceptDownloads = !!this._page._browserContext._options.acceptDownloads;
|
2020-05-12 19:23:08 -07:00
|
|
|
if (suggestedFilename !== undefined)
|
2020-08-21 16:26:33 -07:00
|
|
|
this._page.emit(Page.Events.Download, this);
|
2020-05-12 19:23:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
_filenameSuggested(suggestedFilename: string) {
|
|
|
|
assert(this._suggestedFilename === undefined);
|
|
|
|
this._suggestedFilename = suggestedFilename;
|
2020-08-21 16:26:33 -07:00
|
|
|
this._page.emit(Page.Events.Download, this);
|
2020-04-02 17:56:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:23:08 -07:00
|
|
|
suggestedFilename(): string {
|
|
|
|
return this._suggestedFilename!;
|
|
|
|
}
|
|
|
|
|
2020-08-26 12:46:30 -07:00
|
|
|
async localPath(): Promise<string | null> {
|
2020-04-02 17:56:14 -07:00
|
|
|
if (!this._acceptDownloads)
|
|
|
|
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
|
|
|
|
const fileName = path.join(this._downloadsPath, this._uuid);
|
|
|
|
await this._finishedPromise;
|
|
|
|
if (this._failure)
|
|
|
|
return null;
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2020-08-26 12:46:30 -07:00
|
|
|
saveAs(saveCallback: SaveCallback) {
|
2020-07-22 15:59:11 -07:00
|
|
|
if (!this._acceptDownloads)
|
|
|
|
throw new Error('Pass { acceptDownloads: true } when you are creating your browser context.');
|
|
|
|
if (this._deleted)
|
|
|
|
throw new Error('Download already deleted. Save before deleting.');
|
|
|
|
if (this._failure)
|
|
|
|
throw new Error('Download not found on disk. Check download.failure() for details.');
|
|
|
|
|
|
|
|
if (this._finished) {
|
2020-08-26 12:46:30 -07:00
|
|
|
saveCallback(path.join(this._downloadsPath, this._uuid));
|
2020-07-22 14:55:27 -07:00
|
|
|
return;
|
|
|
|
}
|
2020-08-26 12:46:30 -07:00
|
|
|
this._saveCallbacks.push(saveCallback);
|
2020-07-22 14:55:27 -07:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:56:14 -07:00
|
|
|
async failure(): Promise<string | null> {
|
|
|
|
if (!this._acceptDownloads)
|
|
|
|
return 'Pass { acceptDownloads: true } when you are creating your browser context.';
|
|
|
|
await this._finishedPromise;
|
|
|
|
return this._failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(): Promise<void> {
|
|
|
|
if (!this._acceptDownloads)
|
|
|
|
return;
|
2020-08-26 12:46:30 -07:00
|
|
|
const fileName = await this.localPath();
|
2020-04-02 17:56:14 -07:00
|
|
|
if (this._deleted)
|
|
|
|
return;
|
|
|
|
this._deleted = true;
|
|
|
|
if (fileName)
|
|
|
|
await util.promisify(fs.unlink)(fileName).catch(e => {});
|
|
|
|
}
|
|
|
|
|
2020-07-22 14:55:27 -07:00
|
|
|
async _reportFinished(error?: string) {
|
2020-07-22 15:59:11 -07:00
|
|
|
this._finished = true;
|
|
|
|
this._failure = error || null;
|
|
|
|
|
2020-07-22 14:55:27 -07:00
|
|
|
if (error) {
|
2020-08-26 12:46:30 -07:00
|
|
|
for (const callback of this._saveCallbacks)
|
|
|
|
callback('', error);
|
2020-07-22 14:55:27 -07:00
|
|
|
} else {
|
2020-08-26 12:46:30 -07:00
|
|
|
const fullPath = path.join(this._downloadsPath, this._uuid);
|
|
|
|
for (const callback of this._saveCallbacks)
|
|
|
|
await callback(fullPath);
|
2020-07-22 14:55:27 -07:00
|
|
|
}
|
2020-08-26 12:46:30 -07:00
|
|
|
this._saveCallbacks = [];
|
2020-07-22 14:55:27 -07:00
|
|
|
|
2020-04-02 17:56:14 -07:00
|
|
|
this._finishedCallback();
|
|
|
|
}
|
|
|
|
}
|