summaryrefslogtreecommitdiffstats
path: root/apps/files_external/js/sftp_key.js
blob: 2b39628247c207c54e3e95d99a9583a7c66909a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$(document).ready(function() {

	$('#externalStorage tbody tr.\\\\OC\\\\Files\\\\Storage\\\\SFTP_Key').each(function() {
		var tr = $(this);
		var config = $(tr).find('.configuration');
		if ($(config).find('.sftp_key').length === 0) {
			setupTableRow(tr, config);
		}
	});

	// We can't catch the DOM elements being added, but we can pick up when
	// they receive focus
	$('#externalStorage').on('focus', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\SFTP_Key', function() {
		var tr = $(this);
		var config = $(tr).find('.configuration');

		if ($(config).find('.sftp_key').length === 0) {
			setupTableRow(tr, config);
		}
	});

	$('#externalStorage').on('click', '.sftp_key', function(event) {
		event.preventDefault();
		var tr = $(this).parent().parent();
		generateKeys(tr);
	});

	function setupTableRow(tr, config) {
		$(config).append($(document.createElement('input')).addClass('button sftp_key')
			.attr('type', 'button')
			.attr('value', t('files_external', 'Generate keys')));
		// If there's no private key, build one
		if (0 === $(config).find('[data-parameter="private_key"]').val().length) {
			generateKeys(tr);
		}
	}

	function generateKeys(tr) {
		var config = $(tr).find('.configuration');

		$.post(OC.filePath('files_external', 'ajax', 'sftp_key.php'), {}, function(result) {
			if (result && result.status === 'success') {
				$(config).find('[data-parameter="public_key"]').val(result.data.public_key);
				$(config).find('[data-parameter="private_key"]').val(result.data.private_key);
				OC.MountConfig.saveStorage(tr, function() {
					// Nothing to do
				});
			} else {
				OC.dialogs.alert(result.data.message, t('files_external', 'Error generating key pair') );
			}
		});
	}
});