Old browsers improve
Added function.bind polyfill Added workaround for browsers without Blob constructors Closes #354
This commit is contained in:
parent
ec3dddf50d
commit
9060ec2a44
@ -341,6 +341,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
}
|
}
|
||||||
|
|
||||||
function downloadSmallFile(location) {
|
function downloadSmallFile(location) {
|
||||||
|
if (!FileManager.isAvailable()) {
|
||||||
|
return $q.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'});
|
||||||
|
}
|
||||||
// console.log('dload small', location);
|
// console.log('dload small', location);
|
||||||
var fileName = getFileName(location),
|
var fileName = getFileName(location),
|
||||||
mimeType = 'image/jpeg',
|
mimeType = 'image/jpeg',
|
||||||
@ -379,6 +382,10 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
|
|||||||
}
|
}
|
||||||
|
|
||||||
function downloadFile (dcID, location, size, options) {
|
function downloadFile (dcID, location, size, options) {
|
||||||
|
if (!FileManager.isAvailable()) {
|
||||||
|
return $q.reject({type: 'BROWSER_BLOB_NOT_SUPPORTED'});
|
||||||
|
}
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
// console.log(dT(), 'Dload file', dcID, location, size);
|
// console.log(dT(), 'Dload file', dcID, location, size);
|
||||||
|
@ -139,6 +139,18 @@ angular.module('izhukov.utils', [])
|
|||||||
$window.URL = $window.URL || $window.webkitURL;
|
$window.URL = $window.URL || $window.webkitURL;
|
||||||
$window.BlobBuilder = $window.BlobBuilder || $window.WebKitBlobBuilder || $window.MozBlobBuilder;
|
$window.BlobBuilder = $window.BlobBuilder || $window.WebKitBlobBuilder || $window.MozBlobBuilder;
|
||||||
|
|
||||||
|
var blobSupported = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
blobConstruct([], '');
|
||||||
|
} catch (e) {
|
||||||
|
blobSupported = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isBlobAvailable () {
|
||||||
|
return blobSupported;
|
||||||
|
}
|
||||||
|
|
||||||
function fileCopyTo (fromFileEntry, toFileEntry) {
|
function fileCopyTo (fromFileEntry, toFileEntry) {
|
||||||
return getFileWriter(toFileEntry).then(function (fileWriter) {
|
return getFileWriter(toFileEntry).then(function (fileWriter) {
|
||||||
return fileWriteData(fileWriter, fromFileEntry).then(function () {
|
return fileWriteData(fileWriter, fromFileEntry).then(function () {
|
||||||
@ -150,6 +162,20 @@ angular.module('izhukov.utils', [])
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function blobConstruct (blobParts, mimeType) {
|
||||||
|
var blob;
|
||||||
|
try {
|
||||||
|
blob = new Blob(blobParts, {type: mimeType});
|
||||||
|
} catch (e) {
|
||||||
|
var bb = new BlobBuilder;
|
||||||
|
angular.forEach(blobParts, function(blobPart) {
|
||||||
|
bb.append(blobPart);
|
||||||
|
});
|
||||||
|
blob = bb.getBlob(mimeType);
|
||||||
|
}
|
||||||
|
return blob;
|
||||||
|
}
|
||||||
|
|
||||||
function fileWriteData(fileWriter, bytes) {
|
function fileWriteData(fileWriter, bytes) {
|
||||||
var deferred = $q.defer();
|
var deferred = $q.defer();
|
||||||
|
|
||||||
@ -171,7 +197,12 @@ angular.module('izhukov.utils', [])
|
|||||||
fileWriter.write(bytes);
|
fileWriter.write(bytes);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fileWriter.write(new Blob([bytesToArrayBuffer(bytes)]));
|
try {
|
||||||
|
var blob = blobConstruct([bytesToArrayBuffer(bytes)]);
|
||||||
|
} catch (e) {
|
||||||
|
deferred.reject(e);
|
||||||
|
}
|
||||||
|
fileWriter.write(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
return deferred.promise;
|
return deferred.promise;
|
||||||
@ -213,6 +244,12 @@ angular.module('izhukov.utils', [])
|
|||||||
var blobParts = [],
|
var blobParts = [],
|
||||||
fakeFileWriter = {
|
fakeFileWriter = {
|
||||||
write: function (blob) {
|
write: function (blob) {
|
||||||
|
if (!blobSupported) {
|
||||||
|
if (fakeFileWriter.onerror) {
|
||||||
|
fakeFileWriter.onerror(new Error('Blob not supported by browser'));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
blobParts.push(blob);
|
blobParts.push(blob);
|
||||||
$timeout(function () {
|
$timeout(function () {
|
||||||
if (fakeFileWriter.onwriteend) {
|
if (fakeFileWriter.onwriteend) {
|
||||||
@ -224,16 +261,7 @@ angular.module('izhukov.utils', [])
|
|||||||
blobParts = [];
|
blobParts = [];
|
||||||
},
|
},
|
||||||
finalize: function () {
|
finalize: function () {
|
||||||
var blob;
|
var blob = blobConstruct(blobParts, mimeType);
|
||||||
try {
|
|
||||||
blob = new Blob(blobParts, {type: mimeType});
|
|
||||||
} catch (e) {
|
|
||||||
var bb = new BlobBuilder;
|
|
||||||
angular.forEach(blobParts, function(blobPart) {
|
|
||||||
bb.append(blobPart);
|
|
||||||
});
|
|
||||||
blob = bb.getBlob(mimeType);
|
|
||||||
}
|
|
||||||
if (saveFileCallback) {
|
if (saveFileCallback) {
|
||||||
saveFileCallback(blob);
|
saveFileCallback(blob);
|
||||||
}
|
}
|
||||||
@ -275,6 +303,7 @@ angular.module('izhukov.utils', [])
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
isAvailable: isBlobAvailable,
|
||||||
copy: fileCopyTo,
|
copy: fileCopyTo,
|
||||||
write: fileWriteData,
|
write: fileWriteData,
|
||||||
getFileWriter: getFileWriter,
|
getFileWriter: getFileWriter,
|
||||||
|
@ -108,3 +108,30 @@ function listUniqSorted (list) {
|
|||||||
return resultList;
|
return resultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Bind polyfill from MDN
|
||||||
|
if (!Function.prototype.bind) {
|
||||||
|
Function.prototype.bind = function (oThis) {
|
||||||
|
if (typeof this !== "function") {
|
||||||
|
// closest thing possible to the ECMAScript 5
|
||||||
|
// internal IsCallable function
|
||||||
|
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
|
||||||
|
}
|
||||||
|
|
||||||
|
var aArgs = Array.prototype.slice.call(arguments, 1),
|
||||||
|
fToBind = this,
|
||||||
|
fNOP = function () {},
|
||||||
|
fBound = function () {
|
||||||
|
return fToBind.apply(this instanceof fNOP && oThis
|
||||||
|
? this
|
||||||
|
: oThis,
|
||||||
|
aArgs.concat(Array.prototype.slice.call(arguments)));
|
||||||
|
};
|
||||||
|
|
||||||
|
fNOP.prototype = this.prototype;
|
||||||
|
fBound.prototype = new fNOP();
|
||||||
|
|
||||||
|
return fBound;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user