Skip to content

Commit 11351e7

Browse files
committed
Fix jimp usage
1 parent 9769f69 commit 11351e7

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

api/parts/mgmt/apps.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var appsApi = {},
1717
const taskmanager = require('./../../utils/taskmanager.js');
1818
const {timezoneValidation} = require('../../utils/timezones.js');
1919
const FEATURE_NAME = 'global_applications';
20+
2021
/**
2122
* Get all apps and outputs to browser, requires global admin permission
2223
* @param {params} params - params object
@@ -170,7 +171,7 @@ appsApi.getAppsDetails = function(params) {
170171
* @param {params} params - params object with args to create app
171172
* @return {object} return promise object;
172173
**/
173-
const iconUpload = function(params) {
174+
const iconUpload = async function(params) {
174175
const appId = params.app_id || common.sanitizeFilename(params.qstring.args.app_id);
175176
if (params.files && params.files.app_image) {
176177
const tmp_path = params.files.app_image.path,
@@ -183,25 +184,18 @@ const iconUpload = function(params) {
183184
return Promise.reject();
184185
}
185186
try {
186-
return jimp.read(tmp_path, function(err, icon) {
187-
if (err) {
188-
log.e(err, err.stack);
189-
fs.unlink(tmp_path, function() {});
190-
return true;
187+
const icon = await jimp.Jimp.read(tmp_path);
188+
const buffer = await icon.cover({h: 72, w: 72}).getBuffer(jimp.JimpMime.png);
189+
countlyFs.saveData("appimages", target_path, buffer, {id: appId + ".png", writeMode: "overwrite"}, function(err3) {
190+
if (err3) {
191+
log.e(err3, err3.stack);
191192
}
192-
icon.cover(72, 72).getBuffer(jimp.MIME_PNG, function(err2, buffer) {
193-
countlyFs.saveData("appimages", target_path, buffer, {id: appId + ".png", writeMode: "overwrite"}, function(err3) {
194-
if (err3) {
195-
log.e(err3, err3.stack);
196-
}
197-
fs.unlink(tmp_path, function() {});
198-
});
199-
});
200193
});
201194
}
202195
catch (e) {
203-
log.e(e.stack);
196+
console.log("Problem uploading app icon", e);
204197
}
198+
fs.unlink(tmp_path, function() {});
205199
}
206200
};
207201

frontend/express/app.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ Promise.all([plugins.dbConnection(countlyConfig), plugins.dbConnection("countly_
16061606
req.body.app_id = req.body.app_image_id;
16071607
}
16081608
var params = paramsGenerator({req, res});
1609-
validateCreate(params, 'global_upload', function() {
1609+
validateCreate(params, 'global_upload', async function() {
16101610
if (!req.session.uid && !req.body.app_image_id) {
16111611
res.end();
16121612
return false;
@@ -1630,25 +1630,18 @@ Promise.all([plugins.dbConnection(countlyConfig), plugins.dbConnection("countly_
16301630
}
16311631
plugins.callMethod("iconUpload", {req: req, res: res, next: next, data: req.body});
16321632
try {
1633-
jimp.read(tmp_path, function(err, icon) {
1634-
if (err) {
1635-
console.log(err, err.stack);
1636-
fs.unlink(tmp_path, function() {});
1637-
res.status(400).send(false);
1638-
return true;
1639-
}
1640-
icon.cover(72, 72).getBuffer(jimp.MIME_PNG, function(err2, buffer) {
1641-
countlyFs.saveData("appimages", target_path, buffer, {id: req.body.app_image_id + ".png", writeMode: "overwrite"}, function() {
1642-
fs.unlink(tmp_path, function() {});
1643-
res.send("appimages/" + req.body.app_image_id + ".png");
1644-
countlyDb.collection('apps').updateOne({_id: countlyDb.ObjectID(req.body.app_image_id)}, {'$set': {'has_image': true}}, function() {});
1645-
});
1646-
}); // save
1633+
const icon = await jimp.Jimp.read(tmp_path);
1634+
const buffer = await icon.cover({h: 72, w: 72}).getBuffer(jimp.JimpMime.png);
1635+
countlyFs.saveData("appimages", target_path, buffer, {id: req.body.app_image_id + ".png", writeMode: "overwrite"}, function() {
1636+
res.send("appimages/" + req.body.app_image_id + ".png");
1637+
countlyDb.collection('apps').updateOne({_id: countlyDb.ObjectID(req.body.app_image_id)}, {'$set': {'has_image': true}}, function() {});
16471638
});
16481639
}
16491640
catch (e) {
1650-
console.log(e.stack);
1641+
console.log("Problem uploading app icon", e);
1642+
res.status(400).send(false);
16511643
}
1644+
fs.unlink(tmp_path, function() {});
16521645
});
16531646
});
16541647

@@ -1690,23 +1683,19 @@ Promise.all([plugins.dbConnection(countlyConfig), plugins.dbConnection("countly_
16901683
}
16911684
plugins.callMethod("iconUpload", {req: req, res: res, next: next, data: req.body});
16921685
try {
1693-
jimp.read(tmp_path, function(err, icon) {
1694-
if (err) {
1695-
console.log(err, err.stack);
1696-
}
1697-
icon.cover(72, 72).getBuffer(jimp.MIME_PNG, function(err2, buffer) {
1698-
countlyFs.saveData("memberimages", target_path, buffer, {id: req.body.member_image_id + ".png", writeMode: "overwrite"}, function() {
1699-
fs.unlink(tmp_path, function() {});
1700-
countlyDb.collection('members').updateOne({_id: countlyDb.ObjectID(req.body.member_image_id + "")}, {'$set': {'member_image': "memberimages/" + req.body.member_image_id + ".png"}}, function() {
1701-
res.send("memberimages/" + req.body.member_image_id + ".png");
1702-
});
1703-
});
1704-
}); // save
1686+
const icon = await jimp.Jimp.read(tmp_path);
1687+
const buffer = await icon.cover({h: 72, w: 72}).getBuffer(jimp.JimpMime.png);
1688+
countlyFs.saveData("memberimages", target_path, buffer, {id: req.body.member_image_id + ".png", writeMode: "overwrite"}, function() {
1689+
countlyDb.collection('members').updateOne({_id: countlyDb.ObjectID(req.body.member_image_id + "")}, {'$set': {'member_image': "memberimages/" + req.body.member_image_id + ".png"}}, function() {
1690+
res.send("memberimages/" + req.body.member_image_id + ".png");
1691+
});
17051692
});
17061693
}
17071694
catch (e) {
1708-
console.log(e.stack);
1695+
console.log("Problem uploading member icon", e);
1696+
res.status(400).send(false);
17091697
}
1698+
fs.unlink(tmp_path, function() {});
17101699
});
17111700
});
17121701

0 commit comments

Comments
 (0)