Completed
Pull Request — master (#182)
by Sander
04:13
created

PassmanImporter.readCsv   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 8.8571
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
var PassmanImporter = {};
24
(function(window, $, PassmanImporter) {
25
	'use strict';
26
27
28
	PassmanImporter.parseRow_ = function(row) {
29
		// Strip leading quote.
30
		row = row.trim();
31
		var isQuoted = false;
32
		if (row.charAt(0) === '"') {
33
			row = row.substring(1);
34
			isQuoted = true;
35
		}
36
		if (row.charAt(row.length - 2) === '"') {
37
			row = row.substring(0, row.length - 2);
38
			isQuoted = true;
39
		}
40
		// Strip trailing quote. There seems to be a character between the last quote
41
		// and the line ending, hence 2 instead of 1.
42
		if(isQuoted === true) {
43
			row = row.split('","');
44
		} else {
45
			row = row.split(',');
46
		}
47
		return row;
48
	};
49
	PassmanImporter.htmlDecode = function(input){
50
		var e = document.createElement('div');
51
		e.innerHTML = input;
52
		return e.childNodes[0].nodeValue;
53
	};
54
	PassmanImporter.toObject_ = function(headings, row) {
55
		var result = {};
56
		for (var i = 0, ii = row.length; i < ii; i++) {
57
			if(headings[i]) {
58
				headings[i] = headings[i].replace(',', '_')
59
					.toLowerCase().replace(' ', '_')
60
					.replace('(', '').replace(')', '')
61
					.replace('"', '');
62
				result[headings[i]] = row[i];
63
			} else {
64
				result[ii] = row[i];
65
			}
66
		}
67
		return result;
68
	};
69
70
	PassmanImporter.join_ = function(arr, sep) {
71
		var parts = [];
72
		for (var i = 0, ii = arr.length; i < ii; i++) {
73
			if(arr[i]){
74
				parts.push(arr[i]);
75
			}
76
		}
77
		return parts.join(sep);
78
	};
79
80
	PassmanImporter.newCredential = function () {
81
		var credential = {
82
			'credential_id': null,
83
			'guid': null,
84
			'vault_id': null,
85
			'label': null,
86
			'description': null,
87
			'created': null,
88
			'changed': null,
89
			'tags': [],
90
			'email': null,
91
			'username': null,
92
			'password': null,
93
			'url': null,
94
			'favicon': null,
95
			'renew_interval': null,
96
			'expire_time': 0,
97
			'delete_time': 0,
98
			'files': [],
99
			'custom_fields': [],
100
			'otp': {},
101
			'hidden': false
102
		};
103
		return credential;
104
	};
105
106
	/**
107
	 * Read a csv
108
	 * @param csv the csv file contents
109
	 * @param hasHeadings does csv has headings? (default true)
110
	 */
111
	PassmanImporter.readCsv = function( csv, hasHeadings ){
112
		hasHeadings = (hasHeadings === undefined) ? true : hasHeadings;
113
		var lines = [];
114
		/** global: Papa */
115
		Papa.parse(csv, {
116
			complete: function(results) {
117
				if(results.data) {
118
					var headings = (hasHeadings) ? results.data[0] : null;
119
					var start = (hasHeadings) ? 1 : 0;
120
					for(var i = start; i < results.data.length; i++){
121
						var _row = (hasHeadings) ? PassmanImporter.toObject_(headings, results.data[i]) : results.data[i];
122
						lines.push(_row);
123
					}
124
				}
125
			}
126
		});
127
128
		return lines;
129
	};
130
131
	PassmanImporter.readJson = function (string){
132
		return JSON.parse(string);
133
	};
134
})(window, $, PassmanImporter);
135