Browse Source

Fix #107 - cancel download, Fix #51 - cancel upload

master
Igor Zhukov 11 years ago
parent
commit
fb93cb503e
  1. 6
      app/css/app.css
  2. 6
      app/js/directives.js
  3. 10
      app/js/lib/mtproto.js
  4. 80
      app/js/services.js
  5. 39
      app/partials/message.html

6
app/css/app.css

@ -1273,6 +1273,12 @@ img.im_message_document_thumb {
margin-top: 5px; margin-top: 5px;
} }
.im_message_media_progress_cancel {
font-size: 11px;
margin-left: 10px;
line-height: 100%;
}
.tg_up_progress, .tg_up_progress,
.tg_down_progress { .tg_down_progress {
height: 5px; height: 5px;

6
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 = false;
// $scope.progress = {enabled: true, percent: 50}; // $scope.progress = {enabled: true, percent: 50};
$scope.player.hasQuicktime = hasQt; $scope.player.hasQuicktime = hasQt;
@ -841,7 +843,7 @@ angular.module('myApp.directives', ['myApp.filters'])
}); });
$scope.$on('$destroy', function () { $scope.$on('$destroy', function () {
promise.cancel(); downloadPromise.cancel();
}); });
} }

10
app/js/lib/mtproto.js

@ -281,7 +281,7 @@ function pqPrimeFactorization (pqBytes) {
try { try {
result = pqPrimeLong(goog.math.Long.fromString(what.toString(16), 16)); result = pqPrimeLong(goog.math.Long.fromString(what.toString(16), 16));
} catch (e) { } catch (e) {
console.error(e); console.error('Pq long Exception', e);
}; };
// console.timeEnd('PQ long'); // console.timeEnd('PQ long');
} }
@ -2931,8 +2931,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
resolved = false, resolved = false,
cacheFileWriter, cacheFileWriter,
errorHandler = function (error) { errorHandler = function (error) {
console.error(error); // console.error('Dl Error', error);
// console.log('fail');
deferred.reject(error); deferred.reject(error);
if (cacheFileWriter) cacheFileWriter.truncate(0); if (cacheFileWriter) cacheFileWriter.truncate(0);
errorHandler = angular.noop; errorHandler = angular.noop;
@ -3015,8 +3014,10 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
resolved = true; resolved = true;
deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL()); deferred.resolve(cachedDownloads[fileName] = fileEntry.toURL());
} else { } else {
// setTimeout(function () {
console.log('File bad size', file, size); console.log('File bad size', file, size);
cachedFs.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler) cachedFs.root.getFile(fileName, {create: true}, saveToFileEntry, errorHandler)
// }, 10000);
} }
}, errorHandler); }, errorHandler);
}, function () { }, function () {
@ -3144,7 +3145,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
var fileID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)], var fileID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)],
deferred = $q.defer(), deferred = $q.defer(),
errorHandler = function (error) { errorHandler = function (error) {
console.error('Error', error); // console.error('Up Error', error);
deferred.reject(error); deferred.reject(error);
errorHandler = angular.noop; errorHandler = angular.noop;
}, },
@ -3205,6 +3206,7 @@ factory('MtpApiFileManager', function (MtpApiManager, $q, $window) {
} }
deferred.promise.cancel = function () { deferred.promise.cancel = function () {
console.log('cancel upload', canceled, resolved);
if (!canceled && !resolved) { if (!canceled && !resolved) {
canceled = true; canceled = true;
errorHandler({type: 'UPLOAD_CANCELED'}); errorHandler({type: 'UPLOAD_CANCELED'});

80
app/js/services.js

@ -1214,8 +1214,10 @@ angular.module('myApp.services', [])
} }
message.send = function () { message.send = function () {
var uploaded = false; var uploaded = false,
var promise = MtpApiFileManager.uploadFile(file).then(function (inputFile) { uploadPromise = MtpApiFileManager.uploadFile(file);
uploadPromise.then(function (inputFile) {
uploaded = true; uploaded = true;
var inputMedia; var inputMedia;
switch (attachType) { switch (attachType) {
@ -1265,22 +1267,15 @@ angular.module('myApp.services', [])
toggleError(true); toggleError(true);
}, function (progress) { }, function (progress) {
// console.log('upload progress', 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.done = progress.done;
media.progress.percent = percent; media.progress.percent = Math.max(1, Math.floor(100 * progress.done / progress.total));
if (historyMessage) { $rootScope.$broadcast('history_update', {peerID: peerID});
historyMessage.media.progress.done = progress.done;
historyMessage.media.progress.percent = percent;
$rootScope.$broadcast('history_update', {peerID: peerID});
}
}); });
media.progress.cancel = function () { media.progress.cancel = function () {
if (!uploaded) { if (!uploaded) {
promise.cancel(); uploadPromise.cancel();
cancelPendingMessage(randomID); cancelPendingMessage(randomIDS);
} }
} }
}; };
@ -1407,12 +1402,19 @@ angular.module('myApp.services', [])
function cancelPendingMessage (randomID) { function cancelPendingMessage (randomID) {
var pendingData = pendingByRandomID[randomID]; var pendingData = pendingByRandomID[randomID];
console.log('pending', randomID, pendingData);
if (pendingData) { if (pendingData) {
var peerID = pendingData[0], var peerID = pendingData[0],
tempID = pendingData[1], tempID = pendingData[1],
historyStorage = historiesStorage[peerID], historyStorage = historiesStorage[peerID],
i; i;
ApiUpdatesManager.saveUpdate({
_: 'updateDeleteMessages',
messages: [tempID]
});
for (i = 0; i < historyStorage.pending.length; i++) { for (i = 0; i < historyStorage.pending.length; i++) {
if (historyStorage.pending[i] == tempID) { if (historyStorage.pending[i] == tempID) {
historyStorage.pending.splice(i, 1); historyStorage.pending.splice(i, 1);
@ -1423,10 +1425,6 @@ angular.module('myApp.services', [])
delete messagesForHistory[tempID]; delete messagesForHistory[tempID];
delete messagesStorage[tempID]; delete messagesStorage[tempID];
ApiUpdatesManager.saveUpdate({
_: 'updateDeleteMessages',
messages: [tempID]
});
return true; return true;
} }
@ -1533,8 +1531,8 @@ angular.module('myApp.services', [])
var message = angular.copy(messagesStorage[msgID]) || {id: msgID}; var message = angular.copy(messagesStorage[msgID]) || {id: msgID};
if (message.progress) { if (message.media && message.media.progress !== undefined) {
message.progress = messagesStorage[msgID].progress; message.media.progress = messagesStorage[msgID].media.progress;
} }
message.fromUser = AppUsersManager.getUser(message.from_id); message.fromUser = AppUsersManager.getUser(message.from_id);
@ -1836,13 +1834,22 @@ angular.module('myApp.services', [])
var historyStorage = historiesStorage[peerID]; var historyStorage = historiesStorage[peerID];
if (historyStorage !== undefined) { if (historyStorage !== undefined) {
var newHistory = []; var newHistory = [],
newPending = [];
for (var i = 0; i < historyStorage.history.length; i++) { for (var i = 0; i < historyStorage.history.length; i++) {
if (!updatedData.msgs[historyStorage.history[i]]) { if (!updatedData.msgs[historyStorage.history[i]]) {
newHistory.push(historyStorage.history[i]); newHistory.push(historyStorage.history[i]);
} }
} }
historyStorage.history = newHistory; 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}); $rootScope.$broadcast('history_delete', {peerID: peerID, msgs: updatedData.msgs});
} }
}); });
@ -2154,16 +2161,21 @@ angular.module('myApp.services', [])
extensions: [ext] extensions: [ext]
}] }]
}, function (writableFileEntry) { }, 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; delete historyVideo.progress;
console.log('file save done'); console.log('file save done');
}, function (e) { }, function (e) {
console.log('video download failed', e); console.log('video download failed', e);
historyVideo.progress.enabled = false; historyVideo.progress.enabled = false;
}, updateDownloadProgress); }, updateDownloadProgress);
historyVideo.progress.cancel = downloadPromise.cancel;
}); });
} else { } 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; delete historyVideo.progress;
if (popup) { if (popup) {
@ -2187,6 +2199,8 @@ angular.module('myApp.services', [])
console.log('video download failed', e); console.log('video download failed', e);
historyVideo.progress.enabled = false; historyVideo.progress.enabled = false;
}, updateDownloadProgress); }, updateDownloadProgress);
historyVideo.progress.cancel = downloadPromise.cancel;
} }
}; };
@ -2283,16 +2297,22 @@ angular.module('myApp.services', [])
extensions: [ext] extensions: [ext]
}] }]
}, function (writableFileEntry) { }, 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; delete historyDoc.progress;
console.log('file save done'); console.log('file save done');
}, function (e) { }, function (e) {
console.log('document download failed', e); console.log('document download failed', e);
historyDoc.progress.enabled = false; historyDoc.progress.enabled = false;
}, updateDownloadProgress); }, updateDownloadProgress);
historyDoc.progress.cancel = downloadPromise.cancel;
}); });
} else { } 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; delete historyDoc.progress;
if (popup) { if (popup) {
@ -2316,6 +2336,8 @@ angular.module('myApp.services', [])
console.log('document download failed', e); console.log('document download failed', e);
historyDoc.progress.enabled = false; historyDoc.progress.enabled = false;
}, updateDownloadProgress); }, updateDownloadProgress);
historyDoc.progress.cancel = downloadPromise.cancel;
} }
} }
@ -2364,7 +2386,9 @@ angular.module('myApp.services', [])
$rootScope.$broadcast('history_update'); $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; delete historyAudio.progress;
historyAudio.url = $sce.trustAsResourceUrl(url); historyAudio.url = $sce.trustAsResourceUrl(url);
historyAudio.autoplay = true; historyAudio.autoplay = true;
@ -2374,9 +2398,11 @@ angular.module('myApp.services', [])
$rootScope.$broadcast('history_update'); $rootScope.$broadcast('history_update');
}, 1000); }, 1000);
}, function (e) { }, function (e) {
console.log('document download failed', e); console.log('audio download failed', e);
historyDoc.progress.enabled = false; historyAudio.progress.enabled = false;
}, updateDownloadProgress); }, updateDownloadProgress);
historyAudio.progress.cancel = downloadPromise.cancel;
} }
$rootScope.openAudio = openAudio; $rootScope.openAudio = openAudio;

39
app/partials/message.html

@ -121,12 +121,11 @@
<a href="" ng-click="downloadVideo(historyMessage.media.video.id)">Download</a> <a href="" ng-click="downloadVideo(historyMessage.media.video.id)">Download</a>
<a href="" ng-click="openVideo(historyMessage.media.video.id)">Play video</a> <a href="" ng-click="openVideo(historyMessage.media.video.id)">Play video</a>
</div> </div>
<div class="im_message_download_progress_wrap" ng-if="historyMessage.media.video.progress.enabled"> <div class="clearfix cancelable_progress_wrap" ng-if="historyMessage.media.video.progress.enabled">
<div class="progress tg_down_progress"> <a class="im_message_media_progress_cancel pull-right" ng-click="historyMessage.media.video.progress.cancel()">Cancel</a>
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{historyMessage.media.video.progress.percent}}" aria-valuemin="0" aria-valuemax="100" style="width: {{historyMessage.media.video.progress.percent}}%"> <div class="im_message_download_progress_wrap">
<span class="sr-only"> <div class="progress tg_down_progress">
{{historyMessage.media.video.progress.percent}}% Complete (success) <div class="progress-bar progress-bar-success" ng-style="{width: historyMessage.media.video.progress.percent + '%'}"></div>
</span>
</div> </div>
</div> </div>
</div> </div>
@ -155,9 +154,12 @@
<a href="" ng-click="downloadDoc(historyMessage.media.document.id)">Download</a> <a href="" ng-click="downloadDoc(historyMessage.media.document.id)">Download</a>
<a href="" ng-click="downloadDoc(historyMessage.media.document.id, false, true)" bo-if="historyMessage.media.document.withPreview">Open</a> <a href="" ng-click="downloadDoc(historyMessage.media.document.id, false, true)" bo-if="historyMessage.media.document.withPreview">Open</a>
</div> </div>
<div class="im_message_download_progress_wrap" ng-if="historyMessage.media.document.progress.enabled"> <div class="clearfix cancelable_progress_wrap" ng-if="historyMessage.media.document.progress.enabled">
<div class="progress tg_down_progress"> <a class="im_message_media_progress_cancel pull-right" ng-click="historyMessage.media.document.progress.cancel()">Cancel</a>
<div class="progress-bar progress-bar-success" ng-style="{width: historyMessage.media.document.progress.percent + '%'}"></div> <div class="im_message_download_progress_wrap">
<div class="progress tg_down_progress">
<div class="progress-bar progress-bar-success" ng-style="{width: historyMessage.media.document.progress.percent + '%'}"></div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -181,9 +183,12 @@
<div class="im_message_audio_actions" ng-if="!historyMessage.media.audio.progress.enabled &amp;&amp; !historyMessage.media.audio.url"> <div class="im_message_audio_actions" ng-if="!historyMessage.media.audio.progress.enabled &amp;&amp; !historyMessage.media.audio.url">
<a href="" ng-click="openAudio(historyMessage.media.audio.id)">Play</a> <a href="" ng-click="openAudio(historyMessage.media.audio.id)">Play</a>
</div> </div>
<div class="im_message_download_progress_wrap" ng-if="historyMessage.media.audio.progress.enabled"> <div class="clearfix cancelable_progress_wrap" ng-if="historyMessage.media.audio.progress.enabled">
<div class="progress tg_down_progress"> <a class="im_message_media_progress_cancel pull-right" ng-click="historyMessage.media.audio.progress.cancel()">Cancel</a>
<div class="progress-bar progress-bar-success" ng-style="{width: historyMessage.media.audio.progress.percent + '%'}"></div> <div class="im_message_download_progress_wrap">
<div class="progress tg_down_progress">
<div class="progress-bar progress-bar-success" ng-style="{width: historyMessage.media.audio.progress.percent + '%'}"></div>
</div>
</div> </div>
</div> </div>
<div class="im_message_audio_player_wrap" ng-if="historyMessage.media.audio.url"> <div class="im_message_audio_player_wrap" ng-if="historyMessage.media.audio.url">
@ -221,10 +226,12 @@
<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="clearfix cancelable_progress_wrap">
<div class="im_message_download_progress_wrap"> <a class="im_message_media_progress_cancel pull-right" ng-click="historyMessage.media.progress.cancel()">Cancel</a>
<div class="progress tg_down_progress"> <div class="im_message_download_progress_wrap">
<div class="progress-bar progress-bar-success" role="progressbar" ng-style="{width: historyMessage.media.progress.percent + '%'}"></div> <div class="progress tg_down_progress">
<div class="progress-bar progress-bar-success" role="progressbar" ng-style="{width: historyMessage.media.progress.percent + '%'}"></div>
</div>
</div> </div>
</div> </div>
</div> </div>

Loading…
Cancel
Save