Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Limit image file size to 256kb #461

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions app/views/badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,30 +328,38 @@ function saveBadge(req, callback) {
var path = image.path;
var type = image.type;

// Need to determine acceptable mime types... this is just accepting everything right now.
fs.readFile(path, function(err, data) {
fs.stat(path, function (err, stats) {
if (err)
return innerCallback(err);

const imageQuery = {
id: badgeRow.imageId,
mimetype: type,
data: data,
url: null
};
if (stats.size > (256 * 1024))
return innerCallback(new Error('Maximum image size is 256 KB'));

Image.put(imageQuery, function(err, imageResult) {
// Need to determine acceptable mime types... this is just accepting everything right now.
fs.readFile(path, function(err, data) {
if (err)
return innerCallback(err);

if (badgeRow.imageId === null) {
Badge.put({ id: badgeRow.id, imageId: imageResult.insertId }, function(err, result) {
const imageQuery = {
id: badgeRow.imageId,
mimetype: type,
data: data,
url: null
};

Image.put(imageQuery, function(err, imageResult) {
if (err)
return innerCallback(err);
});
}
else {
return innerCallback(null);
}

if (badgeRow.imageId === null) {
Badge.put({ id: badgeRow.id, imageId: imageResult.insertId }, function(err, result) {
return innerCallback(err);
});
}
else {
return innerCallback(null);
}
});
});
});
}
Expand Down Expand Up @@ -387,7 +395,7 @@ function saveBadge(req, callback) {
exports.save = function save (req, res, next) {
saveBadge(req, function(err, row) {
if (err)
return res.send(500, err);
return res.send(500, err.message);

if (!('notification' in req.session)) {
req.session.notification = 'saved';
Expand Down