Completed
Pull Request — master (#309)
by Sander
04:26
created

PassmanExporter.csv.export   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
24
/** global: PassmanExporter */
25
PassmanExporter.csv = {
26
	info: {
27
		name: 'CSV',
28
		id: 'csv',
29
		description: 'Export credentials as a csv file.'
30
	}
31
};
32
33
PassmanExporter.csv.export = function (credentials, FileService, EncryptService) {
34
	/** global: C_Promise */
35
	return new C_Promise(function () {
36
		PassmanExporter.getCredentialsWithFiles(credentials, FileService, EncryptService).then((function(){
37
		    var headers = ['label', 'username', 'password', 'email', 'description', 'tags', 'url', 'custom_fields', 'files'];
38
		    var file_data = '"' + headers.join('","') + '"\n';
39
		    for (var i = 0; i < credentials.length; i++) {
40
			    var _credential = credentials[i];
41
			    var row_data = [];
42
			    for (var h = 0; h < headers.length; h++) {
43
				    var field = headers[h];
44
				    if (field === 'tags') {
45
					    var _tags = [];
46
					    for (var t = 0; t < _credential[field].length; t++) {
47
						    _tags.push(_credential[field][t].text);
48
					    }
49
					    var tag_data = '[' + _tags.join(",") + ']';
50
					    row_data.push('"' + tag_data + '"');
51
				    } 
52
				    else if (field == 'custom_fields' || field == 'files') {
53
					var _fields = JSON.stringify(_credential[field]);
54
					_fields = _fields.replaceAll('"', '""');
55
					row_data.push('"' + _fields + '"');
56
				    }
57
				    else {
58
					    row_data.push('"' + _credential[field] + '"');
59
				    }
60
			    }
61
			    var progress = {
62
				    percent: i / credentials.length * 100,
63
				    loaded: i,
64
				    total: credentials.length
65
			    };
66
			    this.call_progress(progress);
67
			    file_data += row_data.join(',') + "\n";
68
		    }
69
		    this.call_then();
70
		    download(file_data, 'passman-export.csv');
71
		}).bind(this)).progress(function() {
72
		    
73
		});
74
		
75
		
76
	});
77
};
78