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
This commit is contained in:
Pavle Goloskokovic 2017-10-12 16:20:42 +02:00
parent a7cf674323
commit a0c1d129ce
3 changed files with 40 additions and 37 deletions

View file

@ -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;

View file

@ -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');
}

View file

@ -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');
}
});