mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 16:53:34 +00:00
download attachments function from component
This commit is contained in:
@@ -32,7 +32,7 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
constructor(protected cipherService: CipherService, protected analytics: Angulartics2,
|
constructor(protected cipherService: CipherService, protected analytics: Angulartics2,
|
||||||
protected toasterService: ToasterService, protected i18nService: I18nService,
|
protected toasterService: ToasterService, protected i18nService: I18nService,
|
||||||
protected cryptoService: CryptoService, protected tokenService: TokenService,
|
protected cryptoService: CryptoService, protected tokenService: TokenService,
|
||||||
protected platformUtilsService: PlatformUtilsService) { }
|
protected platformUtilsService: PlatformUtilsService, protected win: Window) { }
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
this.cipherDomain = await this.cipherService.get(this.cipherId);
|
this.cipherDomain = await this.cipherService.get(this.cipherId);
|
||||||
@@ -122,4 +122,36 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
|
|
||||||
this.deletePromises[attachment.id] = null;
|
this.deletePromises[attachment.id] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async download(attachment: AttachmentView) {
|
||||||
|
const a = (attachment as any);
|
||||||
|
if (a.downloading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.canAccessAttachments) {
|
||||||
|
this.toasterService.popAsync('error', this.i18nService.t('premiumRequired'),
|
||||||
|
this.i18nService.t('premiumRequiredDesc'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
a.downloading = true;
|
||||||
|
const response = await fetch(new Request(attachment.url, { cache: 'no-cache' }));
|
||||||
|
if (response.status !== 200) {
|
||||||
|
this.toasterService.popAsync('error', null, this.i18nService.t('errorOccurred'));
|
||||||
|
a.downloading = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const buf = await response.arrayBuffer();
|
||||||
|
const key = await this.cryptoService.getOrgKey(this.cipher.organizationId);
|
||||||
|
const decBuf = await this.cryptoService.decryptFromBytes(buf, key);
|
||||||
|
this.platformUtilsService.saveFile(this.win, decBuf, null, attachment.fileName);
|
||||||
|
} catch (e) {
|
||||||
|
this.toasterService.popAsync('error', null, this.i18nService.t('errorOccurred'));
|
||||||
|
}
|
||||||
|
|
||||||
|
a.downloading = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user