Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
handle lastpass import when users try to upload html file instead of csv
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Oct 13, 2016
1 parent 400826b commit 004ddb1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 45 deletions.
128 changes: 84 additions & 44 deletions src/Web/wwwroot/app/services/importService.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,60 +82,100 @@
}

function importLastPass(file, success, error) {
Papa.parse(file, {
header: true,
complete: function (results) {
var folders = [],
sites = [],
siteRelationships = [];
if (file.type === 'text/html') {
var reader = new FileReader();
reader.readAsText(file, 'utf-8');
reader.onload = function (evt) {
var doc = $(evt.target.result);
var pre = doc.find('pre');

if (pre.length === 1) {
var csv = pre.text().trim();
var results = Papa.parse(csv, { header: true });
parseData(results.data);
}
else {
var foundPre = false;
for (var i = 0; i < doc.length; i++) {
if (doc[i].tagName === 'PRE') {
foundPre = true;
var csv = doc[i].outerText.trim();
var results = Papa.parse(csv, { header: true });
parseData(results.data);
break;
}
}

angular.forEach(results.data, function (value, key) {
if (!value.url || value.url === '') {
return;
if (!foundPre) {
error();
}
}
};

var folderIndex = folders.length,
siteIndex = sites.length,
hasFolder = value.grouping && value.grouping !== '' && value.grouping !== '(none)',
addFolder = hasFolder;
reader.onerror = function (evt) {
error();
};
}
else {
Papa.parse(file, {
header: true,
complete: function (results) {
parseData(results.data);
}
});
}

if (hasFolder) {
for (var i = 0; i < folders.length; i++) {
if (folders[i].name === value.grouping) {
addFolder = false;
folderIndex = i;
break;
}
}
}
function parseData(data) {
var folders = [],
sites = [],
siteRelationships = [];

sites.push({
favorite: value.fav === '1',
uri: value.url,
username: value.username && value.username !== '' ? value.username : null,
password: value.password,
notes: value.extra && value.extra !== '' ? value.extra : null,
name: value.name
});
angular.forEach(data, function (value, key) {
if (!value.url || value.url === '') {
return;
}

if (addFolder) {
folders.push({
name: value.grouping
});
var folderIndex = folders.length,
siteIndex = sites.length,
hasFolder = value.grouping && value.grouping !== '' && value.grouping !== '(none)',
addFolder = hasFolder;

if (hasFolder) {
for (var i = 0; i < folders.length; i++) {
if (folders[i].name === value.grouping) {
addFolder = false;
folderIndex = i;
break;
}
}
}

if (hasFolder) {
var relationship = {
key: siteIndex,
value: folderIndex
};
siteRelationships.push(relationship);
}
sites.push({
favorite: value.fav === '1',
uri: value.url,
username: value.username && value.username !== '' ? value.username : null,
password: value.password,
notes: value.extra && value.extra !== '' ? value.extra : null,
name: value.name
});

success(folders, sites, siteRelationships);
}
});
if (addFolder) {
folders.push({
name: value.grouping
});
}

if (hasFolder) {
var relationship = {
key: siteIndex,
value: folderIndex
};
siteRelationships.push(relationship);
}
});

success(folders, sites, siteRelationships);
}
}

function importSafeInCloudCsv(file, success, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/Web/wwwroot/app/tools/toolsImportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}, function () {
$uibModalInstance.dismiss('cancel');
$state.go('backend.vault').then(function () {
$analytics.eventTrack('Imported Data', { label: model.source });
$analytics.eventTrack('Imported Data', { label: $scope.model.source });
toastr.success('Data has been successfully imported into your vault.', 'Import Success');
});
}, importError);
Expand Down

0 comments on commit 004ddb1

Please sign in to comment.