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

js/importers/importer-zohocsv.js   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 57
rs 9.6818

1 Function

Rating   Name   Duplication   Size   Complexity  
B PassmanImporter.zohoCsv.readFile 0 44 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
// Importers should always start with this
24
/** global: PassmanImporter */
25
var PassmanImporter = PassmanImporter || {};
1 ignored issue
show
Bug introduced by
The variable PassmanImporter seems to be never initialized.
Loading history...
26
27
(function (window, $, PassmanImporter) {
28
	'use strict';
29
30
	// Define the importer
31
	PassmanImporter.zohoCsv = {
32
		info: {
33
			name: 'ZOHO csv',
34
			id: 'zohoCsv',
35
			description: 'Create an csv export. Go to Tools ->  Export secrets -> Select "General CSV" and click "Export Secrets"'
36
		}
37
	};
38
39
	PassmanImporter.zohoCsv.readFile = function (file_data) {
40
		/** global: C_Promise */
41
		return new C_Promise(function () {
42
			var parsed_csv = PassmanImporter.readCsv(file_data);
43
44
			var credential_list = [];
45
			for (var i = 0; i < parsed_csv.length; i++) {
46
				var row = parsed_csv[i];
47
				var _credential = PassmanImporter.newCredential();
48
				_credential.label = row.secret_name;
49
				_credential.url = row.secret_url;
50
				_credential.description = row.notes;
51
				if (row.hasOwnProperty('secretdata')) {
52
					var rows = row.secretdata.split("\n");
53
					for (var r = 0; r < rows.length; r++) {
54
						var cells = rows[r].split(':');
55
						var key = cells[0];
56
						var value = cells.slice(1).join(':');
57
						if (key && value) {
58
							_credential.custom_fields.push(
59
								{
60
									'label': key,
61
									'value': value,
62
									'secret': false
63
								}
64
							);
65
						}
66
					}
67
				}
68
				if (_credential.label !== "") {
69
					credential_list.push(_credential);
70
				}
71
72
				var progress = {
73
					percent: i / parsed_csv.length * 100,
74
					loaded: i,
75
					total: parsed_csv.length
76
				};
77
78
				this.call_progress(progress);
79
			}
80
			this.call_then(credential_list);
81
		});
82
	};
83
})(window, $, PassmanImporter);
84