download attachments function from component

This commit is contained in:
Kyle Spearrin 2018-06-08 12:14:02 -04:00
parent 8a1bc30fc9
commit 4c083eeb92
1 changed files with 33 additions and 1 deletions

View File

@ -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;
}
} }