mirror of
https://github.com/bitwarden/browser.git
synced 2024-08-25 23:03:07 +02:00
import service adjustments
This commit is contained in:
parent
a5476f12aa
commit
8b26d90e74
|
@ -1,7 +1,11 @@
|
||||||
import { Importer } from '../importers/importer';
|
import { Importer } from '../importers/importer';
|
||||||
export type ImportOptions = Array<{id: string, name: string}>;
|
|
||||||
|
export interface ImportOption {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
export abstract class ImportService {
|
export abstract class ImportService {
|
||||||
submit: (importer: Importer, fileContents: string) => Promise<Error>;
|
importOptions: ImportOption[];
|
||||||
getOptions: () => ImportOptions;
|
import: (importer: Importer, fileContents: string) => Promise<any>;
|
||||||
getImporter: (format: string) => Importer;
|
getImporter: (format: string) => Importer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { CipherService } from '../abstractions/cipher.service';
|
||||||
import { FolderService } from '../abstractions/folder.service';
|
import { FolderService } from '../abstractions/folder.service';
|
||||||
import { I18nService } from '../abstractions/i18n.service';
|
import { I18nService } from '../abstractions/i18n.service';
|
||||||
import {
|
import {
|
||||||
ImportOptions,
|
ImportOption,
|
||||||
ImportService as ImportServiceAbstraction,
|
ImportService as ImportServiceAbstraction,
|
||||||
} from '../abstractions/import.service';
|
} from '../abstractions/import.service';
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ import { UpmCsvImporter } from '../importers/upmCsvImporter';
|
||||||
import { ZohoVaultCsvImporter } from '../importers/zohoVaultCsvImporter';
|
import { ZohoVaultCsvImporter } from '../importers/zohoVaultCsvImporter';
|
||||||
|
|
||||||
export class ImportService implements ImportServiceAbstraction {
|
export class ImportService implements ImportServiceAbstraction {
|
||||||
importOptions: ImportOptions = [
|
importOptions: ImportOption[] = [
|
||||||
{ id: 'bitwardencsv', name: 'Bitwarden (csv)' },
|
{ id: 'bitwardencsv', name: 'Bitwarden (csv)' },
|
||||||
{ id: 'lastpasscsv', name: 'LastPass (csv)' },
|
{ id: 'lastpasscsv', name: 'LastPass (csv)' },
|
||||||
{ id: 'chromecsv', name: 'Chrome (csv)' },
|
{ id: 'chromecsv', name: 'Chrome (csv)' },
|
||||||
|
@ -84,24 +84,14 @@ export class ImportService implements ImportServiceAbstraction {
|
||||||
{ id: 'passkeepcsv', name: 'PassKeep (csv)' },
|
{ id: 'passkeepcsv', name: 'PassKeep (csv)' },
|
||||||
];
|
];
|
||||||
|
|
||||||
protected successNavigate: any[] = ['vault'];
|
|
||||||
|
|
||||||
constructor(protected cipherService: CipherService, protected folderService: FolderService,
|
constructor(protected cipherService: CipherService, protected folderService: FolderService,
|
||||||
protected apiService: ApiService, protected i18nService: I18nService) { }
|
protected apiService: ApiService, protected i18nService: I18nService) { }
|
||||||
|
|
||||||
async submit(importer: Importer, fileContents: string): Promise<Error> {
|
async import(importer: Importer, fileContents: string): Promise<any> {
|
||||||
if (importer === null) {
|
|
||||||
return new Error(this.i18nService.t('selectFormat'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileContents == null || fileContents === '') {
|
|
||||||
return new Error(this.i18nService.t('selectFile'));
|
|
||||||
}
|
|
||||||
|
|
||||||
const importResult = await importer.parse(fileContents);
|
const importResult = await importer.parse(fileContents);
|
||||||
if (importResult.success) {
|
if (importResult.success) {
|
||||||
if (importResult.folders.length === 0 && importResult.ciphers.length === 0) {
|
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) {
|
} else if (importResult.ciphers.length > 0) {
|
||||||
const halfway = Math.floor(importResult.ciphers.length / 2);
|
const halfway = Math.floor(importResult.ciphers.length / 2);
|
||||||
const last = importResult.ciphers.length - 1;
|
const last = importResult.ciphers.length - 1;
|
||||||
|
@ -109,23 +99,15 @@ export class ImportService implements ImportServiceAbstraction {
|
||||||
if (this.badData(importResult.ciphers[0]) &&
|
if (this.badData(importResult.ciphers[0]) &&
|
||||||
this.badData(importResult.ciphers[halfway]) &&
|
this.badData(importResult.ciphers[halfway]) &&
|
||||||
this.badData(importResult.ciphers[last])) {
|
this.badData(importResult.ciphers[last])) {
|
||||||
return new Error(this.i18nService.t('importFormatError'));
|
throw new Error(this.i18nService.t('importFormatError'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.postImport(importResult).then(() => {
|
await this.postImport(importResult);
|
||||||
return null;
|
|
||||||
}).catch((err) => {
|
|
||||||
return new Error(err);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
return new Error(this.i18nService.t('importFormatError'));
|
throw new Error(this.i18nService.t('importFormatError'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getOptions(): ImportOptions {
|
|
||||||
return this.importOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
getImporter(format: string): Importer {
|
getImporter(format: string): Importer {
|
||||||
if (format == null || format === '') {
|
if (format == null || format === '') {
|
||||||
return null;
|
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();
|
const request = new ImportCiphersRequest();
|
||||||
for (let i = 0; i < importResult.ciphers.length; i++) {
|
for (let i = 0; i < importResult.ciphers.length; i++) {
|
||||||
const c = await this.cipherService.encrypt(importResult.ciphers[i]);
|
const c = await this.cipherService.encrypt(importResult.ciphers[i]);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user