web/lib/javascripts/transfer.js in vines-0.2.1 vs web/lib/javascripts/transfer.js in vines-0.3.0

- old
+ new

@@ -14,14 +14,14 @@ this.sid = this.session.uniqueId(); this.seq = 0; this.sent = 0; } Transfer.prototype.start = function() { - var callback, node; + var node; node = $("<iq id=\"" + (this.session.uniqueId()) + "\" to=\"" + this.to + "\" type=\"set\">\n <si xmlns=\"http://jabber.org/protocol/si\" id=\"" + (this.session.uniqueId()) + "\" profile=\"http://jabber.org/protocol/si/profile/file-transfer\">\n <file xmlns=\"http://jabber.org/protocol/si/profile/file-transfer\" name=\"\" size=\"" + this.file.size + "\"/>\n <feature xmlns=\"http://jabber.org/protocol/feature-neg\">\n <x xmlns=\"jabber:x:data\" type=\"form\">\n <field var=\"stream-method\" type=\"list-single\">\n <option><value>http://jabber.org/protocol/ibb</value></option>\n </field>\n </x>\n </feature>\n </si>\n</iq>"); $('file', node).attr('name', this.file.name); - callback = __bind(function(result) { + return this.session.sendIQ(node.get(0), __bind(function(result) { var m, methods, ok; methods = $('si feature x field[var="stream-method"] value', result); ok = ((function() { var _i, _len, _results; _results = []; @@ -34,49 +34,46 @@ return _results; })()).length > 0; if (ok) { return this.open(); } - }, this); - return this.session.sendIQ(node.get(0), callback); + }, this)); }; Transfer.prototype.open = function() { - var callback, node; + var node; node = $("<iq id=\"" + (this.session.uniqueId()) + "\" to=\"" + this.to + "\" type=\"set\">\n <open xmlns=\"http://jabber.org/protocol/ibb\" sid=\"" + this.sid + "\" block-size=\"4096\"/>\n</iq>"); - callback = __bind(function(result) { + return this.session.sendIQ(node.get(0), __bind(function(result) { if (this.ok(result)) { this.opened = true; - return this.chunks.start(__bind(function() { - return this.sendChunk(); - }, this)); + return this.sendChunk(); } - }, this); - return this.session.sendIQ(node.get(0), callback); + }, this)); }; Transfer.prototype.sendChunk = function() { - var callback, chunk, node; if (this.closed) { return; } - if (!(chunk = this.chunks.chunk())) { - this.close(); - return; - } - node = $("<iq id=\"" + (this.session.uniqueId()) + "\" to=\"" + this.to + "\" type=\"set\">\n <data xmlns=\"http://jabber.org/protocol/ibb\" sid=\"" + this.sid + "\" seq=\"" + (this.seq++) + "\">" + chunk + "</data>\n</iq>"); - if (this.seq > 65535) { - this.seq = 0; - } - callback = __bind(function(result) { - var pct; - if (!this.ok(result)) { + return this.chunks.chunk(__bind(function(chunk) { + var node; + if (!chunk) { + this.close(); return; } - pct = Math.ceil(++this.sent / this.chunks.total * 100); - this.progress(pct); - return this.sendChunk(); - }, this); - return this.session.sendIQ(node.get(0), callback); + node = $("<iq id=\"" + (this.session.uniqueId()) + "\" to=\"" + this.to + "\" type=\"set\">\n <data xmlns=\"http://jabber.org/protocol/ibb\" sid=\"" + this.sid + "\" seq=\"" + (this.seq++) + "\">" + chunk + "</data>\n</iq>"); + if (this.seq > 65535) { + this.seq = 0; + } + return this.session.sendIQ(node.get(0), __bind(function(result) { + var pct; + if (!this.ok(result)) { + return; + } + pct = Math.ceil(++this.sent / this.chunks.total * 100); + this.progress(pct); + return this.sendChunk(); + }, this)); + }, this)); }; Transfer.prototype.close = function() { var node; if (this.closed) { return; @@ -95,39 +92,32 @@ }; Transfer.prototype.ok = function(result) { return $(result).attr('type') === 'result'; }; Chunks = (function() { + var CHUNK_SIZE; + CHUNK_SIZE = 3 / 4 * 4096; function Chunks(file) { this.file = file; - this.chunks = []; - this.total = 0; + this.total = Math.ceil(this.file.size / CHUNK_SIZE); + this.slice = this.file.slice || this.file.webkitSlice || this.file.mozSlice; + this.pos = 0; } - Chunks.prototype.chunk = function() { - return this.chunks.shift(); - }; - Chunks.prototype.start = function(callback) { - var reader; - reader = new FileReader(); - reader.onload = __bind(function(event) { - var chunk, data; - data = btoa(event.target.result); - this.chunks = (function() { - var _i, _len, _ref, _results; - _ref = data.split(/(.{1,4096})/); - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - chunk = _ref[_i]; - if (chunk) { - _results.push(chunk); - } - } - return _results; - })(); - this.total = this.chunks.length; - return callback(); - }, this); - return reader.readAsBinaryString(this.file); + Chunks.prototype.chunk = function(callback) { + var chunk, end, reader, start; + start = this.pos; + end = this.pos + CHUNK_SIZE; + this.pos = end; + if (start > this.file.size) { + return callback(null); + } else { + chunk = this.slice.call(this.file, start, end); + reader = new FileReader(); + reader.onload = function(event) { + return callback(btoa(event.target.result)); + }; + return reader.readAsBinaryString(chunk); + } }; return Chunks; })(); return Transfer; })(); \ No newline at end of file