file_transfer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /*
  4. * the following function is based on jquery.binarytransport.js
  5. * made by Henry Algus <henryalgus@gmail.com>
  6. */
  7. $.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
  8. // check for conditions and support for blob / arraybuffer response type
  9. if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob)))))
  10. {
  11. return {
  12. // create new XMLHttpRequest
  13. send: function(headers, callback){
  14. // setup all variables
  15. var xhr = new XMLHttpRequest(),
  16. url = options.url,
  17. type = options.type,
  18. async = options.async || true,
  19. // blob or arraybuffer. Default is blob
  20. dataType = options.responseType || "blob",
  21. data = options.data || null,
  22. username = options.username || null,
  23. password = options.password || null;
  24. xhr.addEventListener('load', function(){
  25. var data = {};
  26. data[options.dataType] = xhr.response;
  27. // make callback and send data
  28. callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
  29. });
  30. xhr.addEventListener('progress', function(event){
  31. if (! event.lengthComputable) {
  32. return
  33. }
  34. var percentComplete = event.loaded / event.total;
  35. originalOptions.percentageStatus(percentComplete)
  36. });
  37. xhr.open(type, url, async, username, password);
  38. // setup custom headers
  39. for (var i in headers ) {
  40. xhr.setRequestHeader(i, headers[i]);
  41. }
  42. xhr.responseType = dataType;
  43. xhr.send(data);
  44. },
  45. abort: function(){
  46. jqXHR.abort();
  47. }
  48. };
  49. }
  50. });
  51. var isInstallingPackage = false
  52. function SimpleTransference(from, to, options) {
  53. this.origin = from.replace(/\/?$/, '/')
  54. this.destination = to.replace(/\/?$/, '/')
  55. this.options = $.extend({ from_args: {},
  56. to_args: {}
  57. }, options)
  58. this.request = null;
  59. // download reauthorize
  60. this.reauthorizeDownload = null;
  61. this.reauthorizedDownload = false;
  62. // upload reauthorize
  63. this.reauthorizeUpload = null;
  64. this.reauthorizedUpload = false;
  65. var self = this
  66. this.start = function () {
  67. isInstallingPackage = true
  68. self.reauthorizedUpload = false
  69. var req = $.ajax($.extend({
  70. type: 'GET',
  71. url: self.origin,
  72. success: self.upload,
  73. dataType: 'binary',
  74. cache: false,
  75. global: false,
  76. error: function (resp) {
  77. if (resp.status == 401 && self.reauthorizeDownload != null && ! self.reauthorizedDownload) {
  78. console.log("[TRANSFERENCE] download unauthorized, retrying authentication...")
  79. self.reauthorizedDownload = true
  80. self.reauthorizeDownload(function (ok, options) {
  81. if (ok) {
  82. console.log("[TRANSFERENCE] authentication succeeded")
  83. self.options = $.extend(self.options, options)
  84. self.start()
  85. } else {
  86. console.log("[TRANSFERENCE] authentication failed")
  87. self.abort(resp.statusText)
  88. }
  89. })
  90. return;
  91. }
  92. self.abort(resp.statusText)
  93. },
  94. percentageStatus: function (percentage) {
  95. self.percentageStatus(percentage)
  96. },
  97. }, self.options.from_args))
  98. self.request = req
  99. }
  100. this.upload = function (file) {
  101. self.reauthorizedDownload = false
  102. var req = $.ajax($.extend({
  103. method: 'POST',
  104. url: self.destination,
  105. data: file,
  106. contentType: file.type,
  107. success: self.success,
  108. processData: false,
  109. error: function (resp) {
  110. if (resp.status == 401 && self.reauthorizeUpload != null && ! self.reauthorizedUpload) {
  111. console.log("[TRANSFERENCE] upload unauthorized, retrying authentication...")
  112. self.reauthorizedUpload = true
  113. self.reauthorizeUpload(function (ok, options) {
  114. if (ok) {
  115. console.log("[TRANSFERENCE] authentication succeeded")
  116. self.options = $.extend(self.options, options)
  117. self.upload()
  118. } else {
  119. console.log("[TRANSFERENCE] authentication failed")
  120. self.abort(resp.statusText)
  121. }
  122. })
  123. return;
  124. }
  125. self.abort(resp.statusText)
  126. },
  127. dataType: 'json',
  128. cache: false,
  129. global: false,
  130. }, self.options.to_args))
  131. self.request = req
  132. }
  133. this.percentageStatus = function (percentage) {
  134. self.reportPercentageStatus(percentage)
  135. }
  136. this.success = function (resp) {
  137. isInstallingPackage = false
  138. self.reportFinished(resp)
  139. }
  140. this.abort = function (error) {
  141. isInstallingPackage = false
  142. // abort the in-flight request, if any (a no-op on one that already completed)
  143. if (self.request != null) {
  144. self.request.abort()
  145. self.request = null
  146. }
  147. self.reportError(error)
  148. }
  149. this.release = function () {
  150. for (var attr in this) {
  151. delete this[attr]
  152. }
  153. }
  154. this.reportPercentageStatus = function (percentage) {}
  155. this.reportFinished = function () {}
  156. this.reportError = function (error) {}
  157. }