2020-06-27 02:24:21 +02: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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Download } from '../../download';
|
2020-07-14 19:51:37 +02:00
|
|
|
import { DownloadChannel, DownloadInitializer, StreamChannel } from '../channels';
|
2020-07-01 07:21:17 +02:00
|
|
|
import { Dispatcher, DispatcherScope } from './dispatcher';
|
2020-07-14 19:51:37 +02:00
|
|
|
import { StreamDispatcher } from './streamDispatcher';
|
2020-06-27 02:24:21 +02:00
|
|
|
|
|
|
|
|
export class DownloadDispatcher extends Dispatcher<Download, DownloadInitializer> implements DownloadChannel {
|
|
|
|
|
constructor(scope: DispatcherScope, download: Download) {
|
|
|
|
|
super(scope, download, 'download', {
|
|
|
|
|
url: download.url(),
|
|
|
|
|
suggestedFilename: download.suggestedFilename(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 02:38:06 +02:00
|
|
|
async path(): Promise<{ value?: string }> {
|
|
|
|
|
const path = await this._object.path();
|
|
|
|
|
return { value: path || undefined };
|
2020-06-27 02:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 02:38:06 +02:00
|
|
|
async stream(): Promise<{ stream?: StreamChannel }> {
|
2020-07-14 19:51:37 +02:00
|
|
|
const stream = await this._object.createReadStream();
|
|
|
|
|
if (!stream)
|
2020-07-21 02:38:06 +02:00
|
|
|
return {};
|
2020-07-14 19:51:37 +02:00
|
|
|
await new Promise(f => stream.on('readable', f));
|
2020-07-15 03:26:50 +02:00
|
|
|
return { stream: new StreamDispatcher(this._scope, stream) };
|
2020-07-14 19:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-21 02:38:06 +02:00
|
|
|
async failure(): Promise<{ error?: string }> {
|
|
|
|
|
const error = await this._object.failure();
|
|
|
|
|
return { error: error || undefined };
|
2020-06-27 02:24:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(): Promise<void> {
|
|
|
|
|
await this._object.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|