2018-06-10 05:33:12 +02:00
|
|
|
import {
|
|
|
|
Component,
|
2018-07-06 05:38:36 +02:00
|
|
|
OnInit,
|
2018-06-10 05:33:12 +02:00
|
|
|
} from '@angular/core';
|
2018-06-25 17:39:55 +02:00
|
|
|
import { Router } from '@angular/router';
|
2018-06-10 05:33:12 +02:00
|
|
|
|
2018-06-23 21:16:23 +02:00
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
|
|
|
import { I18nService } from 'jslib/abstractions/i18n.service';
|
2018-08-06 17:40:12 +02:00
|
|
|
import { ImportOption, ImportService } from 'jslib/abstractions/import.service';
|
2018-06-23 21:16:23 +02:00
|
|
|
|
2018-06-10 05:33:12 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-import',
|
|
|
|
templateUrl: 'import.component.html',
|
|
|
|
})
|
2018-07-06 05:38:36 +02:00
|
|
|
export class ImportComponent implements OnInit {
|
2018-08-06 17:40:12 +02:00
|
|
|
featuredImportOptions: ImportOption[];
|
|
|
|
importOptions: ImportOption[];
|
2018-06-23 21:16:23 +02:00
|
|
|
format: string = null;
|
|
|
|
fileContents: string;
|
2018-08-06 17:40:12 +02:00
|
|
|
formPromise: Promise<Error>;
|
2018-06-23 21:16:23 +02:00
|
|
|
|
2018-08-06 17:40:12 +02:00
|
|
|
protected organizationId: string = null;
|
2018-07-06 05:44:58 +02:00
|
|
|
protected successNavigate: any[] = ['vault'];
|
|
|
|
|
2018-07-06 05:38:36 +02:00
|
|
|
constructor(protected i18nService: I18nService, protected analytics: Angulartics2,
|
2018-08-06 17:40:12 +02:00
|
|
|
protected toasterService: ToasterService, protected importService: ImportService,
|
|
|
|
protected router: Router) { }
|
2018-06-23 21:16:23 +02:00
|
|
|
|
2018-07-06 05:38:36 +02:00
|
|
|
ngOnInit() {
|
|
|
|
this.setImportOptions();
|
|
|
|
this.importOptions.sort((a, b) => {
|
2018-06-23 21:16:23 +02:00
|
|
|
if (a.name == null && b.name != null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name != null && b.name == null) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (a.name == null && b.name == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.i18nService.collator ? this.i18nService.collator.compare(a.name, b.name) :
|
|
|
|
a.name.localeCompare(b.name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
2018-08-06 17:40:12 +02:00
|
|
|
const importer = this.importService.getImporter(this.format, this.organizationId != null);
|
2018-06-23 21:16:23 +02:00
|
|
|
if (importer === null) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('selectFormat'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileEl = document.getElementById('file') as HTMLInputElement;
|
|
|
|
const files = fileEl.files;
|
|
|
|
if ((files == null || files.length === 0) && (this.fileContents == null || this.fileContents === '')) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('selectFile'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fileContents = this.fileContents;
|
|
|
|
if (files != null && files.length > 0) {
|
|
|
|
try {
|
|
|
|
const content = await this.getFileContents(files[0]);
|
|
|
|
if (content != null) {
|
|
|
|
fileContents = content;
|
|
|
|
}
|
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileContents == null || fileContents === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('selectFile'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-06 17:40:12 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.importService.import(importer, fileContents, this.organizationId);
|
|
|
|
const error = await this.formPromise;
|
|
|
|
if (error != null) {
|
|
|
|
this.error(error);
|
2018-06-25 17:39:55 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-08-06 17:40:12 +02:00
|
|
|
this.analytics.eventTrack.next({
|
|
|
|
action: 'Imported Data',
|
|
|
|
properties: { label: this.format },
|
|
|
|
});
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('importSuccess'));
|
|
|
|
this.router.navigate(this.successNavigate);
|
|
|
|
} catch { }
|
2018-06-23 21:16:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 02:49:49 +02:00
|
|
|
getFormatInstructionTitle() {
|
2018-06-25 21:47:05 +02:00
|
|
|
if (this.format == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = this.featuredImportOptions.concat(this.importOptions).filter((o) => o.id === this.format);
|
|
|
|
if (results.length > 0) {
|
2018-06-26 02:49:49 +02:00
|
|
|
return this.i18nService.t('instructionsFor', results[0].name);
|
2018-06-25 21:47:05 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:38:36 +02:00
|
|
|
protected setImportOptions() {
|
2018-08-06 17:40:12 +02:00
|
|
|
this.featuredImportOptions = [{
|
|
|
|
id: null,
|
|
|
|
name: '-- ' + this.i18nService.t('select') + ' --',
|
|
|
|
}, ...this.importService.featuredImportOptions];
|
|
|
|
this.importOptions = this.importService.regularImportOptions;
|
2018-07-06 05:38:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 17:40:12 +02:00
|
|
|
private error(error: Error) {
|
2018-06-26 15:11:29 +02:00
|
|
|
this.analytics.eventTrack.next({
|
|
|
|
action: 'Import Data Failed',
|
|
|
|
properties: { label: this.format },
|
|
|
|
});
|
2018-08-06 17:40:12 +02:00
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'), error.message);
|
2018-06-26 15:11:29 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 21:16:23 +02:00
|
|
|
private getFileContents(file: File): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsText(file, 'utf-8');
|
|
|
|
reader.onload = (evt) => {
|
|
|
|
if (this.format === 'lastpasscsv' && file.type === 'text/html') {
|
|
|
|
const parser = new DOMParser();
|
2018-10-03 16:33:04 +02:00
|
|
|
const doc = parser.parseFromString((evt.target as any).result, 'text/html');
|
2018-06-23 21:16:23 +02:00
|
|
|
const pre = doc.querySelector('pre');
|
|
|
|
if (pre != null) {
|
|
|
|
resolve(pre.textContent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:33:04 +02:00
|
|
|
resolve((evt.target as any).result);
|
2018-06-23 21:16:23 +02:00
|
|
|
};
|
|
|
|
reader.onerror = () => {
|
|
|
|
reject();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|