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

45
app/js/services.js

@ -1214,7 +1214,9 @@ angular.module('myApp.services', []) @@ -1214,7 +1214,9 @@ angular.module('myApp.services', [])
}
message.send = function () {
MtpApiFileManager.uploadFile(file).then(function (inputFile) {
var uploaded = false;
var promise = MtpApiFileManager.uploadFile(file).then(function (inputFile) {
uploaded = true;
var inputMedia;
switch (attachType) {
case 'photo':
@ -1274,6 +1276,13 @@ angular.module('myApp.services', []) @@ -1274,6 +1276,13 @@ angular.module('myApp.services', [])
$rootScope.$broadcast('history_update', {peerID: peerID});
}
});
media.progress.cancel = function () {
if (!uploaded) {
promise.cancel();
cancelPendingMessage(randomID);
}
}
};
saveMessages([message]);
@ -1395,6 +1404,36 @@ angular.module('myApp.services', []) @@ -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) {
var pendingData = pendingByRandomID[randomID];
// console.log('pdata', randomID, pendingData);
@ -1494,6 +1533,10 @@ angular.module('myApp.services', []) @@ -1494,6 +1533,10 @@ angular.module('myApp.services', [])
var message = angular.copy(messagesStorage[msgID]) || {id: msgID};
if (message.progress) {
message.progress = messagesStorage[msgID].progress;
}
message.fromUser = AppUsersManager.getUser(message.from_id);
message.fromPhoto = AppUsersManager.getUserPhoto(message.from_id, 'User');

1
app/partials/message.html

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

Loading…
Cancel
Save