1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-07-16 13:55:52 +02:00

import service adjustments

This commit is contained in:
Kyle Spearrin 2018-08-06 10:37:57 -04:00
parent a5476f12aa
commit 8b26d90e74
2 changed files with 15 additions and 29 deletions

View File

@ -1,7 +1,11 @@
import { Importer } from '../importers/importer';
export type ImportOptions = Array<{id: string, name: string}>;
export interface ImportOption {
id: string;
name: string;
}
export abstract class ImportService {
submit: (importer: Importer, fileContents: string) => Promise<Error>;
getOptions: () => ImportOptions;
importOptions: ImportOption[];
import: (importer: Importer, fileContents: string) => Promise<any>;
getImporter: (format: string) => Importer;
}

View File

@ -3,7 +3,7 @@ import { CipherService } from '../abstractions/cipher.service';
import { FolderService } from '../abstractions/folder.service';
import { I18nService } from '../abstractions/i18n.service';
import {
ImportOptions,
ImportOption,
ImportService as ImportServiceAbstraction,
} from '../abstractions/import.service';
@ -51,7 +51,7 @@ import { UpmCsvImporter } from '../importers/upmCsvImporter';
import { ZohoVaultCsvImporter } from '../importers/zohoVaultCsvImporter';
export class ImportService implements ImportServiceAbstraction {
importOptions: ImportOptions = [
importOptions: ImportOption[] = [
{ id: 'bitwardencsv', name: 'Bitwarden (csv)' },
{ id: 'lastpasscsv', name: 'LastPass (csv)' },
{ id: 'chromecsv', name: 'Chrome (csv)' },
@ -84,24 +84,14 @@ export class ImportService implements ImportServiceAbstraction {
{ id: 'passkeepcsv', name: 'PassKeep (csv)' },
];
protected successNavigate: any[] = ['vault'];
constructor(protected cipherService: CipherService, protected folderService: FolderService,
protected apiService: ApiService, protected i18nService: I18nService) { }
async submit(importer: Importer, fileContents: string): Promise<Error> {
if (importer === null) {
return new Error(this.i18nService.t('selectFormat'));
}
if (fileContents == null || fileContents === '') {
return new Error(this.i18nService.t('selectFile'));
}
async import(importer: Importer, fileContents: string): Promise<any> {
const importResult = await importer.parse(fileContents);
if (importResult.success) {
if (importResult.folders.length === 0 && importResult.ciphers.length === 0) {
return new Error(this.i18nService.t('importNothingError'));
throw new Error(this.i18nService.t('importNothingError'));
} else if (importResult.ciphers.length > 0) {
const halfway = Math.floor(importResult.ciphers.length / 2);
const last = importResult.ciphers.length - 1;
@ -109,23 +99,15 @@ export class ImportService implements ImportServiceAbstraction {
if (this.badData(importResult.ciphers[0]) &&
this.badData(importResult.ciphers[halfway]) &&
this.badData(importResult.ciphers[last])) {
return new Error(this.i18nService.t('importFormatError'));
throw new Error(this.i18nService.t('importFormatError'));
}
}
return this.postImport(importResult).then(() => {
return null;
}).catch((err) => {
return new Error(err);
});
await this.postImport(importResult);
} else {
return new Error(this.i18nService.t('importFormatError'));
throw new Error(this.i18nService.t('importFormatError'));
}
}
getOptions(): ImportOptions {
return this.importOptions;
}
getImporter(format: string): Importer {
if (format == null || format === '') {
return null;
@ -204,7 +186,7 @@ export class ImportService implements ImportServiceAbstraction {
}
}
protected async postImport(importResult: ImportResult) {
private async postImport(importResult: ImportResult) {
const request = new ImportCiphersRequest();
for (let i = 0; i < importResult.ciphers.length; i++) {
const c = await this.cipherService.encrypt(importResult.ciphers[i]);