sftp_key.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. $(document).ready(function() {
  2. $('#externalStorage tbody tr.\\\\OC\\\\Files\\\\Storage\\\\SFTP_Key').each(function() {
  3. var tr = $(this);
  4. var config = $(tr).find('.configuration');
  5. if ($(config).find('.sftp_key').length === 0) {
  6. setupTableRow(tr, config);
  7. }
  8. });
  9. // We can't catch the DOM elements being added, but we can pick up when
  10. // they receive focus
  11. $('#externalStorage').on('focus', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\SFTP_Key', function() {
  12. var tr = $(this);
  13. var config = $(tr).find('.configuration');
  14. if ($(config).find('.sftp_key').length === 0) {
  15. setupTableRow(tr, config);
  16. }
  17. });
  18. $('#externalStorage').on('click', '.sftp_key', function(event) {
  19. event.preventDefault();
  20. var tr = $(this).parent().parent();
  21. generateKeys(tr);
  22. });
  23. function setupTableRow(tr, config) {
  24. $(config).append($(document.createElement('input')).addClass('button sftp_key')
  25. .attr('type', 'button')
  26. .attr('value', t('files_external', 'Generate keys')));
  27. // If there's no private key, build one
  28. if (0 === $(config).find('[data-parameter="private_key"]').val().length) {
  29. generateKeys(tr);
  30. }
  31. }
  32. function generateKeys(tr) {
  33. var config = $(tr).find('.configuration');
  34. $.post(OC.filePath('files_external', 'ajax', 'sftp_key.php'), {}, function(result) {
  35. if (result && result.status === 'success') {
  36. $(config).find('[data-parameter="public_key"]').val(result.data.public_key);
  37. $(config).find('[data-parameter="private_key"]').val(result.data.private_key);
  38. OCA.External.mountConfig.saveStorageConfig(tr, function() {
  39. // Nothing to do
  40. });
  41. } else {
  42. OC.dialogs.alert(result.data.message, t('files_external', 'Error generating key pair') );
  43. }
  44. });
  45. }
  46. });