@@ -23,13 +23,17 @@ module.exports = function(RED) {
2323 'createCollection' : [ 'name' , 'description' ] ,
2424 'getCollection' : [ 'collectionId' ] ,
2525 'updateCollection' : [ 'collectionId' ] ,
26- 'deleteCollection' : [ 'collectionId' ]
26+ 'deleteCollection' : [ 'collectionId' ] ,
27+ 'addImages' : [ 'collectionId' ]
2728 } ;
2829
2930 var pkg = require ( '../../package.json' ) ,
3031 serviceutils = require ( '../../utilities/service-utils' ) ,
3132 payloadutils = require ( '../../utilities/payload-utils' ) ,
3233 temp = require ( 'temp' ) ,
34+ fileType = require ( 'file-type' ) ,
35+ fs = require ( 'fs' ) ,
36+ fsp = require ( 'fs' ) . promises ,
3337 sAPIKey = null ,
3438 apikey = '' ,
3539 service = null ,
@@ -238,6 +242,7 @@ module.exports = function(RED) {
238242 case 'getCollection' :
239243 case 'updateCollection' :
240244 case 'deleteCollection' :
245+ case 'addImages' :
241246 theMissing = paramCheckFor ( REQUIRED_PARAMS [ feature ] , msg ) ;
242247 if ( theMissing . length === 0 ) {
243248 return Promise . resolve ( ) ;
@@ -257,6 +262,72 @@ module.exports = function(RED) {
257262 }
258263 }
259264
265+ function bufferCheck ( data ) {
266+ return data instanceof Buffer ;
267+ }
268+
269+ function zipCheck ( msg ) {
270+ return new Promise ( function resolver ( resolve , reject ) {
271+ if ( ! bufferCheck ( msg . payload ) ) {
272+ reject ( 'msg.payload is neither an array or urls or a zip' ) ;
273+ } else {
274+ let ft = fileType ( msg . payload ) ;
275+ if ( ! ft || ! ft . ext || 'zip' != ft . ext ) {
276+ reject ( 'msg.payload is not a zip' ) ;
277+ } else {
278+ resolve ( ) ;
279+ }
280+ }
281+ } ) ;
282+ }
283+
284+ function setImagesFileParam ( msg ) {
285+ return new Promise ( function resolver ( resolve , reject ) {
286+ temp . open ( {
287+ suffix : '.' + 'zip'
288+ } , function ( err , info ) {
289+ if ( err ) {
290+ reject ( 'Node has been unable to open the zip stream' ) ;
291+ }
292+ fsp . writeFile ( info . path , msg . payload )
293+ . then ( ( ) => {
294+ msg . params [ 'imagesFile' ] = fs . createReadStream ( info . path ) ;
295+ resolve ( ) ;
296+ } )
297+ . catch ( ( err ) => {
298+ reject ( err ) ;
299+ } )
300+ } ) ;
301+ } ) ;
302+ }
303+
304+ function processPayload ( node , msg ) {
305+ return new Promise ( function resolver ( resolve , reject ) {
306+ if ( 'addImages' !== node . config [ FEATURE ] ) {
307+ resolve ( ) ;
308+ } else if ( Array . isArray ( msg . payload ) ) {
309+ // Payload can be either an array of urls for images
310+ msg . params [ 'imageUrl' ] = msg . payload ;
311+ resolve ( ) ;
312+ } else {
313+ // or a zip of images
314+ payloadutils . checkForStream ( msg )
315+ . then ( ( ) => {
316+ return zipCheck ( msg ) ;
317+ } )
318+ . then ( ( ) => {
319+ return setImagesFileParam ( msg ) ;
320+ } )
321+ . then ( ( ) => {
322+ resolve ( ) ;
323+ } )
324+ . catch ( ( err ) => {
325+ reject ( err ) ;
326+ } ) ;
327+ }
328+ } ) ;
329+ }
330+
260331 function verifyPayload ( node , msg ) {
261332 switch ( node . config [ FEATURE ] ) {
262333 case 'createCollection' :
@@ -266,8 +337,13 @@ module.exports = function(RED) {
266337 case 'deleteCollection' :
267338 case 'deleteAllCollections' :
268339 return Promise . resolve ( ) ;
340+ case 'addImages' :
341+ if ( ! msg . payload ) {
342+ return Promise . reject ( 'Missing property: msg.payload' ) ;
343+ }
344+ return Promise . resolve ( ) ;
269345 default :
270- return Promise . reject ( 'Missing property: msg.payload ' ) ;
346+ return Promise . reject ( 'Unknown mode has been specified ' ) ;
271347 }
272348 }
273349
@@ -294,6 +370,9 @@ module.exports = function(RED) {
294370 . then ( ( ) => {
295371 return verifyParams ( node , msg ) ;
296372 } )
373+ . then ( ( ) => {
374+ return processPayload ( node , msg ) ;
375+ } )
297376 . then ( ( ) => {
298377 return determineEndpoint ( node . config ) ;
299378 } )
0 commit comments