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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: jsxc.lib.fileTransfer.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<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.fileTransfer.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @namespace jsxc.fileTransfer
* @type {Object}
*/
jsxc.fileTransfer = {};
/**
* Make bytes more human readable.
*
* @memberOf jsxc.fileTransfer
* @param {Integer} byte
* @return {String}
*/
jsxc.fileTransfer.formatByte = function(byte) {
var s = ['', 'KB', 'MB', 'GB', 'TB'];
var i;
for (i = 1; i < s.length; i++) {
if (byte < 1024) {
break;
}
byte /= 1024;
}
return (Math.round(byte * 10) / 10) + s[i - 1];
};
/**
* Start file transfer dialog.
*
* @memberOf jsxc.fileTransfer
* @param {String} jid
*/
jsxc.fileTransfer.startGuiAction = function(jid) {
var bid = jsxc.jidToBid(jid);
var res = Strophe.getResourceFromJid(jid);
if (!res && !jsxc.xmpp.httpUpload.ready) {
if (jsxc.fileTransfer.isWebrtcCapable(bid)) {
jsxc.fileTransfer.selectResource(bid, jsxc.fileTransfer.startGuiAction);
} else {
jsxc.gui.window.postMessage({
bid: bid,
direction: jsxc.Message.SYS,
msg: $.t('No_proper_file_transfer_method_available')
});
}
return;
}
jsxc.fileTransfer.showFileSelection(jid);
};
/**
* Show select dialog for file transfer capable resources.
*
* @memberOf jsxc.fileTransfer
* @param {String} bid
* @param {Function} success_cb Called if user selects resource
* @param {Function} error_cb Called if no resource was found or selected
*/
jsxc.fileTransfer.selectResource = function(bid, success_cb, error_cb) {
var win = jsxc.gui.window.get(bid);
var jid = win.data('jid');
var res = Strophe.getResourceFromJid(jid);
var fileCapableRes = jsxc.webrtc.getCapableRes(jid, jsxc.webrtc.reqFileFeatures);
var resources = Object.keys(jsxc.storage.getUserItem('res', bid)) || [];
if (res === null && resources.length === 1 && fileCapableRes.length === 1) {
// only one resource is available and this resource is also capable to receive files
res = fileCapableRes[0];
jid = bid + '/' + res;
success_cb(jid);
} else if (fileCapableRes.indexOf(res) >= 0) {
// currently used resource is capable to receive files
success_cb(bid + '/' + res);
} else if (fileCapableRes.indexOf(res) < 0) {
// show selection dialog
jsxc.gui.window.selectResource(bid, $.t('Your_contact_uses_multiple_clients_'), function(data) {
if (data.status === 'unavailable') {
jsxc.gui.window.hideOverlay(bid);
if (typeof error_cb === 'function') {
error_cb();
}
} else if (data.status === 'selected') {
success_cb(bid + '/' + data.result);
}
}, fileCapableRes);
}
};
/**
* Show file selector.
*
* @memberOf jsxc.fileTransfer
* @param {String} jid
*/
jsxc.fileTransfer.showFileSelection = function(jid) {
var bid = jsxc.jidToBid(jid);
var msg = $('<div><div><label><input type="file" name="files" /><label></div></div>');
msg.addClass('jsxc_chatmessage');
jsxc.gui.window.showOverlay(bid, msg, true);
// open file selection for user
msg.find('label').click();
msg.find('[type="file"]').change(function(ev) {
var file = ev.target.files[0]; // FileList object
if (!file) {
return;
}
jsxc.fileTransfer.fileSelected(jid, msg, file);
});
};
jsxc.fileTransfer.showFileTooLarge = function(bid, file) {
var maxSize = jsxc.fileTransfer.formatByte(jsxc.options.get('httpUpload').maxSize);
var fileSize = jsxc.fileTransfer.formatByte(file.size);
jsxc.gui.window.postMessage({
bid: bid,
direction: jsxc.Message.SYS,
msg: $.t('File_too_large') + ' (' + fileSize + ' > ' + maxSize + ')'
});
jsxc.gui.window.hideOverlay(bid);
};
/**
* Callback for file selector.
*
* @memberOf jsxc.fileTransfer
* @param {String} jid
* @param {jQuery} msg jQuery object of temporary file message
* @param {File} file selected file
*/
jsxc.fileTransfer.fileSelected = function(jid, msg, file) {
var bid = jsxc.jidToBid(jid);
var httpUploadOptions = jsxc.options.get('httpUpload') || {};
var maxSize = httpUploadOptions.maxSize || -1;
if (file.transportMethod !== 'webrtc' && jsxc.xmpp.httpUpload.ready && maxSize >= 0 && file.size > maxSize) {
jsxc.debug('File too large for http upload.');
if (jsxc.fileTransfer.isWebrtcCapable(bid)) {
// try data channels
file.transportMethod = 'webrtc';
jsxc.fileTransfer.selectResource(bid, function(jid) {
jsxc.fileTransfer.fileSelected(jid, msg, file);
}, function() {
jsxc.fileTransfer.showFileTooLarge(bid, file);
});
} else {
jsxc.fileTransfer.showFileTooLarge(bid, file);
}
return;
} else if (!jsxc.xmpp.httpUpload.ready && Strophe.getResourceFromJid(jid)) {
// http upload not available
file.transportMethod = 'webrtc';
}
var attachment = $('<div>');
attachment.addClass('jsxc_attachment');
attachment.addClass('jsxc_' + file.type.replace(/\//, '-'));
attachment.addClass('jsxc_' + file.type.replace(/^([^/]+)\/.*/, '$1'));
msg.empty().append(attachment);
if (FileReader && file.type.match(/^image\//)) {
// show image preview
var img = $('<img alt="preview">').attr('title', file.name);
img.attr('src', jsxc.options.get('root') + '/img/loading.gif');
img.appendTo(attachment);
var reader = new FileReader();
reader.onload = function() {
img.attr('src', reader.result);
};
reader.readAsDataURL(file);
} else {
attachment.text(file.name + ' (' + file.size + ' byte)');
}
$('<button>').addClass('jsxc_btn jsxc_btn-primary').text($.t('Send')).click(function() {
// user confirmed file transfer
jsxc.gui.window.hideOverlay(bid);
msg.remove();
var message = jsxc.gui.window.postMessage({
bid: bid,
direction: jsxc.Message.OUT,
attachment: {
name: file.name,
size: file.size,
type: file.type,
data: (file.type.match(/^image\//)) ? img.attr('src') : null
}
});
if (file.transportMethod === 'webrtc') {
var sess = jsxc.webrtc.sendFile(jid, file);
sess.sender.on('progress', function(sent, size) {
jsxc.gui.window.updateProgress(message, sent, size);
if (sent === size) {
message.received();
}
});
} else {
// progress is updated in xmpp.httpUpload.uploadFile
jsxc.xmpp.httpUpload.sendFile(file, message);
}
}).appendTo(msg);
$('<button>').addClass('jsxc_btn jsxc_btn-default').text($.t('Abort')).click(function() {
// user aborted file transfer
jsxc.gui.window.hideOverlay(bid);
}).appendTo(msg);
};
/**
* Enable/disable icons for file transfer.
*
* @memberOf jsxc.fileTransfer
* @param {String} bid
*/
jsxc.fileTransfer.updateIcons = function(bid) {
var win = jsxc.gui.window.get(bid);
if (!win || win.length === 0 || !jsxc.xmpp.conn) {
return;
}
jsxc.debug('Update file transfer icons for ' + bid);
if (jsxc.xmpp.httpUpload.ready) {
win.find('.jsxc_sendFile').removeClass('jsxc_disabled');
return;
} else if (!jsxc.fileTransfer.isWebrtcCapable(bid)) {
win.find('.jsxc_sendFile').addClass('jsxc_disabled');
return;
}
var jid = win.data('jid');
var res = Strophe.getResourceFromJid(jid);
var fileCapableRes = jsxc.webrtc.getCapableRes(bid, jsxc.webrtc.reqFileFeatures);
var resources = Object.keys(jsxc.storage.getUserItem('res', bid) || {}) || [];
if (fileCapableRes.indexOf(res) > -1 || (res === null && fileCapableRes.length === 1 && resources.length === 1)) {
win.find('.jsxc_sendFile').removeClass('jsxc_disabled');
} else {
win.find('.jsxc_sendFile').addClass('jsxc_disabled');
}
};
jsxc.fileTransfer.isWebrtcCapable = function(bid) {
return !jsxc.muc.isGroupchat(bid);
};
$(document).on('update.gui.jsxc', function(ev, bid) {
jsxc.fileTransfer.updateIcons(bid);
});
</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>
|