1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 22:33:35 +00:00
Files
browser/src/commands/download.command.ts
Oscar Hinton 477066118e Add jslib as a "real" dependency (#321)
* Split jslib

* Bump jslib

* Bump jslib, replace alias with tsconfig-paths-webpack-plugin
2021-06-07 19:25:55 +02:00

33 lines
1.2 KiB
TypeScript

import * as fet from 'node-fetch';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
import { Response } from 'jslib-node/cli/models/response';
import { CliUtils } from '../utils';
export abstract class DownloadCommand {
constructor(protected cryptoService: CryptoService) { }
protected async saveAttachmentToFile(url: string, key: SymmetricCryptoKey, fileName: string, output?: string) {
const response = await fet.default(new fet.Request(url, { headers: { cache: 'no-cache' } }));
if (response.status !== 200) {
return Response.error('A ' + response.status + ' error occurred while downloading the attachment.');
}
try {
const buf = await response.arrayBuffer();
const decBuf = await this.cryptoService.decryptFromBytes(buf, key);
return await CliUtils.saveResultToFile(Buffer.from(decBuf), output, fileName);
} catch (e) {
if (typeof (e) === 'string') {
return Response.error(e);
} else {
return Response.error('An error occurred while saving the attachment.');
}
}
}
}