Further improved memory footprint in post upload

This commit is contained in:
Marcin Kurczewski 2015-03-21 08:16:02 +01:00
parent 5f4260d0a7
commit aa228d5125

View file

@ -230,24 +230,22 @@ App.Presenters.PostUploadPresenter = function(
stopUpload(); stopUpload();
} }
function makeThumbnailFromDataURL(dataURL, thumbnailWidth, thumbnailHeight) { function makeThumbnail(thumbnailWidth, thumbnailHeight, file) {
return promise.makeSilent(function(resolve, reject) { return promise.makeSilent(function(resolve, reject) {
var canvas = document.createElement('canvas');
var img = new Image(); var img = new Image();
img.src = dataURL; canvas.width = thumbnailWidth;
canvas.height = thumbnailHeight;
var context = canvas.getContext('2d');
img.onload = function() { img.onload = function() {
var canvas = document.createElement('canvas'); //memory still leaks...
var context = canvas.getContext('2d'); img.onload = null;
canvas.width = thumbnailWidth; context.drawImage(img, 0, 0, thumbnailWidth, thumbnailHeight);
canvas.height = thumbnailHeight; URL.revokeObjectURL(img.src);
context.drawImage( img.src = null;
img,
0,
0,
canvas.width,
canvas.height);
resolve(canvas.toDataURL()); resolve(canvas.toDataURL());
canvas = null;
}; };
img.src = URL.createObjectURL(file);
}); });
} }
@ -256,24 +254,20 @@ App.Presenters.PostUploadPresenter = function(
fileName: file.name, fileName: file.name,
file: file, file: file,
getThumbnail: function(thumbnailWidth, thumbnailHeight) { getThumbnail: function(thumbnailWidth, thumbnailHeight) {
if (file.type.match('image.*')) { return promise.makeSilent(function(resolve, reject) {
return promise.makeSilent(function(resolve, reject) { if (!file.type.match('image.*')) {
fileDropper.readAsDataURL(file, function(contentDataURL) {
if (thumbnailWidth === null || thumbnailHeight === null) {
resolve(contentDataURL);
return;
}
makeThumbnailFromDataURL(contentDataURL, thumbnailWidth, thumbnailHeight)
.then(function(thumbnailDataURL) {
resolve(thumbnailDataURL);
});
});
});
} else {
return promise.makeSilent(function(resolve, reject) {
resolve(null); resolve(null);
}); return;
} }
if (thumbnailWidth === null || thumbnailHeight === null) {
resolve(URL.createObjectURL(post.file));
return;
}
makeThumbnail(thumbnailWidth, thumbnailHeight, post.file)
.then(function(thumbnailDataURL) {
resolve(thumbnailDataURL);
});
});
}, },
}); });
@ -335,16 +329,15 @@ App.Presenters.PostUploadPresenter = function(
function updatePostThumbnailInForm(post) { function updatePostThumbnailInForm(post) {
post.getThumbnail(null, null).then(function(thumbnailDataURL) { post.getThumbnail(null, null).then(function(thumbnailDataURL) {
var $thumbnail = $el.find('.form-slider .thumbnail');
var $img = $thumbnail.find('img');
if (thumbnailDataURL === null) { if (thumbnailDataURL === null) {
$el.find('.form-slider .thumbnail img').hide(); $img.hide();
} else { } else {
$el.find('.form-slider .thumbnail img').show()[0].setAttribute('src', thumbnailDataURL); $img.show();
$img.attr('src', thumbnailDataURL);
} }
$el.find('.form-slider .thumbnail a').attr( $el.find('.form-slider .thumbnail a').attr('href', thumbnailDataURL);
'href',
post.url !== null ?
thumbnailDataURL :
URL.createObjectURL(post.file));
}); });
} }
@ -352,9 +345,9 @@ App.Presenters.PostUploadPresenter = function(
post.getThumbnail(30, 30).then(function(thumbnailDataURL) { post.getThumbnail(30, 30).then(function(thumbnailDataURL) {
var $row = post.$tableRow; var $row = post.$tableRow;
if (thumbnailDataURL === null) { if (thumbnailDataURL === null) {
$row.find('img')[0].setAttribute('src', util.transparentPixel()); $row.find('img').attr('src', util.transparentPixel());
} else if ($row.find('img').attr('src') !== thumbnailDataURL) { } else {
$row.find('img')[0].setAttribute('src', thumbnailDataURL); $row.find('img').attr('src', thumbnailDataURL);
} }
}); });
} }