From a0c1d129cef087ea82c0ffca744b3c5cf608e2fe Mon Sep 17 00:00:00 2001 From: Pavle Goloskokovic Date: Thu, 12 Oct 2017 16:20:42 +0200 Subject: [PATCH] moved url generating logic into File class static methods applied it to image and svg file classes fixed issue with unnecessary calls to revokeObjectURL method in SVGFile class --- v3/src/loader/File.js | 30 ++++++++++++++++++++++++ v3/src/loader/filetypes/ImageFile.js | 34 +++------------------------- v3/src/loader/filetypes/SVGFile.js | 13 ++++++----- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/v3/src/loader/File.js b/v3/src/loader/File.js index 717c09cd6..f1ed559c7 100644 --- a/v3/src/loader/File.js +++ b/v3/src/loader/File.js @@ -162,4 +162,34 @@ var File = new Class({ }); +File.createObjectURL = function (data, response, defaultType) +{ + if(URL) + { + data.src = URL.createObjectURL(response); + } + else + { + var reader = new FileReader(); + + reader.onload = function() + { + delete data.crossOrigin; + data.src = 'data:' + (response.type || defaultType) + ';base64,' + reader.result.split(',')[1]; + }; + + reader.onerror = data.onerror; + + reader.readAsDataURL(response); + } +}; + +File.revokeObjectURL = function (data) +{ + if(URL) + { + URL.revokeObjectURL(data.src); + } +}; + module.exports = File; diff --git a/v3/src/loader/filetypes/ImageFile.js b/v3/src/loader/filetypes/ImageFile.js index 163a125cf..ffed0a1fc 100644 --- a/v3/src/loader/filetypes/ImageFile.js +++ b/v3/src/loader/filetypes/ImageFile.js @@ -56,10 +56,7 @@ var ImageFile = new Class({ this.data.onload = function () { - if(URL) - { - URL.revokeObjectURL(_this.data.src); - } + File.revokeObjectURL(_this.data); _this.onComplete(); @@ -68,39 +65,14 @@ var ImageFile = new Class({ this.data.onerror = function () { - if(URL) - { - URL.revokeObjectURL(_this.data.src); - } + File.revokeObjectURL(_this.data); _this.state = CONST.FILE_ERRORED; callback(_this); }; - if(URL) - { - this.data.src = URL.createObjectURL(this.xhrLoader.response); - } - else - { - var reader = new FileReader(); - - reader.onload = function() - { - delete _this.data.crossOrigin; - _this.data.src = 'data:' + (_this.xhrLoader.response.type || 'image/png') + ';base64,' + reader.result.split(',')[1]; - }; - - reader.onerror = function () - { - _this.state = CONST.FILE_ERRORED; - - callback(_this); - }; - - reader.readAsDataURL(this.xhrLoader.response); - } + File.createObjectURL(this.data, this.xhrLoader.response, 'image/png'); } diff --git a/v3/src/loader/filetypes/SVGFile.js b/v3/src/loader/filetypes/SVGFile.js index 2bdaa557f..79881c82d 100644 --- a/v3/src/loader/filetypes/SVGFile.js +++ b/v3/src/loader/filetypes/SVGFile.js @@ -56,7 +56,10 @@ var SVGFile = new Class({ this.data.onload = function () { - URL.revokeObjectURL(_this.data.src); + if(!retry) + { + File.revokeObjectURL(_this.data); + } _this.onComplete(); @@ -65,16 +68,14 @@ var SVGFile = new Class({ this.data.onerror = function () { - URL.revokeObjectURL(_this.data.src); - // Safari 8 re-try if (!retry) { retry = true; - var url = 'data:image/svg+xml,' + encodeURIComponent(svg.join('')); + File.revokeObjectURL(_this.data); - _this.data.src = URL.createObjectURL(url); + _this.data.src = 'data:image/svg+xml,' + encodeURIComponent(svg.join('')); } else { @@ -84,7 +85,7 @@ var SVGFile = new Class({ } }; - this.data.src = URL.createObjectURL(blob); + File.createObjectURL(this.data, blob, 'image/svg+xml'); } });