sftp_key.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. });