Browse Source

Download/upload cancel draft

master
Igor Zhukov 11 years ago
parent
commit
b65b932dbf
  1. 8
      app/js/directives.js
  2. 43
      app/js/lib/mtproto.js
  3. 45
      app/js/services.js
  4. 1
      app/partials/message.html

8
app/js/directives.js

@ -791,7 +791,7 @@ angular.module('myApp.directives', ['myApp.filters'])
</div>\ </div>\
<div class="video_full_error_wrap" ng-if="error">\ <div class="video_full_error_wrap" ng-if="error">\
<div class="video_full_error" ng-if="error.html" ng-bind-html="error.html"></div>\ <div class="video_full_error" ng-if="error.html" ng-bind-html="error.html"></div>\
<div class="video_full_error" ng-if="error.text">{{error.text}}</div>\ <div class="video_full_error" ng-if="error.text" ng-bind="error.text"></div>\
</div>\ </div>\
</div>', </div>',
scope: { scope: {
@ -819,7 +819,7 @@ angular.module('myApp.directives', ['myApp.filters'])
} }
} }
MtpApiFileManager.downloadFile($scope.video.dc_id, inputLocation, $scope.video.size, null, {mime: 'video/mp4'}).then(function (url) { var promise = MtpApiFileManager.downloadFile($scope.video.dc_id, inputLocation, $scope.video.size, null, {mime: 'video/mp4'}).then(function (url) {
$scope.progress.enabled = false; $scope.progress.enabled = false;
// $scope.progress = {enabled: true, percent: 50}; // $scope.progress = {enabled: true, percent: 50};
$scope.player.hasQuicktime = hasQt; $scope.player.hasQuicktime = hasQt;
@ -839,6 +839,10 @@ angular.module('myApp.directives', ['myApp.filters'])
}, function (progress) { }, function (progress) {
$scope.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total)); $scope.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
}); });
$scope.$on('$destroy', function () {
promise.cancel();
});
} }
}) })

43
app/js/lib/mtproto.js

@ -2927,6 +2927,8 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
} }
var deferred = $q.defer(), var deferred = $q.defer(),
canceled = false,
resolved = false,
cacheFileWriter, cacheFileWriter,
errorHandler = function (error) { errorHandler = function (error) {
console.error(error); console.error(error);
@ -2948,7 +2950,10 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
writeFileDeferred = $q.defer(); writeFileDeferred = $q.defer();
(function (isFinal, offset, writeFileDeferred, writeFilePromise) { (function (isFinal, offset, writeFileDeferred, writeFilePromise) {
return downloadRequest(dcID, function () { return downloadRequest(dcID, function () {
// console.log('next big promise'); // console.log('next big promise');
if (canceled) {
return $q.when();
}
return MtpApiManager.invokeApi('upload.getFile', { return MtpApiManager.invokeApi('upload.getFile', {
location: location, location: location,
offset: offset, offset: offset,
@ -2964,6 +2969,9 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
// console.log('waiting for file promise', offset); // console.log('waiting for file promise', offset);
writeFilePromise.then(function () { writeFilePromise.then(function () {
// console.log('resolved file promise', offset); // console.log('resolved file promise', offset);
if (canceled) {
return $q.when();
}
return fileWriteBytes(fileWriter, result.bytes).then(function () { return fileWriteBytes(fileWriter, result.bytes).then(function () {
@ -2974,6 +2982,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
if (isFinal) { if (isFinal) {
// console.timeEnd(fileName + ' ' + (size / 1024)); // console.timeEnd(fileName + ' ' + (size / 1024));
resolved = true;
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL(options.mime || 'image/jpeg')); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL(options.mime || 'image/jpeg'));
} else { } else {
// console.log('notify', {done: offset + limit, total: size}); // console.log('notify', {done: offset + limit, total: size});
@ -3003,6 +3012,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
fileEntry.file(function(file) { fileEntry.file(function(file) {
// console.log(dT(), 'Check size', file.size, size); // console.log(dT(), 'Check size', file.size, size);
if (file.size >= size/* && false*/) { if (file.size >= size/* && false*/) {
resolved = true;
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL());
} else { } else {
console.log('File bad size', file, size); console.log('File bad size', file, size);
@ -3022,6 +3032,9 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
writeBlobDeferred = $q.defer(); writeBlobDeferred = $q.defer();
(function (isFinal, offset, writeBlobDeferred, writeBlobPromise) { (function (isFinal, offset, writeBlobDeferred, writeBlobPromise) {
return downloadRequest(dcID, function () { return downloadRequest(dcID, function () {
if (canceled) {
return $q.when();
}
return MtpApiManager.invokeApi('upload.getFile', { return MtpApiManager.invokeApi('upload.getFile', {
location: location, location: location,
offset: offset, offset: offset,
@ -3033,6 +3046,9 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}); });
}, 6).then(function (result) { }, 6).then(function (result) {
writeBlobPromise.then(function () { writeBlobPromise.then(function () {
if (canceled) {
return $q.when();
}
try { try {
blobParts.push(bytesToArrayBuffer(result.bytes)); blobParts.push(bytesToArrayBuffer(result.bytes));
writeBlobDeferred.resolve(); writeBlobDeferred.resolve();
@ -3049,8 +3065,8 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
var blob = bb.getBlob(options.mime || 'image/jpeg'); var blob = bb.getBlob(options.mime || 'image/jpeg');
} }
window.URL = window.URL || window.webkitURL; window.URL = window.URL || window.webkitURL;
resolved = true;
deferred.resolve(cachedDownloads[fileName] = URL.createObjectURL(blob)); deferred.resolve(cachedDownloads[fileName] = URL.createObjectURL(blob));
} else { } else {
deferred.notify({done: offset + limit, total: size}); deferred.notify({done: offset + limit, total: size});
@ -3071,6 +3087,14 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
}); });
} }
deferred.promise.cancel = function () {
if (!canceled && !resolved) {
canceled = true;
delete cachedDownloadPromises[fileName];
errorHandler({type: 'DOWNLOAD_CANCELED'});
}
}
return cachedDownloadPromises[fileName] = deferred.promise; return cachedDownloadPromises[fileName] = deferred.promise;
} }
@ -3106,8 +3130,11 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
function uploadFile (file) { function uploadFile (file) {
var fileSize = file.size, var fileSize = file.size,
// partSize = fileSize > 102400 ? 65536 : 4096, // partSize = fileSize > 102400 ? 65536 : 4096,
partSize = fileSize > 102400 ? 524288 : 4096, // partSize = fileSize > 102400 ? 524288 : 4096,
partSize = fileSize > 102400 ? 524288 : 30720,
totalParts = Math.ceil(fileSize / partSize), totalParts = Math.ceil(fileSize / partSize),
canceled = false,
resolved = false,
doneParts = 0; doneParts = 0;
if (totalParts > 1500) { if (totalParts > 1500) {
@ -3143,7 +3170,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
var blob = file.slice(offset, offset + partSize); var blob = file.slice(offset, offset + partSize);
reader.onloadend = function (e) { reader.onloadend = function (e) {
if (e.target.readyState != FileReader.DONE) { if (canceled || e.target.readyState != FileReader.DONE) {
return; return;
} }
var apiCurPromise = apiUploadPromise = apiUploadPromise.then(function () { var apiCurPromise = apiUploadPromise = apiUploadPromise.then(function () {
@ -3162,6 +3189,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
fileReadDeferred.resolve(); fileReadDeferred.resolve();
if (doneParts >= totalParts) { if (doneParts >= totalParts) {
deferred.resolve(resultInputFile); deferred.resolve(resultInputFile);
resolved = true;
} else { } else {
console.log(dT(), 'Progress', doneParts * partSize / fileSize); console.log(dT(), 'Progress', doneParts * partSize / fileSize);
deferred.notify({done: doneParts * partSize, total: fileSize}); deferred.notify({done: doneParts * partSize, total: fileSize});
@ -3176,6 +3204,13 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
})(offset, part++); })(offset, part++);
} }
deferred.promise.cancel = function () {
if (!canceled && !resolved) {
canceled = true;
errorHandler({type: 'UPLOAD_CANCELED'});
}
}
return deferred.promise; return deferred.promise;
} }

45
app/js/services.js

@ -1214,7 +1214,9 @@ angular.module('myApp.services', [])
} }
message.send = function () { message.send = function () {
MtpApiFileManager.uploadFile(file).then(function (inputFile) { var uploaded = false;
var promise = MtpApiFileManager.uploadFile(file).then(function (inputFile) {
uploaded = true;
var inputMedia; var inputMedia;
switch (attachType) { switch (attachType) {
case 'photo': case 'photo':
@ -1274,6 +1276,13 @@ angular.module('myApp.services', [])
$rootScope.$broadcast('history_update', {peerID: peerID}); $rootScope.$broadcast('history_update', {peerID: peerID});
} }
}); });
media.progress.cancel = function () {
if (!uploaded) {
promise.cancel();
cancelPendingMessage(randomID);
}
}
}; };
saveMessages([message]); saveMessages([message]);
@ -1395,6 +1404,36 @@ angular.module('myApp.services', [])
}); });
}; };
function cancelPendingMessage (randomID) {
var pendingData = pendingByRandomID[randomID];
if (pendingData) {
var peerID = pendingData[0],
tempID = pendingData[1],
historyStorage = historiesStorage[peerID],
i;
for (i = 0; i < historyStorage.pending.length; i++) {
if (historyStorage.pending[i] == tempID) {
historyStorage.pending.splice(i, 1);
break;
}
}
delete messagesForHistory[tempID];
delete messagesStorage[tempID];
ApiUpdatesManager.saveUpdate({
_: 'updateDeleteMessages',
messages: [tempID]
});
return true;
}
return false;
}
function finalizePendingMessage(randomID, finalMessage) { function finalizePendingMessage(randomID, finalMessage) {
var pendingData = pendingByRandomID[randomID]; var pendingData = pendingByRandomID[randomID];
// console.log('pdata', randomID, pendingData); // console.log('pdata', randomID, pendingData);
@ -1494,6 +1533,10 @@ angular.module('myApp.services', [])
var message = angular.copy(messagesStorage[msgID]) || {id: msgID}; var message = angular.copy(messagesStorage[msgID]) || {id: msgID};
if (message.progress) {
message.progress = messagesStorage[msgID].progress;
}
message.fromUser = AppUsersManager.getUser(message.from_id); message.fromUser = AppUsersManager.getUser(message.from_id);
message.fromPhoto = AppUsersManager.getUserPhoto(message.from_id, 'User'); message.fromPhoto = AppUsersManager.getUserPhoto(message.from_id, 'User');

1
app/partials/message.html

@ -221,6 +221,7 @@
<span class="im_message_document_name" bo-bind="historyMessage.media.file_name"></span> <span class="im_message_document_name" bo-bind="historyMessage.media.file_name"></span>
<span class="im_message_document_size" ng-if="historyMessage.media.progress" ng-bind="historyMessage.media.progress | formatSizeProgress"></span> <span class="im_message_document_size" ng-if="historyMessage.media.progress" ng-bind="historyMessage.media.progress | formatSizeProgress"></span>
</div> </div>
<!-- <a href="" class="pull-right" ng-click="historyMessage.media.progress.cancel()">Cancel</a> -->
<div class="im_message_download_progress_wrap"> <div class="im_message_download_progress_wrap">
<div class="progress tg_down_progress"> <div class="progress tg_down_progress">
<div class="progress-bar progress-bar-success" role="progressbar" ng-style="{width: historyMessage.media.progress.percent + '%'}"></div> <div class="progress-bar progress-bar-success" role="progressbar" ng-style="{width: historyMessage.media.progress.percent + '%'}"></div>

Loading…
Cancel
Save