Improved upload speed
Added upload multi threads Improved web workers: added onload event Fixed unauthorised box
This commit is contained in:
parent
08ab86fe86
commit
9f9827c7a6
@ -467,7 +467,6 @@ angular.module('myApp.controllers', ['myApp.i18n'])
|
|||||||
peersInDialogs = {},
|
peersInDialogs = {},
|
||||||
contactsShown;
|
contactsShown;
|
||||||
|
|
||||||
MtpApiManager.invokeApi('account.updateStatus', {offline: false});
|
|
||||||
$scope.$on('dialogs_need_more', function () {
|
$scope.$on('dialogs_need_more', function () {
|
||||||
// console.log('on need more');
|
// console.log('on need more');
|
||||||
showMoreDialogs();
|
showMoreDialogs();
|
||||||
@ -629,7 +628,8 @@ angular.module('myApp.controllers', ['myApp.i18n'])
|
|||||||
|
|
||||||
if (error.code == 401) {
|
if (error.code == 401) {
|
||||||
MtpApiManager.logOut()['finally'](function () {
|
MtpApiManager.logOut()['finally'](function () {
|
||||||
$location.url('/login');
|
location.hash = '/login';
|
||||||
|
AppRuntimeManager.reload();
|
||||||
});
|
});
|
||||||
error.handled = true;
|
error.handled = true;
|
||||||
}
|
}
|
||||||
|
@ -175,6 +175,18 @@ function convertToUint8Array(bytes) {
|
|||||||
return new Uint8Array(bytes);
|
return new Uint8Array(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function convertToByteArray(bytes) {
|
||||||
|
if (Array.isArray(bytes)) {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
bytes = convertToUint8Array(bytes);
|
||||||
|
var newBytes = [];
|
||||||
|
for (var i = 0, len = bytes.length; i < len; i++) {
|
||||||
|
newBytes.push(bytes[i]);
|
||||||
|
}
|
||||||
|
return newBytes;
|
||||||
|
}
|
||||||
|
|
||||||
function bytesFromArrayBuffer (buffer) {
|
function bytesFromArrayBuffer (buffer) {
|
||||||
var len = buffer.byteLength,
|
var len = buffer.byteLength,
|
||||||
byteView = new Uint8Array(buffer),
|
byteView = new Uint8Array(buffer),
|
||||||
|
@ -46,3 +46,5 @@ onmessage = function (e) {
|
|||||||
|
|
||||||
postMessage({taskID: taskID, result: result});
|
postMessage({taskID: taskID, result: result});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postMessage('ready');
|
||||||
|
@ -988,13 +988,32 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
|
|||||||
currentTime = tsNow(),
|
currentTime = tsNow(),
|
||||||
hasApiCall = false,
|
hasApiCall = false,
|
||||||
hasHttpWait = false,
|
hasHttpWait = false,
|
||||||
|
lengthOverflow = false,
|
||||||
|
singlesCount = 0,
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
angular.forEach(this.pendingMessages, function (value, messageID) {
|
angular.forEach(this.pendingMessages, function (value, messageID) {
|
||||||
if (!value || value >= currentTime) {
|
if (!value || value >= currentTime) {
|
||||||
if (message = self.sentMessages[messageID]) {
|
if (message = self.sentMessages[messageID]) {
|
||||||
|
var messageByteLength = (message.body.byteLength || message.body.length) + 32;
|
||||||
|
if (!message.notContentRelated &&
|
||||||
|
lengthOverflow) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!message.notContentRelated &&
|
||||||
|
messagesByteLen &&
|
||||||
|
messagesByteLen + messageByteLength > 655360) { // 640 Kb
|
||||||
|
lengthOverflow = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (message.singleInRequest) {
|
||||||
|
singlesCount++;
|
||||||
|
if (singlesCount > 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
messagesByteLen += (message.body.byteLength || message.body.length) + 32;
|
messagesByteLen += messageByteLength;
|
||||||
if (message.isAPI) {
|
if (message.isAPI) {
|
||||||
hasApiCall = true;
|
hasApiCall = true;
|
||||||
}
|
}
|
||||||
@ -1111,6 +1130,10 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
|
|||||||
|
|
||||||
self.toggleOffline(true);
|
self.toggleOffline(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (lengthOverflow || singlesCount > 1) {
|
||||||
|
this.sheduleRequest()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
MtpNetworker.prototype.getEncryptedMessage = function (bytes) {
|
MtpNetworker.prototype.getEncryptedMessage = function (bytes) {
|
||||||
|
@ -234,14 +234,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
|
|
||||||
var cachedFs = false;
|
var cachedFs = false;
|
||||||
var cachedFsPromise = false;
|
var cachedFsPromise = false;
|
||||||
var apiUploadPromise = $q.when();
|
|
||||||
var cachedSavePromises = {};
|
var cachedSavePromises = {};
|
||||||
var cachedDownloadPromises = {};
|
var cachedDownloadPromises = {};
|
||||||
var cachedDownloads = {};
|
var cachedDownloads = {};
|
||||||
|
|
||||||
var downloadPulls = {};
|
var downloadPulls = {};
|
||||||
var downloadActives = {};
|
var downloadActives = {};
|
||||||
var downloadLimit = 5;
|
|
||||||
|
|
||||||
function downloadRequest(dcID, cb, activeDelta) {
|
function downloadRequest(dcID, cb, activeDelta) {
|
||||||
if (downloadPulls[dcID] === undefined) {
|
if (downloadPulls[dcID] === undefined) {
|
||||||
@ -251,7 +249,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
var downloadPull = downloadPulls[dcID];
|
var downloadPull = downloadPulls[dcID];
|
||||||
var deferred = $q.defer();
|
var deferred = $q.defer();
|
||||||
downloadPull.push({cb: cb, deferred: deferred, activeDelta: activeDelta});
|
downloadPull.push({cb: cb, deferred: deferred, activeDelta: activeDelta});
|
||||||
downloadCheck(dcID);
|
setZeroTimeout(function () {
|
||||||
|
downloadCheck(dcID);
|
||||||
|
});
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
@ -260,6 +260,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
|
|
||||||
function downloadCheck(dcID) {
|
function downloadCheck(dcID) {
|
||||||
var downloadPull = downloadPulls[dcID];
|
var downloadPull = downloadPulls[dcID];
|
||||||
|
var downloadLimit = dcID == 'upload' ? 17 : 5;
|
||||||
|
|
||||||
if (downloadActives[dcID] >= downloadLimit || !downloadPull || !downloadPull.length) {
|
if (downloadActives[dcID] >= downloadLimit || !downloadPull || !downloadPull.length) {
|
||||||
return false;
|
return false;
|
||||||
@ -507,15 +508,23 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
}
|
}
|
||||||
|
|
||||||
function uploadFile (file) {
|
function uploadFile (file) {
|
||||||
var fileSize = file.size,
|
var fileSize = file.size,
|
||||||
// partSize = fileSize > 102400 ? 65536 : 4096,
|
isBigFile = fileSize >= 10485760,
|
||||||
// partSize = fileSize > 102400 ? 524288 : 4096,
|
canceled = false,
|
||||||
partSize = fileSize > 102400 ? 524288 : 32768,
|
resolved = false,
|
||||||
isBigFile = fileSize >= 10485760,
|
doneParts = 0,
|
||||||
totalParts = Math.ceil(fileSize / partSize),
|
partSize = 262144, // 256 Kb
|
||||||
canceled = false,
|
activeDelta = 2;
|
||||||
resolved = false,
|
|
||||||
doneParts = 0;
|
if (fileSize > 67108864) {
|
||||||
|
partSize = 524288;
|
||||||
|
activeDelta = 4;
|
||||||
|
}
|
||||||
|
else if (fileSize < 102400) {
|
||||||
|
partSize = 32768;
|
||||||
|
activeDelta = 1;
|
||||||
|
}
|
||||||
|
var totalParts = Math.ceil(fileSize / partSize);
|
||||||
|
|
||||||
if (totalParts > 1500) {
|
if (totalParts > 1500) {
|
||||||
return $q.reject({type: 'FILE_TOO_BIG'});
|
return $q.reject({type: 'FILE_TOO_BIG'});
|
||||||
@ -526,6 +535,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
errorHandler = function (error) {
|
errorHandler = function (error) {
|
||||||
// console.error('Up Error', error);
|
// console.error('Up Error', error);
|
||||||
deferred.reject(error);
|
deferred.reject(error);
|
||||||
|
canceled = true;
|
||||||
errorHandler = angular.noop;
|
errorHandler = angular.noop;
|
||||||
},
|
},
|
||||||
part = 0,
|
part = 0,
|
||||||
@ -539,35 +549,34 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var fileReadPromise = $q.when();
|
|
||||||
|
|
||||||
for (offset = 0; offset < fileSize; offset += partSize) {
|
for (offset = 0; offset < fileSize; offset += partSize) {
|
||||||
(function (offset, part) {
|
(function (offset, part) {
|
||||||
fileReadPromise = fileReadPromise.then(function () {
|
downloadRequest('upload', function () {
|
||||||
var fileReadDeferred = $q.defer();
|
var uploadDeferred = $q.defer();
|
||||||
|
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
var blob = file.slice(offset, offset + partSize);
|
var blob = file.slice(offset, offset + partSize);
|
||||||
|
|
||||||
reader.onloadend = function (e) {
|
reader.onloadend = function (e) {
|
||||||
if (canceled || e.target.readyState != FileReader.DONE) {
|
if (canceled) {
|
||||||
|
uploadDeferred.reject();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var apiCurPromise = apiUploadPromise = apiUploadPromise.then(function () {
|
if (e.target.readyState != FileReader.DONE) {
|
||||||
return MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', {
|
return;
|
||||||
file_id: fileID,
|
}
|
||||||
file_part: part,
|
MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', {
|
||||||
file_total_parts: totalParts,
|
file_id: fileID,
|
||||||
bytes: e.target.result
|
file_part: part,
|
||||||
}, {
|
file_total_parts: totalParts,
|
||||||
startMaxLength: partSize + 256,
|
bytes: e.target.result
|
||||||
fileUpload: true
|
}, {
|
||||||
});
|
startMaxLength: partSize + 256,
|
||||||
}, errorHandler);
|
fileUpload: true,
|
||||||
|
singleInRequest: true
|
||||||
apiCurPromise.then(function (result) {
|
}).then(function (result) {
|
||||||
doneParts++;
|
doneParts++;
|
||||||
fileReadDeferred.resolve();
|
uploadDeferred.resolve();
|
||||||
if (doneParts >= totalParts) {
|
if (doneParts >= totalParts) {
|
||||||
deferred.resolve(resultInputFile);
|
deferred.resolve(resultInputFile);
|
||||||
resolved = true;
|
resolved = true;
|
||||||
@ -580,8 +589,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
|
|
||||||
reader.readAsArrayBuffer(blob);
|
reader.readAsArrayBuffer(blob);
|
||||||
|
|
||||||
return fileReadDeferred.promise;
|
return uploadDeferred.promise;
|
||||||
});
|
}, activeDelta);
|
||||||
})(offset, part++);
|
})(offset, part++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,12 +484,12 @@ angular.module('izhukov.utils', [])
|
|||||||
|
|
||||||
.service('CryptoWorker', function ($timeout, $q) {
|
.service('CryptoWorker', function ($timeout, $q) {
|
||||||
|
|
||||||
var worker = window.Worker && new Worker('js/lib/crypto_worker.js') || false,
|
var webWorker = false,
|
||||||
|
naClEmbed = false,
|
||||||
taskID = 0,
|
taskID = 0,
|
||||||
awaiting = {},
|
awaiting = {},
|
||||||
webCrypto = window.crypto && (window.crypto.subtle || window.crypto.webkitSubtle) || window.msCrypto && window.msCrypto.subtle,
|
webCrypto = window.crypto && (window.crypto.subtle || window.crypto.webkitSubtle) || window.msCrypto && window.msCrypto.subtle,
|
||||||
useSha1Crypto = webCrypto && webCrypto.digest !== undefined,
|
useSha1Crypto = webCrypto && webCrypto.digest !== undefined,
|
||||||
aesNaClEmbed = false,
|
|
||||||
finalizeTask = function (taskID, result) {
|
finalizeTask = function (taskID, result) {
|
||||||
var deferred = awaiting[taskID];
|
var deferred = awaiting[taskID];
|
||||||
if (deferred !== undefined) {
|
if (deferred !== undefined) {
|
||||||
@ -502,7 +502,7 @@ angular.module('izhukov.utils', [])
|
|||||||
if (navigator.mimeTypes['application/x-pnacl'] !== undefined) {
|
if (navigator.mimeTypes['application/x-pnacl'] !== undefined) {
|
||||||
var listener = $('<div id="nacl_listener"><embed id="mtproto_crypto" width="0" height="0" src="nacl/mtproto_crypto.nmf?'+Math.random()+'" type="application/x-pnacl" /></div>').appendTo($('body'))[0];
|
var listener = $('<div id="nacl_listener"><embed id="mtproto_crypto" width="0" height="0" src="nacl/mtproto_crypto.nmf?'+Math.random()+'" type="application/x-pnacl" /></div>').appendTo($('body'))[0];
|
||||||
listener.addEventListener('load', function (e) {
|
listener.addEventListener('load', function (e) {
|
||||||
aesNaClEmbed = listener.firstChild;
|
naClEmbed = listener.firstChild;
|
||||||
console.log(dT(), 'NaCl ready');
|
console.log(dT(), 'NaCl ready');
|
||||||
}, true);
|
}, true);
|
||||||
listener.addEventListener('message', function (e) {
|
listener.addEventListener('message', function (e) {
|
||||||
@ -513,13 +513,18 @@ angular.module('izhukov.utils', [])
|
|||||||
}, true);
|
}, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (worker) {
|
if (window.Worker) {
|
||||||
worker.onmessage = function (e) {
|
var tmpWorker = new Worker('js/lib/crypto_worker.js');
|
||||||
finalizeTask(e.data.taskID, e.data.result);
|
tmpWorker.onmessage = function (e) {
|
||||||
|
if (!webWorker) {
|
||||||
|
webWorker = tmpWorker;
|
||||||
|
} else {
|
||||||
|
finalizeTask(e.data.taskID, e.data.result);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
worker.onerror = function(error) {
|
tmpWorker.onerror = function(error) {
|
||||||
console.error('CW error', error, error.stack);
|
console.error('CW error', error, error.stack);
|
||||||
worker = false;
|
webWorker = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,7 +536,7 @@ angular.module('izhukov.utils', [])
|
|||||||
|
|
||||||
params.task = task;
|
params.task = task;
|
||||||
params.taskID = taskID;
|
params.taskID = taskID;
|
||||||
(embed || worker).postMessage(params);
|
(embed || webWorker).postMessage(params);
|
||||||
|
|
||||||
taskID++;
|
taskID++;
|
||||||
|
|
||||||
@ -561,34 +566,35 @@ angular.module('izhukov.utils', [])
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
aesEncrypt: function (bytes, keyBytes, ivBytes) {
|
aesEncrypt: function (bytes, keyBytes, ivBytes) {
|
||||||
if (aesNaClEmbed) {
|
if (naClEmbed) {
|
||||||
return performTaskWorker('aes-encrypt', {
|
return performTaskWorker('aes-encrypt', {
|
||||||
bytes: addPadding(convertToArrayBuffer(bytes)),
|
bytes: addPadding(convertToArrayBuffer(bytes)),
|
||||||
keyBytes: convertToArrayBuffer(keyBytes),
|
keyBytes: convertToArrayBuffer(keyBytes),
|
||||||
ivBytes: convertToArrayBuffer(ivBytes)
|
ivBytes: convertToArrayBuffer(ivBytes)
|
||||||
}, aesNaClEmbed);
|
}, naClEmbed);
|
||||||
}
|
}
|
||||||
return $timeout(function () {
|
return $timeout(function () {
|
||||||
return convertToArrayBuffer(aesEncryptSync(bytes, keyBytes, ivBytes));
|
return convertToArrayBuffer(aesEncryptSync(bytes, keyBytes, ivBytes));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
aesDecrypt: function (encryptedBytes, keyBytes, ivBytes) {
|
aesDecrypt: function (encryptedBytes, keyBytes, ivBytes) {
|
||||||
if (aesNaClEmbed) {
|
if (naClEmbed) {
|
||||||
return performTaskWorker('aes-decrypt', {
|
return performTaskWorker('aes-decrypt', {
|
||||||
encryptedBytes: addPadding(convertToArrayBuffer(encryptedBytes)),
|
encryptedBytes: addPadding(convertToArrayBuffer(encryptedBytes)),
|
||||||
keyBytes: convertToArrayBuffer(keyBytes),
|
keyBytes: convertToArrayBuffer(keyBytes),
|
||||||
ivBytes: convertToArrayBuffer(ivBytes)
|
ivBytes: convertToArrayBuffer(ivBytes)
|
||||||
}, aesNaClEmbed);
|
}, naClEmbed);
|
||||||
}
|
}
|
||||||
return $timeout(function () {
|
return $timeout(function () {
|
||||||
return convertToArrayBuffer(aesDecryptSync(encryptedBytes, keyBytes, ivBytes));
|
return convertToArrayBuffer(aesDecryptSync(encryptedBytes, keyBytes, ivBytes));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
factorize: function (bytes) {
|
factorize: function (bytes) {
|
||||||
if (aesNaClEmbed && bytes.length <= 8) {
|
bytes = convertToByteArray(bytes);
|
||||||
return performTaskWorker('factorize', {bytes: bytes}, aesNaClEmbed);
|
if (naClEmbed && bytes.length <= 8) {
|
||||||
|
return performTaskWorker('factorize', {bytes: bytes}, naClEmbed);
|
||||||
}
|
}
|
||||||
if (worker) {
|
if (webWorker) {
|
||||||
return performTaskWorker('factorize', {bytes: bytes});
|
return performTaskWorker('factorize', {bytes: bytes});
|
||||||
}
|
}
|
||||||
return $timeout(function () {
|
return $timeout(function () {
|
||||||
@ -596,7 +602,7 @@ angular.module('izhukov.utils', [])
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
modPow: function (x, y, m) {
|
modPow: function (x, y, m) {
|
||||||
if (worker) {
|
if (webWorker) {
|
||||||
return performTaskWorker('mod-pow', {
|
return performTaskWorker('mod-pow', {
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
|
@ -850,7 +850,7 @@ angular.module('myApp.services', ['myApp.i18n'])
|
|||||||
offset: offset || 0,
|
offset: offset || 0,
|
||||||
limit: limit || 0,
|
limit: limit || 0,
|
||||||
max_id: maxID || 0
|
max_id: maxID || 0
|
||||||
}).then(function (historyResult) {
|
}, {noErrorBox: true}).then(function (historyResult) {
|
||||||
AppUsersManager.saveApiUsers(historyResult.users);
|
AppUsersManager.saveApiUsers(historyResult.users);
|
||||||
AppChatsManager.saveApiChats(historyResult.chats);
|
AppChatsManager.saveApiChats(historyResult.chats);
|
||||||
saveMessages(historyResult.messages);
|
saveMessages(historyResult.messages);
|
||||||
@ -3130,7 +3130,7 @@ angular.module('myApp.services', ['myApp.i18n'])
|
|||||||
|
|
||||||
function attach () {
|
function attach () {
|
||||||
MtpNetworkerFactory.setUpdatesProcessor(processUpdateMessage);
|
MtpNetworkerFactory.setUpdatesProcessor(processUpdateMessage);
|
||||||
MtpApiManager.invokeApi('updates.getState', {noErrorBox: true}).then(function (stateResult) {
|
MtpApiManager.invokeApi('updates.getState', {}, {noErrorBox: true}).then(function (stateResult) {
|
||||||
curState.seq = stateResult.seq;
|
curState.seq = stateResult.seq;
|
||||||
curState.pts = stateResult.pts;
|
curState.pts = stateResult.pts;
|
||||||
curState.date = stateResult.date;
|
curState.date = stateResult.date;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user