From 2bca6fae410e841d2a7342be8afe1e573d1d2b3b Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Sat, 21 Jun 2014 01:23:52 +0400 Subject: [PATCH] Fixed Chrome App file download bugs Closes #335 --- app/js/lib/aes_worker.js | 2 +- app/js/lib/mtproto_wrapper.js | 2 +- app/js/lib/ng_utils.js | 44 ++++++++++++++++++++----------- app/js/lib/pq_worker.js | 2 +- app/js/lib/sha1_worker.js | 2 +- app/partials/phonebook_modal.html | 4 +++ 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/js/lib/aes_worker.js b/app/js/lib/aes_worker.js index 4fb69b50..2b89c847 100644 --- a/app/js/lib/aes_worker.js +++ b/app/js/lib/aes_worker.js @@ -7,7 +7,7 @@ importScripts( '../../vendor/console-polyfill/console-polyfill.js', - 'mtproto.js', + 'bin_utils.js', '../../vendor/jsbn/jsbn_combined.js', '../../vendor/cryptoJS/crypto.js' ); diff --git a/app/js/lib/mtproto_wrapper.js b/app/js/lib/mtproto_wrapper.js index a53809c5..bf564263 100644 --- a/app/js/lib/mtproto_wrapper.js +++ b/app/js/lib/mtproto_wrapper.js @@ -422,7 +422,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) deferred.resolve(cachedDownloads[fileName] = FileManager.getUrl(blob, mimeType)); } }, function () { - var fileWriterPromise = toFileEntry ? $q.when(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType); + var fileWriterPromise = toFileEntry ? FileManager.getFileWriter(toFileEntry) : fileStorage.getFileWriter(fileName, mimeType); fileWriterPromise.then(function (fileWriter) { cacheFileWriter = fileWriter; diff --git a/app/js/lib/ng_utils.js b/app/js/lib/ng_utils.js index be46726b..dd6654a8 100644 --- a/app/js/lib/ng_utils.js +++ b/app/js/lib/ng_utils.js @@ -140,20 +140,14 @@ angular.module('izhukov.utils', []) $window.BlobBuilder = $window.BlobBuilder || $window.WebKitBlobBuilder || $window.MozBlobBuilder; function fileCopyTo (fromFileEntry, toFileEntry) { - var deferred = $q.defer(); - - toFileEntry.createWriter(function (fileWriter) { - fileWriteData(fileWriter, fromFileEntry).then(function () { - deferred.resolve(fileWriter); - }, function (e) { - deferred.reject(e); + return getFileWriter(toFileEntry).then(function (fileWriter) { + return fileWriteData(fileWriter, fromFileEntry).then(function () { + return fileWriter; + }, function (error) { + return $q.reject(error); fileWriter.truncate(0); }); - }, function (e) { - deferred.reject(e); }); - - return deferred.promise; } function fileWriteData(fileWriter, bytes) { @@ -166,9 +160,15 @@ angular.module('izhukov.utils', []) deferred.reject(); }; - if (bytes instanceof Blob) { // is file bytes + if (bytes instanceof FileEntry) { + bytes.file(function (file) { + fileWriter.write(file); + }, fileWriter.onerror); + } + else if (bytes instanceof Blob) { // is file bytes fileWriter.write(bytes); - } else { + } + else { fileWriter.write(new Blob([bytesToArrayBuffer(bytes)])); } @@ -195,6 +195,18 @@ angular.module('izhukov.utils', []) return deferred.promise; } + function getFileWriter (fileEntry) { + var deferred = $q.defer(); + + fileEntry.createWriter(function (fileWriter) { + deferred.resolve(fileWriter); + }, function (error) { + deferred.reject(error); + }); + + return deferred.promise; + } + function getFakeFileWriter (mimeType, saveFileCallback) { var blobParts = [], fakeFileWriter = { @@ -259,6 +271,7 @@ angular.module('izhukov.utils', []) return { copy: fileCopyTo, write: fileWriteData, + getFileWriter: getFileWriter, getFakeFileWriter: getFakeFileWriter, chooseSave: chooseSaveFile, getUrl: getUrl, @@ -429,7 +442,7 @@ angular.module('izhukov.utils', []) fileEntry.file(function(file) { // console.log(dT(), 'aa', file); if (file.size >= size) { - deferred.resolve(fileEntry); + deferred.resolve(file); } else { deferred.reject(new Error('FILE_NOT_FOUND')); } @@ -457,11 +470,10 @@ angular.module('izhukov.utils', []) return requestFS().then(function () { var deferred = $q.defer(); cachedFs.root.getFile(fileName, {create: true}, function (fileEntry) { - fileEntry.createWriter(function (fileWriter) { + FileManager.getFileWriter(fileEntry).then(function (fileWriter) { fileWriter.finalize = function () { return fileEntry; } - // console.log(dT(), 'got writer'); deferred.resolve(fileWriter); }, function (error) { deferred.reject(error); diff --git a/app/js/lib/pq_worker.js b/app/js/lib/pq_worker.js index 2e7adab2..a54af723 100644 --- a/app/js/lib/pq_worker.js +++ b/app/js/lib/pq_worker.js @@ -7,7 +7,7 @@ importScripts( '../../vendor/console-polyfill/console-polyfill.js', - 'mtproto.js', + 'bin_utils.js', '../../vendor/closure/long.js', '../../vendor/jsbn/jsbn_combined.js' ); diff --git a/app/js/lib/sha1_worker.js b/app/js/lib/sha1_worker.js index f99aa3ba..bdcd198e 100644 --- a/app/js/lib/sha1_worker.js +++ b/app/js/lib/sha1_worker.js @@ -7,7 +7,7 @@ importScripts( '../../vendor/console-polyfill/console-polyfill.js', - 'mtproto.js', + 'bin_utils.js', '../../vendor/cryptoJS/crypto.js' ); diff --git a/app/partials/phonebook_modal.html b/app/partials/phonebook_modal.html index a3c85566..f22d8832 100644 --- a/app/partials/phonebook_modal.html +++ b/app/partials/phonebook_modal.html @@ -1,7 +1,11 @@
+ +