Path: blob/main/src/vs/platform/download/common/downloadService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CancellationToken } from '../../../base/common/cancellation.js';6import { Schemas } from '../../../base/common/network.js';7import { URI } from '../../../base/common/uri.js';8import { IDownloadService } from './download.js';9import { IFileService } from '../../files/common/files.js';10import { asTextOrError, IRequestService } from '../../request/common/request.js';1112export class DownloadService implements IDownloadService {1314declare readonly _serviceBrand: undefined;1516constructor(17@IRequestService private readonly requestService: IRequestService,18@IFileService private readonly fileService: IFileService19) { }2021async download(resource: URI, target: URI, cancellationToken: CancellationToken = CancellationToken.None): Promise<void> {22if (resource.scheme === Schemas.file || resource.scheme === Schemas.vscodeRemote) {23// Intentionally only support this for file|remote<->file|remote scenarios24await this.fileService.copy(resource, target);25return;26}27const options = { type: 'GET', url: resource.toString(true) };28const context = await this.requestService.request(options, cancellationToken);29if (context.res.statusCode === 200) {30await this.fileService.writeFile(target, context.stream);31} else {32const message = await asTextOrError(context);33throw new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`);34}35}36}373839