bitwarden-estensione-browser/src/app/vault/attachments.component.ts

133 lines
5.1 KiB
TypeScript
Raw Normal View History

2018-01-30 05:19:55 +01:00
import * as template from './attachments.component.html';
import {
Component,
EventEmitter,
Input,
OnInit,
Output,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
2018-02-08 16:37:54 +01:00
import { Angulartics2 } from 'angulartics2';
2018-01-30 05:19:55 +01:00
import { CipherService } from 'jslib/abstractions/cipher.service';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-02-03 05:42:33 +01:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-01-30 05:19:55 +01:00
import { TokenService } from 'jslib/abstractions/token.service';
import { Cipher } from 'jslib/models/domain/cipher';
import { AttachmentView } from 'jslib/models/view/attachmentView';
2018-02-08 16:37:54 +01:00
import { CipherView } from 'jslib/models/view/cipherView';
2018-01-30 05:19:55 +01:00
@Component({
selector: 'app-vault-attachments',
template: template,
})
export class AttachmentsComponent implements OnInit {
@Input() cipherId: string;
cipher: CipherView;
cipherDomain: Cipher;
hasUpdatedKey: boolean;
canAccessAttachments: boolean;
2018-02-01 03:56:47 +01:00
formPromise: Promise<any>;
deletePromises: { [id: string]: Promise<any>; } = {};
2018-01-30 05:19:55 +01:00
constructor(private cipherService: CipherService, private analytics: Angulartics2,
private toasterService: ToasterService, private i18nService: I18nService,
2018-02-03 05:42:33 +01:00
private cryptoService: CryptoService, private tokenService: TokenService,
private platformUtilsService: PlatformUtilsService) { }
2018-01-30 05:19:55 +01:00
async ngOnInit() {
this.cipherDomain = await this.cipherService.get(this.cipherId);
this.cipher = await this.cipherDomain.decrypt();
const key = await this.cryptoService.getEncKey();
this.hasUpdatedKey = key != null;
const isPremium = this.tokenService.getPremium();
this.canAccessAttachments = isPremium || this.cipher.organizationId != null;
if (!this.canAccessAttachments) {
2018-02-03 05:42:33 +01:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('premiumRequiredDesc'), this.i18nService.t('premiumRequired'),
2018-02-08 16:37:54 +01:00
this.i18nService.t('learnMore'), this.i18nService.t('cancel'));
2018-02-03 05:42:33 +01:00
if (confirmed) {
this.platformUtilsService.launchUri('https://vault.bitwarden.com/#/?premium=purchase');
}
2018-01-30 05:19:55 +01:00
} else if (!this.hasUpdatedKey) {
2018-02-03 05:42:33 +01:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('updateKey'), this.i18nService.t('featureUnavailable'),
2018-02-08 16:37:54 +01:00
this.i18nService.t('learnMore'), this.i18nService.t('cancel'), 'warning');
2018-02-03 05:42:33 +01:00
if (confirmed) {
this.platformUtilsService.launchUri('https://help.bitwarden.com/article/update-encryption-key/');
}
2018-01-30 05:19:55 +01:00
}
}
2018-02-01 03:56:47 +01:00
async submit() {
2018-01-30 05:19:55 +01:00
if (!this.hasUpdatedKey) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('updateKey'));
return;
}
const fileEl = document.getElementById('file') as HTMLInputElement;
const files = fileEl.files;
if (files == null || files.length === 0) {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('selectFile'));
return;
}
if (files[0].size > 104857600) { // 100 MB
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('maxFileSize'));
return;
}
2018-02-01 03:56:47 +01:00
try {
this.formPromise = this.cipherService.saveAttachmentWithServer(this.cipherDomain, files[0]);
this.cipherDomain = await this.formPromise;
this.cipher = await this.cipherDomain.decrypt();
this.analytics.eventTrack.next({ action: 'Added Attachment' });
this.toasterService.popAsync('success', null, this.i18nService.t('attachmentSaved'));
} catch { }
2018-01-30 05:19:55 +01:00
// reset file input
// ref: https://stackoverflow.com/a/20552042
fileEl.type = '';
fileEl.type = 'file';
fileEl.value = '';
}
async delete(attachment: AttachmentView) {
2018-02-01 03:56:47 +01:00
if (this.deletePromises[attachment.id] != null) {
2018-01-30 05:19:55 +01:00
return;
}
2018-02-03 05:42:33 +01:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('deleteAttachmentConfirmation'), this.i18nService.t('deleteAttachment'),
2018-02-08 16:37:54 +01:00
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
2018-02-03 05:42:33 +01:00
if (!confirmed) {
2018-02-01 03:56:47 +01:00
return;
2018-01-30 05:19:55 +01:00
}
2018-02-01 03:56:47 +01:00
try {
this.deletePromises[attachment.id] = this.cipherService.deleteAttachmentWithServer(
this.cipher.id, attachment.id);
await this.deletePromises[attachment.id];
this.analytics.eventTrack.next({ action: 'Deleted Attachment' });
this.toasterService.popAsync('success', null, this.i18nService.t('deletedAttachment'));
const i = this.cipher.attachments.indexOf(attachment);
if (i > -1) {
this.cipher.attachments.splice(i, 1);
}
} catch { }
this.deletePromises[attachment.id] = null;
2018-01-30 05:19:55 +01:00
}
}