|
|
@ -54,7 +54,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (cache[dcID] !== undefined) { |
|
|
|
if (cache[dcID] !== undefined) { |
|
|
|
return $q.when(cache[dcID]); |
|
|
|
return {then: function (cb) { |
|
|
|
|
|
|
|
cb(cache[dcID]); |
|
|
|
|
|
|
|
}}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var akk = 'dc' + dcID + '_auth_key', |
|
|
|
var akk = 'dc' + dcID + '_auth_key', |
|
|
@ -120,24 +122,17 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) |
|
|
|
dcID, |
|
|
|
dcID, |
|
|
|
networkerPromise; |
|
|
|
networkerPromise; |
|
|
|
|
|
|
|
|
|
|
|
if (dcID = options.dcID) { |
|
|
|
var cachedNetworker; |
|
|
|
networkerPromise = mtpGetNetworker(dcID, options); |
|
|
|
var stack = (new Error()).stack; |
|
|
|
} else { |
|
|
|
if (!stack) { |
|
|
|
networkerPromise = Storage.get('dc').then(function (baseDcID) { |
|
|
|
try {window.unexistingFunction();} catch (e) { |
|
|
|
return mtpGetNetworker(dcID = baseDcID || 2, options); |
|
|
|
stack = e.stack || ''; |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
var cachedNetworker, |
|
|
|
var performRequest = function (networker) { |
|
|
|
stack = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
networkerPromise.then(function (networker) { |
|
|
|
|
|
|
|
return (cachedNetworker = networker).wrapApiCall(method, params, options).then( |
|
|
|
return (cachedNetworker = networker).wrapApiCall(method, params, options).then( |
|
|
|
function (result) { |
|
|
|
function (result) { |
|
|
|
deferred.resolve(result); |
|
|
|
deferred.resolve(result); |
|
|
|
// $timeout(function () {
|
|
|
|
|
|
|
|
// deferred.resolve(result);
|
|
|
|
|
|
|
|
// }, 1000);
|
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
function (error) { |
|
|
|
function (error) { |
|
|
|
console.error(dT(), 'Error', error.code, error.type, baseDcID, dcID); |
|
|
|
console.error(dT(), 'Error', error.code, error.type, baseDcID, dcID); |
|
|
@ -168,12 +163,8 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) |
|
|
|
cachedExportPromise[dcID].then(function () { |
|
|
|
cachedExportPromise[dcID].then(function () { |
|
|
|
(cachedNetworker = networker).wrapApiCall(method, params, options).then(function (result) { |
|
|
|
(cachedNetworker = networker).wrapApiCall(method, params, options).then(function (result) { |
|
|
|
deferred.resolve(result); |
|
|
|
deferred.resolve(result); |
|
|
|
}, function (error) { |
|
|
|
}, rejectPromise); |
|
|
|
rejectPromise(error); |
|
|
|
}, rejectPromise); |
|
|
|
}); |
|
|
|
|
|
|
|
}, function (error) { |
|
|
|
|
|
|
|
rejectPromise(error); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else if (error.code == 303) { |
|
|
|
else if (error.code == 303) { |
|
|
|
var newDcID = error.type.match(/^(PHONE_MIGRATE_|NETWORK_MIGRATE_|USER_MIGRATE_)(\d+)/)[2]; |
|
|
|
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']) |
|
|
|
mtpGetNetworker(newDcID, options).then(function (networker) { |
|
|
|
mtpGetNetworker(newDcID, options).then(function (networker) { |
|
|
|
networker.wrapApiCall(method, params, options).then(function (result) { |
|
|
|
networker.wrapApiCall(method, params, options).then(function (result) { |
|
|
|
deferred.resolve(result); |
|
|
|
deferred.resolve(result); |
|
|
|
}, function (error) { |
|
|
|
}, rejectPromise); |
|
|
|
rejectPromise(error); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -197,14 +186,14 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) |
|
|
|
rejectPromise(error); |
|
|
|
rejectPromise(error); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, function (error) { |
|
|
|
}; |
|
|
|
rejectPromise(error); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(stack = (stack || (new Error()).stack))) { |
|
|
|
if (dcID = (options.dcID || baseDcID)) { |
|
|
|
try {window.unexistingFunction();} catch (e) { |
|
|
|
mtpGetNetworker(dcID, options).then(performRequest, rejectPromise); |
|
|
|
stack = e.stack || ''; |
|
|
|
} else { |
|
|
|
} |
|
|
|
Storage.get('dc').then(function (baseDcID) { |
|
|
|
|
|
|
|
mtpGetNetworker(dcID = baseDcID || 2, options).then(performRequest, rejectPromise); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return deferred.promise; |
|
|
|
return deferred.promise; |
|
|
@ -234,14 +223,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 +238,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}); |
|
|
|
|
|
|
|
setZeroTimeout(function () { |
|
|
|
downloadCheck(dcID); |
|
|
|
downloadCheck(dcID); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return deferred.promise; |
|
|
|
return deferred.promise; |
|
|
|
}; |
|
|
|
}; |
|
|
@ -260,6 +249,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; |
|
|
@ -515,14 +505,22 @@ 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,
|
|
|
|
|
|
|
|
// partSize = fileSize > 102400 ? 524288 : 4096,
|
|
|
|
|
|
|
|
partSize = fileSize > 102400 ? 524288 : 32768, |
|
|
|
|
|
|
|
isBigFile = fileSize >= 10485760, |
|
|
|
isBigFile = fileSize >= 10485760, |
|
|
|
totalParts = Math.ceil(fileSize / partSize), |
|
|
|
|
|
|
|
canceled = false, |
|
|
|
canceled = false, |
|
|
|
resolved = 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) { |
|
|
|
if (totalParts > 1500) { |
|
|
|
return $q.reject({type: 'FILE_TOO_BIG'}); |
|
|
|
return $q.reject({type: 'FILE_TOO_BIG'}); |
|
|
@ -533,6 +531,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, |
|
|
@ -546,35 +545,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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (e.target.readyState != FileReader.DONE) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
var apiCurPromise = apiUploadPromise = apiUploadPromise.then(function () { |
|
|
|
MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', { |
|
|
|
return MtpApiManager.invokeApi(isBigFile ? 'upload.saveBigFilePart' : 'upload.saveFilePart', { |
|
|
|
|
|
|
|
file_id: fileID, |
|
|
|
file_id: fileID, |
|
|
|
file_part: part, |
|
|
|
file_part: part, |
|
|
|
file_total_parts: totalParts, |
|
|
|
file_total_parts: totalParts, |
|
|
|
bytes: e.target.result |
|
|
|
bytes: e.target.result |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
startMaxLength: partSize + 256, |
|
|
|
startMaxLength: partSize + 256, |
|
|
|
fileUpload: true |
|
|
|
fileUpload: true, |
|
|
|
}); |
|
|
|
singleInRequest: true |
|
|
|
}, errorHandler); |
|
|
|
}).then(function (result) { |
|
|
|
|
|
|
|
|
|
|
|
apiCurPromise.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; |
|
|
@ -587,8 +585,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++); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|