Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
download attachments function from component
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jun 8, 2018
1 parent 8a1bc30 commit 4c083ee
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/angular/components/attachments.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AttachmentsComponent implements OnInit {
constructor(protected cipherService: CipherService, protected analytics: Angulartics2,
protected toasterService: ToasterService, protected i18nService: I18nService,
protected cryptoService: CryptoService, protected tokenService: TokenService,
protected platformUtilsService: PlatformUtilsService) { }
protected platformUtilsService: PlatformUtilsService, protected win: Window) { }

async ngOnInit() {
this.cipherDomain = await this.cipherService.get(this.cipherId);
Expand Down Expand Up @@ -122,4 +122,36 @@ export class AttachmentsComponent implements OnInit {

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

0 comments on commit 4c083ee

Please sign in to comment.