diff --git a/src/importers/aviraCsvImporter.ts b/src/importers/aviraCsvImporter.ts index bb10ad3927..41774ba75c 100644 --- a/src/importers/aviraCsvImporter.ts +++ b/src/importers/aviraCsvImporter.ts @@ -3,11 +3,6 @@ import { Importer } from './importer'; import { ImportResult } from '../models/domain/importResult'; -import { CipherView } from '../models/view/cipherView'; -import { LoginView } from '../models/view/loginView'; - -import { CipherType } from '../enums/cipherType'; - export class AviraCsvImporter extends BaseImporter implements Importer { parse(data: string): ImportResult { const result = new ImportResult(); @@ -18,11 +13,9 @@ export class AviraCsvImporter extends BaseImporter implements Importer { } results.forEach((value) => { - const cipher = new CipherView(); - cipher.type = CipherType.Login; + const cipher = this.initLoginCipher(); cipher.name = this.getValueOrDefault(value.name, this.getValueOrDefault(this.nameFromUrl(value.website), '--')); - cipher.login = new LoginView(); cipher.login.uris = this.makeUriArray(value.website); cipher.login.password = this.getValueOrDefault(value.password); @@ -33,6 +26,7 @@ export class AviraCsvImporter extends BaseImporter implements Importer { cipher.notes = this.getValueOrDefault(value.secondary_username); } + this.cleanupCipher(cipher); result.ciphers.push(cipher); }); diff --git a/src/importers/baseImporter.ts b/src/importers/baseImporter.ts index 8bdadfe7cf..cdef5ef338 100644 --- a/src/importers/baseImporter.ts +++ b/src/importers/baseImporter.ts @@ -2,10 +2,16 @@ import * as papa from 'papaparse'; import { ImportResult } from '../models/domain/importResult'; +import { CipherView } from '../models/view/cipherView'; import { CollectionView } from '../models/view/collectionView'; import { LoginUriView } from '../models/view/loginUriView'; import { Utils } from '../misc/utils'; +import { FieldView } from '../models/view/fieldView'; +import { LoginView } from '../models/view/loginView'; + +import { CipherType } from '../enums/cipherType'; +import { FieldType } from '../enums/fieldType'; export abstract class BaseImporter { organization = false; @@ -227,4 +233,58 @@ export abstract class BaseImporter { protected querySelectorAllDirectChild(parentEl: Element, query: string) { return Array.from(parentEl.querySelectorAll(query)).filter((el) => el.parentNode === parentEl); } + + protected initLoginCipher() { + const cipher = new CipherView(); + cipher.favorite = false; + cipher.notes = ''; + cipher.fields = []; + cipher.login = new LoginView(); + cipher.type = CipherType.Login; + return cipher; + } + + protected cleanupCipher(cipher: CipherView) { + if (cipher == null) { + return; + } + if (cipher.type !== CipherType.Login) { + cipher.login = null; + } + if (this.isNullOrWhitespace(cipher.name)) { + cipher.name = '--'; + } + if (this.isNullOrWhitespace(cipher.notes)) { + cipher.notes = null; + } else { + cipher.notes = cipher.notes.trim(); + } + if (cipher.fields != null && cipher.fields.length === 0) { + cipher.fields = null; + } + } + + protected processKvp(cipher: CipherView, key: string, value: string) { + if (this.isNullOrWhitespace(value)) { + return; + } + if (this.isNullOrWhitespace(key)) { + key = ''; + } + if (value.length > 200 || value.search(this.newLineRegex) > -1) { + if (cipher.notes == null) { + cipher.notes = ''; + } + cipher.notes += (key + ': ' + value + '\n'); + } else { + if (cipher.fields == null) { + cipher.fields = []; + } + const field = new FieldView(); + field.type = FieldType.Text; + field.name = key; + field.value = value; + cipher.fields.push(field); + } + } } diff --git a/src/importers/blurCsvImporter.ts b/src/importers/blurCsvImporter.ts index 33620181b5..9fd484a51f 100644 --- a/src/importers/blurCsvImporter.ts +++ b/src/importers/blurCsvImporter.ts @@ -3,11 +3,6 @@ import { Importer } from './importer'; import { ImportResult } from '../models/domain/importResult'; -import { CipherView } from '../models/view/cipherView'; -import { LoginView } from '../models/view/loginView'; - -import { CipherType } from '../enums/cipherType'; - export class BlurCsvImporter extends BaseImporter implements Importer { parse(data: string): ImportResult { const result = new ImportResult(); @@ -18,14 +13,12 @@ export class BlurCsvImporter extends BaseImporter implements Importer { } results.forEach((value) => { - const cipher = new CipherView(); - cipher.type = CipherType.Login; if (value.label === 'null') { value.label = null; } + const cipher = this.initLoginCipher(); cipher.name = this.getValueOrDefault(value.label, this.getValueOrDefault(this.nameFromUrl(value.domain), '--')); - cipher.login = new LoginView(); cipher.login.uris = this.makeUriArray(value.domain); cipher.login.password = this.getValueOrDefault(value.password); @@ -36,6 +29,7 @@ export class BlurCsvImporter extends BaseImporter implements Importer { cipher.notes = this.getValueOrDefault(value.username); } + this.cleanupCipher(cipher); result.ciphers.push(cipher); }); diff --git a/src/importers/keepassxCsvImporter.ts b/src/importers/keepassxCsvImporter.ts index 141e6e1771..c679a45cec 100644 --- a/src/importers/keepassxCsvImporter.ts +++ b/src/importers/keepassxCsvImporter.ts @@ -3,11 +3,7 @@ import { Importer } from './importer'; import { ImportResult } from '../models/domain/importResult'; -import { CipherView } from '../models/view/cipherView'; import { FolderView } from '../models/view/folderView'; -import { LoginView } from '../models/view/loginView'; - -import { CipherType } from '../enums/cipherType'; export class KeePassXCsvImporter extends BaseImporter implements Importer { parse(data: string): ImportResult { @@ -50,15 +46,13 @@ export class KeePassXCsvImporter extends BaseImporter implements Importer { result.folderRelationships.push([result.ciphers.length, folderIndex]); } - const cipher = new CipherView(); - cipher.type = CipherType.Login; - cipher.favorite = false; + const cipher = this.initLoginCipher(); cipher.notes = this.getValueOrDefault(value.Notes); cipher.name = this.getValueOrDefault(value.Title, '--'); - cipher.login = new LoginView(); cipher.login.username = this.getValueOrDefault(value.Username); cipher.login.password = this.getValueOrDefault(value.Password); cipher.login.uris = this.makeUriArray(value.URL); + this.cleanupCipher(cipher); result.ciphers.push(cipher); }); diff --git a/src/importers/padlockCsvImporter.ts b/src/importers/padlockCsvImporter.ts index dea2fdc99e..7e0cd4f153 100644 --- a/src/importers/padlockCsvImporter.ts +++ b/src/importers/padlockCsvImporter.ts @@ -3,14 +3,8 @@ import { Importer } from './importer'; import { ImportResult } from '../models/domain/importResult'; -import { CipherView } from '../models/view/cipherView'; import { CollectionView } from '../models/view/collectionView'; -import { FieldView } from '../models/view/fieldView'; import { FolderView } from '../models/view/folderView'; -import { LoginView } from '../models/view/loginView'; - -import { CipherType } from '../enums/cipherType'; -import { FieldType } from '../enums/fieldType'; export class PadlockCsvImporter extends BaseImporter implements Importer { parse(data: string): ImportResult { @@ -84,13 +78,8 @@ export class PadlockCsvImporter extends BaseImporter implements Importer { } } - const cipher = new CipherView(); - cipher.type = CipherType.Login; - cipher.favorite = false; - cipher.notes = ''; - cipher.fields = []; + const cipher = this.initLoginCipher(); cipher.name = this.getValueOrDefault(value[0], '--'); - cipher.login = new LoginView(); for (let i = 2; i < value.length; i++) { const header = headers[i].trim().toLowerCase(); @@ -105,27 +94,11 @@ export class PadlockCsvImporter extends BaseImporter implements Importer { } else if (this.uriFieldNames.indexOf(header) > -1) { cipher.login.uris = this.makeUriArray(value[i]); } else { - if (value[i].length > 200 || value[i].search(this.newLineRegex) > -1) { - cipher.notes += (headers[i] + ': ' + value[i] + '\n'); - } else { - const field = new FieldView(); - field.type = FieldType.Text; - field.name = headers[i]; - field.value = value[i]; - cipher.fields.push(field); - } + this.processKvp(cipher, headers[i], value[i]); } } - if (this.isNullOrWhitespace(cipher.notes)) { - cipher.notes = null; - } else { - cipher.notes = cipher.notes.trim(); - } - if (cipher.fields.length === 0) { - cipher.fields = null; - } - + this.cleanupCipher(cipher); result.ciphers.push(cipher); }); diff --git a/src/importers/safeInCloudXmlImporter.ts b/src/importers/safeInCloudXmlImporter.ts index 1edaf8139f..4029a2c979 100644 --- a/src/importers/safeInCloudXmlImporter.ts +++ b/src/importers/safeInCloudXmlImporter.ts @@ -3,14 +3,10 @@ import { Importer } from './importer'; import { ImportResult } from '../models/domain/importResult'; -import { CipherView } from '../models/view/cipherView'; -import { FieldView } from '../models/view/fieldView'; import { FolderView } from '../models/view/folderView'; -import { LoginView } from '../models/view/loginView'; import { SecureNoteView } from '../models/view/secureNoteView'; import { CipherType } from '../enums/cipherType'; -import { FieldType } from '../enums/fieldType'; import { SecureNoteType } from '../enums/secureNoteType'; export class SafeInCloudXmlImporter extends BaseImporter implements Importer { @@ -55,11 +51,8 @@ export class SafeInCloudXmlImporter extends BaseImporter implements Importer { } } - const cipher = new CipherView(); - cipher.favorite = false; - cipher.notes = ''; + const cipher = this.initLoginCipher(); cipher.name = this.getValueOrDefault(cardEl.getAttribute('title'), '--'); - cipher.fields = null; const cardType = cardEl.getAttribute('type'); if (cardType === 'note') { @@ -67,8 +60,6 @@ export class SafeInCloudXmlImporter extends BaseImporter implements Importer { cipher.secureNote = new SecureNoteView(); cipher.secureNote.type = SecureNoteType.Generic; } else { - cipher.type = CipherType.Login; - cipher.login = new LoginView(); Array.from(this.querySelectorAllDirectChild(cardEl, 'field')).forEach((fieldEl) => { const text = fieldEl.textContent; if (this.isNullOrWhitespace(text)) { @@ -84,17 +75,8 @@ export class SafeInCloudXmlImporter extends BaseImporter implements Importer { cipher.notes += (text + '\n'); } else if (fieldType === 'weblogin' || fieldType === 'website') { cipher.login.uris = this.makeUriArray(text); - } else if (text.length > 200) { - cipher.notes += (name + ': ' + text + '\n'); } else { - if (cipher.fields == null) { - cipher.fields = []; - } - const field = new FieldView(); - field.name = name; - field.value = text; - field.type = FieldType.Text; - cipher.fields.push(field); + this.processKvp(cipher, name, text); } }); } @@ -103,11 +85,7 @@ export class SafeInCloudXmlImporter extends BaseImporter implements Importer { cipher.notes += (notesEl.textContent + '\n'); }); - cipher.notes = cipher.notes.trim(); - if (cipher.notes === '') { - cipher.notes = null; - } - + this.cleanupCipher(cipher); result.ciphers.push(cipher); });