From fb93cb503e54fd84f56de93bed47b756ca4e8ed2 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Wed, 2 Apr 2014 14:16:09 +0400 Subject: [PATCH] Fix #107 - cancel download, Fix #51 - cancel upload --- app/css/app.css | 6 +++ app/js/directives.js | 6 ++- app/js/lib/mtproto.js | 10 +++-- app/js/services.js | 80 ++++++++++++++++++++++++++------------- app/partials/message.html | 39 +++++++++++-------- 5 files changed, 92 insertions(+), 49 deletions(-) diff --git a/app/css/app.css b/app/css/app.css index 4796c549..967a8482 100644 --- a/app/css/app.css +++ b/app/css/app.css @@ -1273,6 +1273,12 @@ img.im_message_document_thumb { margin-top: 5px; } +.im_message_media_progress_cancel { + font-size: 11px; + margin-left: 10px; + line-height: 100%; +} + .tg_up_progress, .tg_down_progress { height: 5px; diff --git a/app/js/directives.js b/app/js/directives.js index 5fa5d6bd..f2a491db 100644 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -819,7 +819,9 @@ angular.module('myApp.directives', ['myApp.filters']) } } - var promise = MtpApiFileManager.downloadFile($scope.video.dc_id, inputLocation, $scope.video.size, null, {mime: 'video/mp4'}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile($scope.video.dc_id, inputLocation, $scope.video.size, null, {mime: 'video/mp4'}); + + downloadPromise.then(function (url) { $scope.progress.enabled = false; // $scope.progress = {enabled: true, percent: 50}; $scope.player.hasQuicktime = hasQt; @@ -841,7 +843,7 @@ angular.module('myApp.directives', ['myApp.filters']) }); $scope.$on('$destroy', function () { - promise.cancel(); + downloadPromise.cancel(); }); } diff --git a/app/js/lib/mtproto.js b/app/js/lib/mtproto.js index 2ff499af..2f595107 100644 --- a/app/js/lib/mtproto.js +++ b/app/js/lib/mtproto.js @@ -281,7 +281,7 @@ function pqPrimeFactorization (pqBytes) { try { result = pqPrimeLong(goog.math.Long.fromString(what.toString(16), 16)); } catch (e) { - console.error(e); + console.error('Pq long Exception', e); }; // console.timeEnd('PQ long'); } @@ -2931,8 +2931,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { resolved = false, cacheFileWriter, errorHandler = function (error) { - console.error(error); - // console.log('fail'); + // console.error('Dl Error', error); deferred.reject(error); if (cacheFileWriter) cacheFileWriter.truncate(0); errorHandler = angular.noop; @@ -3015,8 +3014,10 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { resolved = true; deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); } else { + // setTimeout(function () { console.log('File bad size', file, size); cachedFs.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler) + // }, 10000); } }, errorHandler); }, function () { @@ -3144,7 +3145,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { var fileID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)], deferred = $q.defer(), errorHandler = function (error) { - console.error('Error', error); + // console.error('Up Error', error); deferred.reject(error); errorHandler = angular.noop; }, @@ -3205,6 +3206,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) { } deferred.promise.cancel = function () { + console.log('cancel upload', canceled, resolved); if (!canceled && !resolved) { canceled = true; errorHandler({type: 'UPLOAD_CANCELED'}); diff --git a/app/js/services.js b/app/js/services.js index 31fa575a..0b8ec76b 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -1214,8 +1214,10 @@ angular.module('myApp.services', []) } message.send = function () { - var uploaded = false; - var promise = MtpApiFileManager.uploadFile(file).then(function (inputFile) { + var uploaded = false, + uploadPromise = MtpApiFileManager.uploadFile(file); + + uploadPromise.then(function (inputFile) { uploaded = true; var inputMedia; switch (attachType) { @@ -1265,22 +1267,15 @@ angular.module('myApp.services', []) toggleError(true); }, function (progress) { // console.log('upload progress', progress); - var historyMessage = messagesForHistory[messageID], - percent = Math.max(1, Math.floor(100 * progress.done / progress.total)); - media.progress.done = progress.done; - media.progress.percent = percent; - if (historyMessage) { - historyMessage.media.progress.done = progress.done; - historyMessage.media.progress.percent = percent; - $rootScope.$broadcast('history_update', {peerID: peerID}); - } + 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) { - promise.cancel(); - cancelPendingMessage(randomID); + uploadPromise.cancel(); + cancelPendingMessage(randomIDS); } } }; @@ -1407,12 +1402,19 @@ angular.module('myApp.services', []) function cancelPendingMessage (randomID) { var pendingData = pendingByRandomID[randomID]; + console.log('pending', randomID, pendingData); + if (pendingData) { var peerID = pendingData[0], tempID = pendingData[1], historyStorage = historiesStorage[peerID], i; + ApiUpdatesManager.saveUpdate({ + _: 'updateDeleteMessages', + messages: [tempID] + }); + for (i = 0; i < historyStorage.pending.length; i++) { if (historyStorage.pending[i] == tempID) { historyStorage.pending.splice(i, 1); @@ -1423,10 +1425,6 @@ angular.module('myApp.services', []) delete messagesForHistory[tempID]; delete messagesStorage[tempID]; - ApiUpdatesManager.saveUpdate({ - _: 'updateDeleteMessages', - messages: [tempID] - }); return true; } @@ -1533,8 +1531,8 @@ angular.module('myApp.services', []) var message = angular.copy(messagesStorage[msgID]) || {id: msgID}; - if (message.progress) { - message.progress = messagesStorage[msgID].progress; + if (message.media && message.media.progress !== undefined) { + message.media.progress = messagesStorage[msgID].media.progress; } message.fromUser = AppUsersManager.getUser(message.from_id); @@ -1836,13 +1834,22 @@ angular.module('myApp.services', []) var historyStorage = historiesStorage[peerID]; if (historyStorage !== undefined) { - var newHistory = []; + var newHistory = [], + newPending = []; for (var i = 0; i < historyStorage.history.length; i++) { if (!updatedData.msgs[historyStorage.history[i]]) { newHistory.push(historyStorage.history[i]); } } historyStorage.history = newHistory; + + for (var i = 0; i < historyStorage.pending.length; i++) { + if (!updatedData.msgs[historyStorage.pending[i]]) { + newPending.push(historyStorage.pending[i]); + } + } + historyStorage.pending = newPending; + $rootScope.$broadcast('history_delete', {peerID: peerID, msgs: updatedData.msgs}); } }); @@ -2154,16 +2161,21 @@ angular.module('myApp.services', []) extensions: [ext] }] }, function (writableFileEntry) { - MtpApiFileManager.downloadFile(video.dc_id, inputFileLocation, video.size, writableFileEntry, {mime: mimeType}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile(video.dc_id, inputFileLocation, video.size, writableFileEntry, {mime: mimeType}); + downloadPromise.then(function (url) { delete historyVideo.progress; console.log('file save done'); }, function (e) { console.log('video download failed', e); historyVideo.progress.enabled = false; }, updateDownloadProgress); + + historyVideo.progress.cancel = downloadPromise.cancel; }); } else { - MtpApiFileManager.downloadFile(video.dc_id, inputFileLocation, video.size, null, {mime: mimeType}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile(video.dc_id, inputFileLocation, video.size, null, {mime: mimeType}); + + downloadPromise.then(function (url) { delete historyVideo.progress; if (popup) { @@ -2187,6 +2199,8 @@ angular.module('myApp.services', []) console.log('video download failed', e); historyVideo.progress.enabled = false; }, updateDownloadProgress); + + historyVideo.progress.cancel = downloadPromise.cancel; } }; @@ -2283,16 +2297,22 @@ angular.module('myApp.services', []) extensions: [ext] }] }, function (writableFileEntry) { - MtpApiFileManager.downloadFile(doc.dc_id, inputFileLocation, doc.size, writableFileEntry, {mime: doc.mime_type}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile(doc.dc_id, inputFileLocation, doc.size, writableFileEntry, {mime: doc.mime_type}); + + downloadPromise.then(function (url) { delete historyDoc.progress; console.log('file save done'); }, function (e) { console.log('document download failed', e); historyDoc.progress.enabled = false; }, updateDownloadProgress); + + historyDoc.progress.cancel = downloadPromise.cancel; }); } else { - MtpApiFileManager.downloadFile(doc.dc_id, inputFileLocation, doc.size, null, {mime: doc.mime_type}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile(doc.dc_id, inputFileLocation, doc.size, null, {mime: doc.mime_type}); + + downloadPromise.then(function (url) { delete historyDoc.progress; if (popup) { @@ -2316,6 +2336,8 @@ angular.module('myApp.services', []) console.log('document download failed', e); historyDoc.progress.enabled = false; }, updateDownloadProgress); + + historyDoc.progress.cancel = downloadPromise.cancel; } } @@ -2364,7 +2386,9 @@ angular.module('myApp.services', []) $rootScope.$broadcast('history_update'); } - MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, null, {mime: 'audio/mpeg'}).then(function (url) { + var downloadPromise = MtpApiFileManager.downloadFile(audio.dc_id, inputFileLocation, audio.size, null, {mime: 'audio/mpeg'}); + + downloadPromise.then(function (url) { delete historyAudio.progress; historyAudio.url = $sce.trustAsResourceUrl(url); historyAudio.autoplay = true; @@ -2374,9 +2398,11 @@ angular.module('myApp.services', []) $rootScope.$broadcast('history_update'); }, 1000); }, function (e) { - console.log('document download failed', e); - historyDoc.progress.enabled = false; + console.log('audio download failed', e); + historyAudio.progress.enabled = false; }, updateDownloadProgress); + + historyAudio.progress.cancel = downloadPromise.cancel; } $rootScope.openAudio = openAudio; diff --git a/app/partials/message.html b/app/partials/message.html index dcfeee7a..983a5bcd 100644 --- a/app/partials/message.html +++ b/app/partials/message.html @@ -121,12 +121,11 @@ Download Play video -
-
-
- - {{historyMessage.media.video.progress.percent}}% Complete (success) - +
+ Cancel +
+
+
@@ -155,9 +154,12 @@ Download Open
-
-
-
+
+ Cancel +
+
+
+
@@ -181,9 +183,12 @@ -
-
-
+
+ Cancel +
+
+
+
@@ -221,10 +226,12 @@
- -
-
-
+
+ Cancel +
+
+
+