@@ -10,7 +10,9 @@ var appsApi = {},
10
10
moment = require ( 'moment-timezone' ) ,
11
11
crypto = require ( 'crypto' ) ,
12
12
plugins = require ( '../../../plugins/pluginManager.js' ) ,
13
- fs = require ( 'fs' ) ;
13
+ fs = require ( 'fs' ) ,
14
+ jimp = require ( 'jimp' ) ,
15
+ countlyFs = require ( './../../utils/countlyFs.js' ) ;
14
16
15
17
/**
16
18
* Get all apps and outputs to browser, requires global admin permission
@@ -150,6 +152,40 @@ appsApi.getAppsDetails = function(params) {
150
152
151
153
return true ;
152
154
} ;
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
+ } ;
153
189
154
190
/**
155
191
* Creates new app, and outputs result to browser
@@ -232,6 +268,7 @@ appsApi.createApp = function(params) {
232
268
appId : app . ops [ 0 ] . _id ,
233
269
data : newApp
234
270
} ) ;
271
+ iconUpload ( Object . assign ( { } , params , { app_id : app . ops [ 0 ] . _id } ) ) ;
235
272
common . returnOutput ( params , newApp ) ;
236
273
} ) ;
237
274
} ;
@@ -317,6 +354,7 @@ appsApi.updateApp = function(params) {
317
354
update : updatedApp
318
355
}
319
356
} ) ;
357
+ iconUpload ( params ) ;
320
358
common . returnOutput ( params , updatedApp ) ;
321
359
} ) ;
322
360
}
@@ -332,6 +370,7 @@ appsApi.updateApp = function(params) {
332
370
update : updatedApp
333
371
}
334
372
} ) ;
373
+ iconUpload ( params ) ;
335
374
common . returnOutput ( params , updatedApp ) ;
336
375
} ) ;
337
376
}
0 commit comments