test.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <title>SFTPGo WebClient - External integration test</title>
  8. </head>
  9. <body>
  10. <textarea id="textarea_test" name="textarea_test" rows="6" cols="80">The text here will be sent to SFTPGo as blob</textarea>
  11. <br>
  12. <button onclick="saveBlob(false);">Save</button>
  13. <br>
  14. <button onclick="saveBlob(true);">Save binary file</button>
  15. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  16. <script type="text/javascript">
  17. var fileName;
  18. var sftpgoUser;
  19. // in real world usage set the origin when you call postMessage, we use `*` for testing purpose here
  20. $(document).ready(function () {
  21. if (window.opener == null || window.opener.closed) {
  22. console.log("windows opener gone!");
  23. return;
  24. }
  25. // notify SFTPGo that the page is ready to receive the file
  26. window.opener.postMessage({type: 'ready'},"*");
  27. });
  28. window.addEventListener('message', (event) => {
  29. if (window.opener == null || window.opener.closed) {
  30. console.log("windows opener gone!");
  31. return;
  32. }
  33. // you should check the origin before continuing
  34. console.log("new message: "+JSON.stringify(event.data));
  35. switch (event.data.type){
  36. case 'readyResponse':
  37. // after sending the ready request SFTPGo will reply with this response
  38. // now you know the file name and the SFTPGo user
  39. fileName = event.data.file_name;
  40. sftpgoUser = event.data.user;
  41. console.log("ready response received, file name: " + fileName+" SFTPGo user: "+sftpgoUser);
  42. // you can initialize your viewer/editor based on the file extension and request the blob
  43. window.opener.postMessage({type: 'sendBlob'}, "*");
  44. break;
  45. case 'blobDownloadStart':
  46. // SFTPGo may take a while to read the file, just before it starts reading it will send this message.
  47. // You can initialize a spinner if required for this file or simply ignore this message
  48. console.log("blob download start received, file name: " + fileName+" SFTPGo user: "+sftpgoUser);
  49. break;
  50. case 'blob':
  51. // we received the file as blob
  52. var extension = fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2).toLowerCase();
  53. console.log("blob received, file name: " + fileName+" extension: "+extension+" SFTPGo user: "+sftpgoUser);
  54. if (extension == "txt"){
  55. event.data.file.text().then(function(text){
  56. $("#textarea_test").val(text);
  57. });
  58. }
  59. break;
  60. case 'blobSaveResult':
  61. // event.data.status is OK or KO, if KO message is not empty
  62. console.log("blob save status: "+event.data.status+", message: "+event.data.message);
  63. if (event.data.status == "OK"){
  64. console.log("blob saved, I'm useless now, close me");
  65. }
  66. break;
  67. default:
  68. console.log("Unsupported message: " + JSON.stringify(event.data));
  69. }
  70. });
  71. function saveBlob(binary){
  72. // if we have modified the file we can send it back to SFTPGo as a blob for saving
  73. console.log("save blob, binary? "+binary);
  74. if (binary){
  75. // we download and save the SFTPGo logo
  76. fetch('https://raw.githubusercontent.com/drakkan/sftpgo/main/docs/howto/img/logo.png')
  77. .then(response => response.blob())
  78. .then(function(responseBlob){
  79. var blob = new File([responseBlob], fileName);
  80. window.opener.postMessage({
  81. type: 'saveBlob',
  82. file: blob
  83. },"*");
  84. });
  85. } else {
  86. var blob = new Blob([$("#textarea_test").val()]);
  87. window.opener.postMessage({
  88. type: 'saveBlob',
  89. file: blob
  90. },"*");
  91. }
  92. }
  93. </script>
  94. </body>