1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: jsxc.lib.xmpp.httpUpload.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: jsxc.lib.xmpp.httpUpload.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Implements Http File Upload (XEP-0363)
*
* @namespace jsxc.xmpp.httpUpload
* @see {@link http://xmpp.org/extensions/xep-0363.html}
*/
jsxc.xmpp.httpUpload = {
conn: null,
ready: false,
CONST: {
NS: {
HTTPUPLOAD: 'urn:xmpp:http:upload'
}
}
};
/**
* Set up http file upload.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {Object} o options
*/
jsxc.xmpp.httpUpload.init = function(o) {
var self = jsxc.xmpp.httpUpload;
self.conn = jsxc.xmpp.conn;
var fileTransferOptions = jsxc.options.get('fileTransfer') || {};
var options = o || jsxc.options.get('httpUpload');
if (!fileTransferOptions.httpUpload.enable) {
jsxc.debug('http upload disabled');
jsxc.options.set('httpUpload', false);
return;
}
if (options && options.server) {
self.ready = true;
return;
}
if (!jsxc.xmpp.conn) {
return;
}
var caps = jsxc.xmpp.conn.caps;
var domain = jsxc.xmpp.conn.domain;
if (!caps || !domain || typeof caps._knownCapabilities[caps._jidVerIndex[domain]] === 'undefined') {
jsxc.debug('Waiting for server capabilities');
$(document).on('caps.strophe', function onCaps(ev, from) {
if (from !== domain) {
return;
}
self.init();
$(document).off('caps.strophe', onCaps);
});
return;
}
self.discoverUploadService();
};
/**
* Discover upload service for http upload.
*
* @memberOf jsxc.xmpp.httpUpload
*/
jsxc.xmpp.httpUpload.discoverUploadService = function() {
var self = jsxc.xmpp.httpUpload;
var domain = self.conn.domain;
jsxc.debug('discover http upload service');
if (jsxc.xmpp.conn.caps.hasFeatureByJid(domain, self.CONST.NS.HTTPUPLOAD)) {
self.queryItemForUploadService(domain);
}
self.conn.disco.items(domain, null, function(items) {
$(items).find('item').each(function() {
var jid = $(this).attr('jid');
if (self.ready) {
// abort, because we already found a service
return false;
}
self.queryItemForUploadService(jid);
});
});
};
/**
* Query item for upload service.
*
* @param {String} jid
* @param {Function} cb Callback on success
* @memberOf jsxc.xmpp.httpUpload
*/
jsxc.xmpp.httpUpload.queryItemForUploadService = function(jid, cb) {
var self = jsxc.xmpp.httpUpload;
jsxc.debug('query ' + jid + ' for upload service');
self.conn.disco.info(jid, null, function(info) {
var httpUploadFeature = $(info).find('feature[var="' + self.CONST.NS.HTTPUPLOAD + '"]');
var httpUploadMaxSize = $(info).find('field[var="max-file-size"]');
if (httpUploadFeature.length > 0) {
jsxc.debug('http upload service found on ' + jid);
jsxc.options.set('httpUpload', {
server: jid,
name: $(info).find('identity').attr('name'),
maxSize: parseInt(httpUploadMaxSize.text()) || -1
});
self.ready = true;
if (typeof cb === 'function') {
cb.call(info);
}
}
});
};
/**
* Upload file and send link to peer.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {File} file
* @param {Message} message Preview message
*/
jsxc.xmpp.httpUpload.sendFile = function(file, message) {
jsxc.debug('Send file via http upload');
var self = jsxc.xmpp.httpUpload;
// even if the link is encrypted the file isn't
message.encrypted = false;
self.requestSlot(file, function(data) {
if (!data) {
// general error
jsxc.warn('Unknown error occured. Please check the debug log.');
} else if (data.error) {
// specific error
jsxc.warn('The xmpp server responded with an error of the type "' + data.error.type + '"');
message.getDOM().remove();
jsxc.gui.window.postMessage({
bid: message.bid,
direction: jsxc.Message.SYS,
msg: data.error.text
});
message.delete();
} else if (data.get && data.put) {
jsxc.debug('slot received, start upload to ' + data.put);
self.uploadFile(data.put, file, message, function() {
var attachment = message.attachment;
var metaString = attachment.type + '|' + attachment.size + '|' + attachment.name;
var a = $('<a>');
a.attr('href', data.get);
attachment.data = data.get;
if (attachment.thumbnail) {
var img = $('<img>');
img.attr('alt', 'Preview:' + metaString);
img.attr('src', attachment.thumbnail);
a.prepend(img);
} else {
a.text(metaString);
}
message.msg = data.get;
message.htmlMsg = $('<span>').append(a).html();
message.type = jsxc.Message.HTML;
jsxc.gui.window.postMessage(message);
});
}
});
};
/**
* Upload the given file to the given url.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {String} url upload url
* @param {File} file
* @param {Message} message preview message
* @param {Function} success_cb callback on successful transition
*/
jsxc.xmpp.httpUpload.uploadFile = function(url, file, message, success_cb) {
$.ajax({
url: url,
type: 'PUT',
contentType: 'application/octet-stream',
data: file,
processData: false,
xhr: function() {
var xhr = $.ajaxSettings.xhr();
// track upload progress
xhr.upload.onprogress = function(ev) {
if (ev.lengthComputable) {
jsxc.gui.window.updateProgress(message, ev.loaded, ev.total);
}
};
return xhr;
},
success: function() {
jsxc.debug('file successful uploaded');
// In case that upload progress is not available, inform user
jsxc.gui.window.updateProgress(message, 1, 1);
if (success_cb) {
success_cb();
}
},
error: function() {
jsxc.warn('error while uploading file to ' + url);
message.error = 'Could not upload file';
jsxc.gui.window.postMessage(message);
}
});
};
/**
* Request upload slot.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {File} file
* @param {Function} cb Callback after finished request
*/
jsxc.xmpp.httpUpload.requestSlot = function(file, cb) {
var self = jsxc.xmpp.httpUpload;
var options = jsxc.options.get('httpUpload');
if (!options || !options.server) {
jsxc.warn('could not request upload slot, because I am not aware of a server or http upload is disabled');
return;
}
var iq = $iq({
to: options.server,
type: 'get'
}).c('request', {
xmlns: self.CONST.NS.HTTPUPLOAD
}).c('filename').t(file.name)
.up()
.c('size').t(file.size);
self.conn.sendIQ(iq, function(stanza) {
self.successfulRequestSlotCB(stanza, cb);
}, function(stanza) {
self.failedRequestSlotCB(stanza, cb);
});
};
/**
* Process successful response to slot request.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {String} stanza
* @param {Function} cb
*/
jsxc.xmpp.httpUpload.successfulRequestSlotCB = function(stanza, cb) {
var self = jsxc.xmpp.httpUpload;
var slot = $(stanza).find('slot[xmlns="' + self.CONST.NS.HTTPUPLOAD + '"]');
if (slot.length > 0) {
var put = slot.find('put').text();
var get = slot.find('get').text();
cb({
put: put,
get: get
});
} else {
self.failedRequestSlotCB(stanza, cb);
}
};
/**
* Process failed response to slot request.
*
* @memberOf jsxc.xmpp.httpUpload
* @param {String} stanza
* @param {Function} cb
*/
jsxc.xmpp.httpUpload.failedRequestSlotCB = function(stanza, cb) {
if ($(stanza).find('error').length <= 0) {
jsxc.warn('response does not contain a slot element');
cb();
return;
}
var error = {
type: $(stanza).find('error').attr('type') || 'unknown',
text: $(stanza).find('error text').text()
};
if ($(stanza).find('error not-acceptable')) {
error.reason = 'not-acceptable';
} else if ($(stanza).find('error resource-constraint')) {
error.reason = 'resource-constraint';
} else if ($(stanza).find('error not-allowed')) {
error.reason = 'not-allowed';
}
cb({
error: error
});
};
$(document).on('stateUIChange.jsxc', function(ev, state) {
if (state === jsxc.CONST.UISTATE.INITIATING) {
jsxc.xmpp.httpUpload.init();
}
});
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsxc.Message.html">Message</a></li></ul><h3>Namespaces</h3><ul><li><a href="jsxc.html">jsxc</a></li><li><a href="jsxc.fileTransfer.html">fileTransfer</a></li><li><a href="jsxc.gui.html">gui</a></li><li><a href="jsxc.gui.dialog.html">dialog</a></li><li><a href="jsxc.gui.queryActions.html">queryActions</a></li><li><a href="jsxc.gui.roster.html">roster</a></li><li><a href="jsxc.gui.window.html">window</a></li><li><a href="jsxc.muc.html">muc</a></li><li><a href="jsxc.notification.html">notification</a></li><li><a href="jsxc.options.html">options</a></li><li><a href="jsxc.otr.html">otr</a></li><li><a href="jsxc.storage.html">storage</a></li><li><a href="jsxc.tab.html">tab</a></li><li><a href="jsxc.webrtc.html">webrtc</a></li><li><a href="jsxc.xmpp.html">xmpp</a></li><li><a href="jsxc.xmpp.bookmarks.html">bookmarks</a></li><li><a href="jsxc.xmpp.carbons.html">carbons</a></li><li><a href="jsxc.xmpp.chatState.html">chatState</a></li><li><a href="jsxc.xmpp.httpUpload.html">httpUpload</a></li><li><a href="jsxc.xmpp.mam.html">mam</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri May 10 2019 12:36:11 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
|