2020-06-26 17:24:21 -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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
import * as channels from '../protocol/channels';
|
2020-06-26 17:24:21 -07:00
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { Readable } from 'stream';
|
2020-07-14 10:51:37 -07:00
|
|
|
import { Stream } from './stream';
|
2020-08-26 12:46:30 -07:00
|
|
|
import { Browser } from './browser';
|
|
|
|
import { BrowserContext } from './browserContext';
|
2021-02-11 06:36:15 -08:00
|
|
|
import fs from 'fs';
|
2020-08-26 12:46:30 -07:00
|
|
|
import { mkdirIfNeeded } from '../utils/utils';
|
2020-12-26 17:05:57 -08:00
|
|
|
import * as api from '../../types/types';
|
2020-06-26 17:24:21 -07:00
|
|
|
|
2020-12-26 17:05:57 -08:00
|
|
|
export class Download extends ChannelOwner<channels.DownloadChannel, channels.DownloadInitializer> implements api.Download {
|
2020-09-14 16:50:47 +02:00
|
|
|
private _browser: Browser | null;
|
2020-08-26 12:46:30 -07:00
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
static from(download: channels.DownloadChannel): Download {
|
2020-07-01 18:36:09 -07:00
|
|
|
return (download as any)._object;
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:05:16 -07:00
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.DownloadInitializer) {
|
2020-07-10 18:00:10 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-08-26 12:46:30 -07:00
|
|
|
this._browser = (parent as BrowserContext)._browser;
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
url(): string {
|
|
|
|
return this._initializer.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestedFilename(): string {
|
|
|
|
return this._initializer.suggestedFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
async path(): Promise<string | null> {
|
2020-08-26 12:46:30 -07:00
|
|
|
if (this._browser && this._browser._isRemote)
|
|
|
|
throw new Error(`Path is not available when using browserType.connect(). Use download.saveAs() to save a local copy.`);
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('download.path', async (channel: channels.DownloadChannel) => {
|
|
|
|
return (await channel.path()).value || null;
|
|
|
|
});
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 14:55:27 -07:00
|
|
|
async saveAs(path: string): Promise<void> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('download.saveAs', async (channel: channels.DownloadChannel) => {
|
2020-08-26 12:46:30 -07:00
|
|
|
if (!this._browser || !this._browser._isRemote) {
|
2021-02-19 16:21:39 -08:00
|
|
|
await channel.saveAs({ path });
|
2020-08-26 12:46:30 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:21:39 -08:00
|
|
|
const result = await channel.saveAsStream();
|
2020-08-26 12:46:30 -07:00
|
|
|
const stream = Stream.from(result.stream);
|
|
|
|
await mkdirIfNeeded(path);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
stream.stream().pipe(fs.createWriteStream(path))
|
|
|
|
.on('finish' as any, resolve)
|
|
|
|
.on('error' as any, reject);
|
|
|
|
});
|
2020-07-22 14:55:27 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:24:21 -07:00
|
|
|
async failure(): Promise<string | null> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('download.failure', async (channel: channels.DownloadChannel) => {
|
|
|
|
return (await channel.failure()).error || null;
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async createReadStream(): Promise<Readable | null> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('download.createReadStream', async (channel: channels.DownloadChannel) => {
|
|
|
|
const result = await channel.stream();
|
2021-01-22 06:49:59 -08:00
|
|
|
if (!result.stream)
|
|
|
|
return null;
|
|
|
|
const stream = Stream.from(result.stream);
|
|
|
|
return stream.stream();
|
|
|
|
});
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(): Promise<void> {
|
2021-02-19 16:21:39 -08:00
|
|
|
return this._wrapApiCall('download.delete', async (channel: channels.DownloadChannel) => {
|
|
|
|
return channel.delete();
|
2021-01-22 06:49:59 -08:00
|
|
|
});
|
2020-06-26 17:24:21 -07:00
|
|
|
}
|
|
|
|
}
|