Skip to content

Commit 37360e0

Browse files
authored
Merge pull request #706 from waiterZen/SERVER-1067
upload icon support for app create & edit restful api
2 parents a6772e1 + 2f83010 commit 37360e0

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

api/parts/mgmt/apps.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ var appsApi = {},
1010
moment = require('moment-timezone'),
1111
crypto = require('crypto'),
1212
plugins = require('../../../plugins/pluginManager.js'),
13-
fs = require('fs');
13+
fs = require('fs'),
14+
jimp = require('jimp'),
15+
countlyFs = require('./../../utils/countlyFs.js');
1416

1517
/**
1618
* Get all apps and outputs to browser, requires global admin permission
@@ -150,6 +152,40 @@ appsApi.getAppsDetails = function(params) {
150152

151153
return true;
152154
};
155+
/**
156+
* upload app icon function
157+
* @param {params} params - params object with args to create app
158+
* @return {object} return promise object;
159+
**/
160+
const iconUpload = function(params) {
161+
const appId = params.app_id || params.qstring.args.app_id;
162+
if (params.files && params.files.app_image) {
163+
const tmp_path = params.files.app_image.path,
164+
target_path = __dirname + '/../../../frontend/express/public/appimages/' + appId + ".png",
165+
type = params.files.app_image.type;
166+
if (type !== "image/png" && type !== "image/gif" && type !== "image/jpeg") {
167+
fs.unlink(tmp_path, function() {});
168+
log.d("Invalid file type");
169+
return Promise.reject();
170+
}
171+
try {
172+
return jimp.read(tmp_path, function(err, icon) {
173+
if (err) {
174+
log.e(err, err.stack);
175+
}
176+
icon.cover(72, 72).getBuffer(jimp.MIME_PNG, function(err2, buffer) {
177+
countlyFs.saveData("appimages", target_path, buffer, {id: appId + ".png", writeMode: "overwrite"}, function(err3) {
178+
log.e(err3, err3.stack);
179+
fs.unlink(tmp_path, function() {});
180+
});
181+
});
182+
});
183+
}
184+
catch (e) {
185+
log.e(e.stack);
186+
}
187+
}
188+
};
153189

154190
/**
155191
* Creates new app, and outputs result to browser
@@ -232,6 +268,7 @@ appsApi.createApp = function(params) {
232268
appId: app.ops[0]._id,
233269
data: newApp
234270
});
271+
iconUpload(Object.assign({}, params, {app_id: app.ops[0]._id}));
235272
common.returnOutput(params, newApp);
236273
});
237274
};
@@ -317,6 +354,7 @@ appsApi.updateApp = function(params) {
317354
update: updatedApp
318355
}
319356
});
357+
iconUpload(params);
320358
common.returnOutput(params, updatedApp);
321359
});
322360
}
@@ -332,6 +370,7 @@ appsApi.updateApp = function(params) {
332370
update: updatedApp
333371
}
334372
});
373+
iconUpload(params);
335374
common.returnOutput(params, updatedApp);
336375
});
337376
}

0 commit comments

Comments
 (0)