Further improved memory footprint in post upload
This commit is contained in:
parent
5f4260d0a7
commit
aa228d5125
1 changed files with 33 additions and 40 deletions
|
@ -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 img = new Image();
|
|
||||||
img.src = dataURL;
|
|
||||||
img.onload = function() {
|
|
||||||
var canvas = document.createElement('canvas');
|
var canvas = document.createElement('canvas');
|
||||||
var context = canvas.getContext('2d');
|
var img = new Image();
|
||||||
canvas.width = thumbnailWidth;
|
canvas.width = thumbnailWidth;
|
||||||
canvas.height = thumbnailHeight;
|
canvas.height = thumbnailHeight;
|
||||||
context.drawImage(
|
var context = canvas.getContext('2d');
|
||||||
img,
|
img.onload = function() {
|
||||||
0,
|
//memory still leaks...
|
||||||
0,
|
img.onload = null;
|
||||||
canvas.width,
|
context.drawImage(img, 0, 0, thumbnailWidth, thumbnailHeight);
|
||||||
canvas.height);
|
URL.revokeObjectURL(img.src);
|
||||||
|
img.src = null;
|
||||||
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) {
|
||||||
fileDropper.readAsDataURL(file, function(contentDataURL) {
|
if (!file.type.match('image.*')) {
|
||||||
if (thumbnailWidth === null || thumbnailHeight === null) {
|
resolve(null);
|
||||||
resolve(contentDataURL);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
makeThumbnailFromDataURL(contentDataURL, thumbnailWidth, thumbnailHeight)
|
if (thumbnailWidth === null || thumbnailHeight === null) {
|
||||||
|
resolve(URL.createObjectURL(post.file));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
makeThumbnail(thumbnailWidth, thumbnailHeight, post.file)
|
||||||
.then(function(thumbnailDataURL) {
|
.then(function(thumbnailDataURL) {
|
||||||
resolve(thumbnailDataURL);
|
resolve(thumbnailDataURL);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return promise.makeSilent(function(resolve, reject) {
|
|
||||||
resolve(null);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue