Sequential file send

This commit is contained in:
Igor Zhukov 2014-04-08 13:51:29 +02:00
parent 376c1173ff
commit 9048fad53f

View File

@ -681,6 +681,7 @@ angular.module('myApp.services', [])
var pendingByRandomID = {}; var pendingByRandomID = {};
var pendingByMessageID = {}; var pendingByMessageID = {};
var pendingAfterMsgs = {}; var pendingAfterMsgs = {};
var sendFilePromise = $q.when();
var tempID = -1; var tempID = -1;
@ -1214,70 +1215,81 @@ angular.module('myApp.services', [])
} }
message.send = function () { message.send = function () {
var uploaded = false, var sendFileDeferred = $q.defer();
uploadPromise = MtpApiFileManager.uploadFile(file);
uploadPromise.then(function (inputFile) { sendFilePromise.then(function () {
uploaded = true; var uploaded = false,
var inputMedia; uploadPromise = MtpApiFileManager.uploadFile(file);
switch (attachType) {
case 'photo':
inputMedia = {_: 'inputMediaUploadedPhoto', file: inputFile};
break;
case 'video': uploadPromise.then(function (inputFile) {
inputMedia = {_: 'inputMediaUploadedVideo', file: inputFile, duration: 0, w: 0, h: 0}; uploaded = true;
break; var inputMedia;
switch (attachType) {
case 'photo':
inputMedia = {_: 'inputMediaUploadedPhoto', file: inputFile};
break;
case 'audio': case 'video':
inputMedia = {_: 'inputMediaUploadedAudio', file: inputFile, duration: 0}; inputMedia = {_: 'inputMediaUploadedVideo', file: inputFile, duration: 0, w: 0, h: 0};
break; break;
case 'document': case 'audio':
default: inputMedia = {_: 'inputMediaUploadedAudio', file: inputFile, duration: 0};
inputMedia = {_: 'inputMediaUploadedDocument', file: inputFile, file_name: file.name, mime_type: file.type}; break;
}
MtpApiManager.invokeApi('messages.sendMedia', {
peer: inputPeer,
media: inputMedia,
random_id: randomID
}).then(function (result) {
if (ApiUpdatesManager.saveSeq(result.seq)) {
ApiUpdatesManager.saveUpdate({
_: 'updateMessageID',
random_id: randomIDS,
id: result.message.id
});
message.date = result.message.date; case 'document':
message.id = result.message.id; default:
message.media = result.message.media; inputMedia = {_: 'inputMediaUploadedDocument', file: inputFile, file_name: file.name, mime_type: file.type};
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage',
message: message,
pts: result.pts
});
} }
MtpApiManager.invokeApi('messages.sendMedia', {
peer: inputPeer,
media: inputMedia,
random_id: randomID
}).then(function (result) {
if (ApiUpdatesManager.saveSeq(result.seq)) {
ApiUpdatesManager.saveUpdate({
_: 'updateMessageID',
random_id: randomIDS,
id: result.message.id
});
message.date = result.message.date;
message.id = result.message.id;
message.media = result.message.media;
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage',
message: message,
pts: result.pts
});
}
}, function (error) {
toggleError(true);
});
}, function (error) { }, function (error) {
toggleError(true); toggleError(true);
}, function (progress) {
// console.log('upload progress', progress);
media.progress.done = progress.done;
media.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
$rootScope.$broadcast('history_update', {peerID: peerID});
});
media.progress.cancel = function () {
if (!uploaded) {
sendFileDeferred.resolve();
uploadPromise.cancel();
cancelPendingMessage(randomIDS);
}
}
uploadPromise['finally'](function () {
sendFileDeferred.resolve();
}); });
}, function (error) {
toggleError(true);
}, function (progress) {
// console.log('upload progress', progress);
media.progress.done = progress.done;
media.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
$rootScope.$broadcast('history_update', {peerID: peerID});
}); });
media.progress.cancel = function () { sendFilePromise = sendFileDeferred.promise;
if (!uploaded) {
uploadPromise.cancel();
cancelPendingMessage(randomIDS);
}
}
}; };
saveMessages([message]); saveMessages([message]);