Commit 83c0d42e authored by worlword's avatar worlword

changed line-endings in converse-httpFileUpload.js

parent 530e7f9d
/* /*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 Klaus Herberth <klaus@jsxc.org> Copyright (c) 2014 Klaus Herberth <klaus@jsxc.org>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
/** /**
* Implements Http File Upload (XEP-0363) * Implements Http File Upload (XEP-0363)
* *
* @see {@link http://xmpp.org/extensions/xep-0363.html} * @see {@link http://xmpp.org/extensions/xep-0363.html}
*/ */
(function (root, factory) { (function (root, factory) {
define([ define([
"converse-core", "converse-core",
], factory); ], factory);
}(this, function ( }(this, function (
converse converse
) { ) {
"use strict"; "use strict";
const { $msg, Backbone, Strophe, _, b64_sha1, moment, utils } = converse.env; const { $msg, Backbone, Strophe, _, b64_sha1, moment, utils } = converse.env;
Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload'); Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload');
var requestSlotUrl; var requestSlotUrl;
var ready; var ready;
var httpUploadOption = { var httpUploadOption = {
enable: true enable: true
} }
converse.plugins.add('converse-httpFileUpload', { converse.plugins.add('converse-httpFileUpload', {
dependencies: ["converse-chatboxes", "converse-disco"], dependencies: ["converse-chatboxes", "converse-disco"],
initialize() { initialize() {
const { _converse } = this, const { _converse } = this,
{ __ } = _converse; { __ } = _converse;
var connection = _converse.connection; var connection = _converse.connection;
var domain; var domain;
var file; var file;
var chatBox; var chatBox;
_converse.FileUpload = Backbone.NativeView.extend({ _converse.FileUpload = Backbone.NativeView.extend({
/** /**
* Set up http file upload. * Set up http file upload.
* *
* @param {*} connection the current strophe-connection * @param {*} connection the current strophe-connection
*/ */
initFiletransfer () { initFiletransfer () {
connection = _converse.connection; connection = _converse.connection;
domain = _converse.connection.domain; domain = _converse.connection.domain;
if (httpUploadOption && requestSlotUrl != undefined) { if (httpUploadOption && requestSlotUrl != undefined) {
ready = true; ready = true;
return; return;
} }
this.discoverUploadService(); this.discoverUploadService();
}, },
/** /**
* Discover upload service for http upload. * Discover upload service for http upload.
* *
*/ */
discoverUploadService () { discoverUploadService () {
var self = this; var self = this;
console.log('discover http upload service'); console.log('discover http upload service');
connection.disco.items(domain, null, function(items) { connection.disco.items(domain, null, function(items) {
var childs = items.getElementsByTagName('item'); var childs = items.getElementsByTagName('item');
for(var i = 0; i < childs.length; i++){ for(var i = 0; i < childs.length; i++){
var jid = childs[i].attributes.jid.value; var jid = childs[i].attributes.jid.value;
if (ready) { if (ready) {
// abort, because we already found a service // abort, because we already found a service
return false; return false;
} }
self.queryItemForUploadService(jid); self.queryItemForUploadService(jid);
} }
}); });
}, },
/** /**
* Query item for upload service. * Query item for upload service.
* *
* @param {String} jid of the logged-in user * @param {String} jid of the logged-in user
* @param {Function} cb Callback on success * @param {Function} cb Callback on success
*/ */
queryItemForUploadService (jid) { queryItemForUploadService (jid) {
var self = this; var self = this;
console.log('query ' + jid + ' for upload service'); console.log('query ' + jid + ' for upload service');
connection.disco.info(jid, null, function(info) { connection.disco.info(jid, null, function(info) {
var httpUploadFeature; var httpUploadFeature;
var temp = info.getElementsByTagName('feature'); var temp = info.getElementsByTagName('feature');
for(var i = 0; i < temp.length; i++){ for(var i = 0; i < temp.length; i++){
var feature = temp[i].attributes.var; var feature = temp[i].attributes.var;
if(feature != undefined && feature.value === Strophe.NS.HTTPUPLOAD){ if(feature != undefined && feature.value === Strophe.NS.HTTPUPLOAD){
requestSlotUrl = jid; requestSlotUrl = jid;
ready = true; ready = true;
self.sendFile(); self.sendFile();
} }
} }
}); });
}, },
/** /**
* Saves the file the user has picked. * Saves the file the user has picked.
* *
* @param {*} file the name of the file the user has picked. * @param {*} file the name of the file the user has picked.
* @param {*} chatBox the chatbox from which the user initiated the file-upload * @param {*} chatBox the chatbox from which the user initiated the file-upload
*/ */
setFile (file1, chatBox1){ setFile (file1, chatBox1){
file = file1; file = file1;
chatBox = chatBox1; chatBox = chatBox1;
this.sendFile(); this.sendFile();
}, },
/** /**
* Upload file. * Upload file.
* Waits till the Upload-Service is discovered and till the user has picked a file. * Waits till the Upload-Service is discovered and till the user has picked a file.
* *
*/ */
sendFile () { sendFile () {
var self = this; var self = this;
if(file === undefined){ if(file === undefined){
console.log("waiting to choose a file"); console.log("waiting to choose a file");
return; return;
} }
else if(requestSlotUrl === undefined){ else if(requestSlotUrl === undefined){
console.log("waiting for service discovery"); console.log("waiting for service discovery");
return; return;
} }
console.log('Send file via http upload'); console.log('Send file via http upload');
chatBox.showHelpMessages([__('The file upload starts now')],'info'); chatBox.showHelpMessages([__('The file upload starts now')],'info');
this.requestSlot(file, function(data) { this.requestSlot(file, function(data) {
if (!data) { if (!data) {
// general error // general error
console.log('Unknown error while requesting upload slot.'); console.log('Unknown error while requesting upload slot.');
alert(__('File upload failed. Please check the log.')); alert(__('File upload failed. Please check the log.'));
} else if (data.error) { } else if (data.error) {
// specific error // specific error
console.log('The XMPP-Server return an error of the type: ' + data.error.type); console.log('The XMPP-Server return an error of the type: ' + data.error.type);
alert(__('File upload failed. Please check the log.')); alert(__('File upload failed. Please check the log.'));
} else if (data.get && data.put) { } else if (data.get && data.put) {
console.log('slot received, start upload to ' + data.put); console.log('slot received, start upload to ' + data.put);
self.uploadFile(data.put, file, function() { self.uploadFile(data.put, file, function() {
console.log(data.put); console.log(data.put);
chatBox.onMessageSubmitted(data.put, null, file); chatBox.onMessageSubmitted(data.put, null, file);
file = undefined; file = undefined;
}); });
} }
}); });
}, },
/** /**
* Request upload slot from xmpp-server * Request upload slot from xmpp-server
* *
* @param {File} file the file the user picked * @param {File} file the file the user picked
* @param {Function} cb Callback after finished request * @param {Function} cb Callback after finished request
*/ */
requestSlot (file, cb) { requestSlot (file, cb) {
var self = this; var self = this;
console.log("try sending file to: " + requestSlotUrl); console.log("try sending file to: " + requestSlotUrl);
var iq = converse.env.$iq({ var iq = converse.env.$iq({
to: requestSlotUrl, to: requestSlotUrl,
type: 'get' type: 'get'
}).c('request', { }).c('request', {
xmlns: Strophe.NS.HTTPUPLOAD xmlns: Strophe.NS.HTTPUPLOAD
}).c('filename').t(file.name) }).c('filename').t(file.name)
.up() .up()
.c('size').t(file.size); .c('size').t(file.size);
connection.sendIQ(iq, function(stanza) { connection.sendIQ(iq, function(stanza) {
self.successfulRequestSlotCB(stanza, cb); self.successfulRequestSlotCB(stanza, cb);
}, function(stanza) { }, function(stanza) {
self.failedRequestSlotCB(stanza, cb); self.failedRequestSlotCB(stanza, cb);
}); });
}, },
/** /**
* Upload the given file to the given url. * Upload the given file to the given url.
* *
* @param {String} url upload url * @param {String} url upload url
* @param {File} file the file the user picked * @param {File} file the file the user picked
* @param {Function} success_cb callback on successful transition * @param {Function} success_cb callback on successful transition
*/ */
uploadFile (url, file, success_cb) { uploadFile (url, file, success_cb) {
console.log("uploadFile start"); console.log("uploadFile start");
var xmlhttp = new XMLHttpRequest(); var xmlhttp = new XMLHttpRequest();
var type = 'PUT'; var type = 'PUT';
var contentType = 'application/octet-stream'; var contentType = 'application/octet-stream';
var data = file; var data = file;
var processData = false; var processData = false;
xmlhttp.onreadystatechange = function() { xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) { if (xmlhttp.readyState == XMLHttpRequest.DONE) {
console.log("Status: " + xmlhttp.status); console.log("Status: " + xmlhttp.status);
if (xmlhttp.status == 200 || xmlhttp.status == 201) { if (xmlhttp.status == 200 || xmlhttp.status == 201) {
console.log('file successful uploaded'); console.log('file successful uploaded');
if (success_cb) { if (success_cb) {
success_cb(); success_cb();
} }
} }
else { else {
console.log('error while uploading file to ' + url); console.log('error while uploading file to ' + url);
alert(__('Could not upload File please try again.')); alert(__('Could not upload File please try again.'));
} }
} }
}; };
xmlhttp.open(type, url, true); xmlhttp.open(type, url, true);
xmlhttp.setRequestHeader("Content-type", contentType); xmlhttp.setRequestHeader("Content-type", contentType);
xmlhttp.send(data); xmlhttp.send(data);
console.log("uploadFile end"); console.log("uploadFile end");
}, },
/** /**
* Process successful response to slot request. * Process successful response to slot request.
* *
* @param {String} stanza * @param {String} stanza
* @param {Function} cb * @param {Function} cb
*/ */
successfulRequestSlotCB (stanza, cb) { successfulRequestSlotCB (stanza, cb) {
var slot = stanza.getElementsByTagName('slot')[0]; var slot = stanza.getElementsByTagName('slot')[0];
if (slot != undefined) { if (slot != undefined) {
var put = slot.getElementsByTagName('put')[0].textContent; var put = slot.getElementsByTagName('put')[0].textContent;
var get = slot.getElementsByTagName('get')[0].textContent; var get = slot.getElementsByTagName('get')[0].textContent;
cb({ cb({
put: put, put: put,
get: get get: get
}); });
} else { } else {
this.failedRequestSlotCB(stanza, cb); this.failedRequestSlotCB(stanza, cb);
} }
}, },
/** /**
* Process failed response to slot request. * Process failed response to slot request.
* *
* @param {String} stanza * @param {String} stanza
* @param {Function} cb * @param {Function} cb
*/ */
failedRequestSlotCB (stanza, cb) { failedRequestSlotCB (stanza, cb) {
chatBox.showHelpMessages([__('Fileupload failed')],'info'); chatBox.showHelpMessages([__('Fileupload failed')],'info');
} }
}) })
} }
}); });
return converse; return converse;
})); }));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment