|
|
|
@ -54,7 +54,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -54,7 +54,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (cache[dcID] !== undefined) { |
|
|
|
|
return $q.when(cache[dcID]); |
|
|
|
|
return {then: function (cb) { |
|
|
|
|
cb(cache[dcID]); |
|
|
|
|
}}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var akk = 'dc' + dcID + '_auth_key', |
|
|
|
@ -120,24 +122,17 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -120,24 +122,17 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
dcID, |
|
|
|
|
networkerPromise; |
|
|
|
|
|
|
|
|
|
if (dcID = options.dcID) { |
|
|
|
|
networkerPromise = mtpGetNetworker(dcID, options); |
|
|
|
|
} else { |
|
|
|
|
networkerPromise = Storage.get('dc').then(function (baseDcID) { |
|
|
|
|
return mtpGetNetworker(dcID = baseDcID || 2, options); |
|
|
|
|
}); |
|
|
|
|
var cachedNetworker; |
|
|
|
|
var stack = (new Error()).stack; |
|
|
|
|
if (!stack) { |
|
|
|
|
try {window.unexistingFunction();} catch (e) { |
|
|
|
|
stack = e.stack || ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var cachedNetworker, |
|
|
|
|
stack = false; |
|
|
|
|
|
|
|
|
|
networkerPromise.then(function (networker) { |
|
|
|
|
} |
|
|
|
|
var performRequest = function (networker) { |
|
|
|
|
return (cachedNetworker = networker).wrapApiCall(method, params, options).then( |
|
|
|
|
function (result) { |
|
|
|
|
deferred.resolve(result); |
|
|
|
|
// $timeout(function () {
|
|
|
|
|
// deferred.resolve(result);
|
|
|
|
|
// }, 1000);
|
|
|
|
|
}, |
|
|
|
|
function (error) { |
|
|
|
|
console.error(dT(), 'Error', error.code, error.type, baseDcID, dcID); |
|
|
|
@ -168,12 +163,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -168,12 +163,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
cachedExportPromise[dcID].then(function () { |
|
|
|
|
(cachedNetworker = networker).wrapApiCall(method, params, options).then(function (result) { |
|
|
|
|
deferred.resolve(result); |
|
|
|
|
}, function (error) { |
|
|
|
|
rejectPromise(error); |
|
|
|
|
}); |
|
|
|
|
}, function (error) { |
|
|
|
|
rejectPromise(error); |
|
|
|
|
}); |
|
|
|
|
}, rejectPromise); |
|
|
|
|
}, rejectPromise); |
|
|
|
|
} |
|
|
|
|
else if (error.code == 303) { |
|
|
|
|
var newDcID = error.type.match(/^(PHONE_MIGRATE_|NETWORK_MIGRATE_|USER_MIGRATE_)(\d+)/)[2]; |
|
|
|
@ -187,9 +178,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -187,9 +178,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
mtpGetNetworker(newDcID, options).then(function (networker) { |
|
|
|
|
networker.wrapApiCall(method, params, options).then(function (result) { |
|
|
|
|
deferred.resolve(result); |
|
|
|
|
}, function (error) { |
|
|
|
|
rejectPromise(error); |
|
|
|
|
}); |
|
|
|
|
}, rejectPromise); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -197,14 +186,14 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -197,14 +186,14 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
rejectPromise(error); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}, function (error) { |
|
|
|
|
rejectPromise(error); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (!(stack = (stack || (new Error()).stack))) { |
|
|
|
|
try {window.unexistingFunction();} catch (e) { |
|
|
|
|
stack = e.stack || ''; |
|
|
|
|
} |
|
|
|
|
if (dcID = (options.dcID || baseDcID)) { |
|
|
|
|
mtpGetNetworker(dcID, options).then(performRequest, rejectPromise); |
|
|
|
|
} else { |
|
|
|
|
Storage.get('dc').then(function (baseDcID) { |
|
|
|
|
mtpGetNetworker(dcID = baseDcID || 2, options).then(performRequest, rejectPromise); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return deferred.promise; |
|
|
|
@ -234,14 +223,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -234,14 +223,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
|
|
|
|
|
var cachedFs = false; |
|
|
|
|
var cachedFsPromise = false; |
|
|
|
|
var apiUploadPromise = $q.when(); |
|
|
|
|
var cachedSavePromises = {}; |
|
|
|
|
var cachedDownloadPromises = {}; |
|
|
|
|
var cachedDownloads = {}; |
|
|
|
|
|
|
|
|
|
var downloadPulls = {}; |
|
|
|
|
var downloadActives = {}; |
|
|
|
|
var downloadLimit = 5; |
|
|
|
|
|
|
|
|
|
function downloadRequest(dcID, cb, activeDelta) { |
|
|
|
|
if (downloadPulls[dcID] === undefined) { |
|
|
|
@ -251,7 +238,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -251,7 +238,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
var downloadPull = downloadPulls[dcID]; |
|
|
|
|
var deferred = $q.defer(); |
|
|
|
|
downloadPull.push({cb: cb, deferred: deferred, activeDelta: activeDelta}); |
|
|
|
|
setZeroTimeout(function () { |
|
|
|
|
downloadCheck(dcID); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return deferred.promise; |
|
|
|
|
}; |
|
|
|
@ -260,6 +249,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -260,6 +249,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
|
|
|
|
|
function downloadCheck(dcID) { |
|
|
|
|
var downloadPull = downloadPulls[dcID]; |
|
|
|
|
var downloadLimit = dcID == 'upload' ? 17 : 5; |
|
|
|
|
|
|
|
|
|
if (downloadActives[dcID] >= downloadLimit || !downloadPull || !downloadPull.length) { |
|
|
|
|
return false; |
|
|
|
@ -515,14 +505,22 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -515,14 +505,22 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
|
|
|
|
|
function uploadFile (file) { |
|
|
|
|
var fileSize = file.size, |
|
|
|
|
// partSize = fileSize > 102400 ? 65536 : 4096,
|
|
|
|
|
// partSize = fileSize > 102400 ? 524288 : 4096,
|
|
|
|
|
partSize = fileSize > 102400 ? 524288 : 32768, |
|
|
|
|
isBigFile = fileSize >= 10485760, |
|
|
|
|
totalParts = Math.ceil(fileSize / partSize), |
|
|
|
|
canceled = false, |
|
|
|
|
resolved = false, |
|
|
|
|
doneParts = 0; |
|
|
|
|
doneParts = 0, |
|
|
|
|
partSize = 262144, // 256 Kb
|
|
|
|
|
activeDelta = 2; |
|
|
|
|
|
|
|
|
|
if (fileSize > 67108864) { |
|
|
|
|
partSize = 524288; |
|
|
|
|
activeDelta = 4; |
|
|
|
|
} |
|
|
|
|
else if (fileSize < 102400) { |
|
|
|
|
partSize = 32768; |
|
|
|
|
activeDelta = 1; |
|
|
|
|
} |
|
|
|
|
var totalParts = Math.ceil(fileSize / partSize); |
|
|
|
|
|
|
|
|
|
if (totalParts > 1500) { |
|
|
|
|
return $q.reject({type: 'FILE_TOO_BIG'}); |
|
|
|
@ -533,6 +531,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -533,6 +531,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
errorHandler = function (error) { |
|
|
|
|
// console.error('Up Error', error);
|
|
|
|
|
deferred.reject(error); |
|
|
|
|
canceled = true; |
|
|
|
|
errorHandler = angular.noop; |
|
|
|
|
}, |
|
|
|
|
part = 0, |
|
|
|
@ -546,35 +545,34 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -546,35 +545,34 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fileReadPromise = $q.when(); |
|
|
|
|
|
|
|
|
|
for (offset = 0; offset < fileSize; offset += partSize) { |
|
|
|
|
(function (offset, part) { |
|
|
|
|
fileReadPromise = fileReadPromise.then(function () { |
|
|
|
|
var fileReadDeferred = $q.defer(); |
|
|
|
|
downloadRequest('upload', function () { |
|
|
|
|
var uploadDeferred = $q.defer(); |
|
|
|
|
|
|
|
|
|
var reader = new FileReader(); |
|
|
|
|
var blob = file.slice(offset, offset + partSize); |
|
|
|
|
|
|
|
|
|
reader.onloadend = function (e) { |
|
|
|
|
if (canceled || e.target.readyState != FileReader.DONE) { |
|
|
|
|
if (canceled) { |
|
|
|
|
uploadDeferred.reject(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (e.target.readyState != FileReader.DONE) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
var apiCurPromise = apiUploadPromise = apiUploadPromise.then(function () { |
|
|
|
|
return MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', { |
|
|
|
|
MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', { |
|
|
|
|
file_id: fileID, |
|
|
|
|
file_part: part, |
|
|
|
|
file_total_parts: totalParts, |
|
|
|
|
bytes: e.target.result |
|
|
|
|
}, { |
|
|
|
|
startMaxLength: partSize + 256, |
|
|
|
|
fileUpload: true |
|
|
|
|
}); |
|
|
|
|
}, errorHandler); |
|
|
|
|
|
|
|
|
|
apiCurPromise.then(function (result) { |
|
|
|
|
fileUpload: true, |
|
|
|
|
singleInRequest: true |
|
|
|
|
}).then(function (result) { |
|
|
|
|
doneParts++; |
|
|
|
|
fileReadDeferred.resolve(); |
|
|
|
|
uploadDeferred.resolve(); |
|
|
|
|
if (doneParts >= totalParts) { |
|
|
|
|
deferred.resolve(resultInputFile); |
|
|
|
|
resolved = true; |
|
|
|
@ -587,8 +585,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
@@ -587,8 +585,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|
|
|
|
|
|
|
|
|
reader.readAsArrayBuffer(blob); |
|
|
|
|
|
|
|
|
|
return fileReadDeferred.promise; |
|
|
|
|
}); |
|
|
|
|
return uploadDeferred.promise; |
|
|
|
|
}, activeDelta); |
|
|
|
|
})(offset, part++); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|