diff --git a/src/conversejs/converse.js b/src/conversejs/converse.js index c7d950736d15f1287c94080a03229cf0b59f00b4..8cdce6057ac643fe3e9f8c5d0186b2aeb1d5e3e6 100644 --- a/src/conversejs/converse.js +++ b/src/conversejs/converse.js @@ -78282,59 +78282,58 @@ const MessageMixin = { }, uploadFile() { - const xhr = new XMLHttpRequest(); - - xhr.onreadystatechange = () => { - if (xhr.readyState === XMLHttpRequest.DONE) { - headless_log.info('Status: ' + xhr.status); - - if (xhr.status === 200 || xhr.status === 201) { + if (document.querySelector('sib-auth')) { + document.querySelector('sib-auth').getFetch()(this.get('put'), { + method: 'PUT', + mode: 'cors', + cache: 'no-cache', + credentials: 'same-origin', + redirect: 'follow', + referrerPolicy: 'no-referrer', + body: this.file + }).then(res => { + if (!res.ok) { + throw new Error('Network failure'); + } else { + headless_log.info('Status: ' + res.status); + this.save({ 'upload': core_converse.SUCCESS, 'oob_url': this.get('get'), 'message': this.get('get') }); + } + }).catch(error => { + const { + __ + } = core_converse; + let message; + + if (error) { + message = __('Sorry, could not succesfully upload your file. Your server’s response: "%1$s"', error); } else { - xhr.onerror(); + message = __('Sorry, could not succesfully upload your file.'); } - } - }; - - xhr.upload.addEventListener('progress', evt => { - if (evt.lengthComputable) { - this.set('progress', evt.loaded / evt.total); - } - }, false); - - xhr.onerror = () => { + + this.save({ + 'type': 'error', + 'upload': core_converse.FAILURE, + 'message': message, + 'is_ephemeral': true + }); + }); + } else { const { __ } = core_converse; - let message; - - if (xhr.responseText) { - message = __('Sorry, could not succesfully upload your file. Your server’s response: "%1$s"', xhr.responseText); - } else { - message = __('Sorry, could not succesfully upload your file.'); - } - + message = __('Sorry, could not succesfully upload your file. Startin\'blox Auth is required.'); this.save({ 'type': 'error', 'upload': core_converse.FAILURE, 'message': message, 'is_ephemeral': true }); - }; - - xhr.open('PUT', this.get('put'), true); - xhr.setRequestHeader('Content-type', this.file.type); - - // Add custom headers to request - this.get('headers').forEach(header => { - xhr.setRequestHeader(header.getAttribute('name'), header.innerHTML); - }); - - xhr.send(this.file); + } } };