| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Node.js core.
 | 
					
						
							|  |  |  | const crypto = require('crypto'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const fs = require('fs-extra'); | 
					
						
							|  |  |  | const io = require('socket.io-client'); | 
					
						
							|  |  |  | const request = require('request'); | 
					
						
							|  |  |  | const RSA = require('node-rsa'); | 
					
						
							|  |  |  | const stringify = require('json-stringify-safe'); | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  | const unzip = require('unzip2'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |  * Studio hook | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  | module.exports = function (strapi) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |   const hook = { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Default options | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     defaults: { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |       studio: { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         enabled: true, | 
					
						
							|  |  |  |         secretKey: '' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Initialize the hook | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     initialize: function (cb) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       const _self = this; | 
					
						
							|  |  |  |       let firstConnectionAttempt = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 09:51:57 +02:00
										 |  |  |       if (_.isPlainObject(strapi.config.studio) && !_.isEmpty(strapi.config.studio) && strapi.config.studio.enabled === true) { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |         const manager = io.Manager(strapi.config.studio.url, { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           reconnectionDelay: 2000, | 
					
						
							|  |  |  |           reconnectionDelayMax: 5000, | 
					
						
							|  |  |  |           reconnectionAttempts: 5 | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |         const socket = io.connect(strapi.config.studio.url, { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           'reconnection': true, | 
					
						
							|  |  |  |           'force new connection': true, | 
					
						
							|  |  |  |           'transports': [ | 
					
						
							|  |  |  |             'polling', | 
					
						
							|  |  |  |             'websocket', | 
					
						
							|  |  |  |             'htmlfile', | 
					
						
							|  |  |  |             'xhr-polling', | 
					
						
							|  |  |  |             'jsonp-polling' | 
					
						
							|  |  |  |           ] | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         manager.on('connect_failed', function () { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           if (firstConnectionAttempt) { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |             strapi.log.warn('Connection to the Studio server failed!'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         manager.on('reconnect_failed', function () { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           strapi.log.warn('Reconnection to the Studio server failed!'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         manager.on('reconnect', function () { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           strapi.log.info('Connection to the Studio server found, please wait a few seconds...'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         manager.on('reconnecting', function (number) { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           strapi.log.warn('Connection error with the Studio server, new attempt in progress... (' + number + ')'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('connect', function () { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           strapi.log.info('Connection with the Studio server found, please wait a few seconds...'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           firstConnectionAttempt = false; | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           _self.connectWithStudio(socket); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('error', function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           strapi.log.warn(err); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('disconnect', function () { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           strapi.log.info('Disconnected from the Studio server.'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('authorized', function (data) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           const decryptedData = strapi.rsa.decryptPublic(data, 'json'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (decryptedData.status === 'ok') { | 
					
						
							|  |  |  |             if (strapi.config.environment === 'development') { | 
					
						
							|  |  |  |               socket.emit('testEncryption', { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                 appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 token: strapi.token, | 
					
						
							|  |  |  |                 encrypted: strapi.rsa.encrypt({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   secretKey: strapi.config.studio.secretKey, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   data: 'ok' | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |               }, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 if (err) { | 
					
						
							|  |  |  |                   strapi.log.warn(err); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                 strapi.log.info('Connected with the Studio server.'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |               }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               socket.emit('testEncryption', { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                 appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 env: strapi.config.environment, | 
					
						
							|  |  |  |                 encrypted: strapi.rsa.encrypt({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   secretKey: strapi.config.studio.secretKey, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   data: 'ok' | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |               }, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 if (err) { | 
					
						
							|  |  |  |                   strapi.log.warn(err); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                 strapi.log.info('Connected with the Studio server.'); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |               }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('todo', function (data, fn) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           if (!data.hasOwnProperty('from') || !data.hasOwnProperty('to')) { | 
					
						
							|  |  |  |             fn(stringify('Some required attributes are missing', null, 2), null); | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           } else if (data.from === strapi.token) { | 
					
						
							|  |  |  |             if (data.hasOwnProperty('files')) { | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  |               const syncPromise = function (file, index) { | 
					
						
							|  |  |  |                 const deferred = Promise.defer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 _self.unzipper(file) | 
					
						
							|  |  |  |                   .then(function () { | 
					
						
							|  |  |  |                     if (!_.isEmpty(data.files[index + 1])) { | 
					
						
							|  |  |  |                       return syncPromise(data.files[index + 1], index + 1); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                       deferred.resolve(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                   }) | 
					
						
							|  |  |  |                   .then(function () { | 
					
						
							|  |  |  |                     deferred.resolve(); | 
					
						
							|  |  |  |                   }) | 
					
						
							|  |  |  |                   .catch(function (err) { | 
					
						
							|  |  |  |                     deferred.reject(err); | 
					
						
							|  |  |  |                   }); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  |                 return deferred.promise; | 
					
						
							|  |  |  |               }; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  |               syncPromise(_.first(data.files), 0) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                 .then(function () { | 
					
						
							|  |  |  |                   if (data.hasOwnProperty('action') && _.isFunction(_self[data.action])) { | 
					
						
							|  |  |  |                     _self[data.action](data, function (err, obj) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                       if (err) { | 
					
						
							|  |  |  |                         fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                           appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                           token: strapi.token, | 
					
						
							|  |  |  |                           encrypted: strapi.rsa.encrypt({ | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                             err: stringify(err, null, 2), | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                             data: null | 
					
						
							|  |  |  |                           }) | 
					
						
							|  |  |  |                         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         return false; | 
					
						
							|  |  |  |                       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                       fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                         appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                         token: strapi.token, | 
					
						
							|  |  |  |                         encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                           err: null, | 
					
						
							|  |  |  |                           data: stringify(obj, null, 2) | 
					
						
							|  |  |  |                         }) | 
					
						
							|  |  |  |                       }); | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                   } else if (!data.hasOwnProperty('action')) { | 
					
						
							|  |  |  |                     fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                       appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                       token: strapi.token, | 
					
						
							|  |  |  |                       encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                         err: null, | 
					
						
							|  |  |  |                         data: stringify(true, null, 2) | 
					
						
							|  |  |  |                       }) | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   } else { | 
					
						
							|  |  |  |                     fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                       appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                       token: strapi.token, | 
					
						
							|  |  |  |                       encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                         err: stringify('Unknow action', null, 2), | 
					
						
							|  |  |  |                         data: null | 
					
						
							|  |  |  |                       }) | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  |                   } | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                 }) | 
					
						
							|  |  |  |                 .catch(function (err) { | 
					
						
							|  |  |  |                   fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                     appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                     token: strapi.token, | 
					
						
							|  |  |  |                     encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                       err: err, | 
					
						
							|  |  |  |                       data: null | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                   }); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } else if (!data.hasOwnProperty('action')) { | 
					
						
							|  |  |  |               fn(strapi.rsa.encrypt(stringify('`action` attribute is missing', null, 2)), strapi.rsa.encryptPrivate(null)); | 
					
						
							|  |  |  |             } else if (_.isFunction(_self[data.action])) { | 
					
						
							|  |  |  |               _self[data.action](data, function (err, obj) { | 
					
						
							|  |  |  |                 if (err) { | 
					
						
							|  |  |  |                   fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                     appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                     token: strapi.token, | 
					
						
							|  |  |  |                     encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                       err: err, | 
					
						
							|  |  |  |                       data: null | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                   return false; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                   token: strapi.token, | 
					
						
							|  |  |  |                   encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                     err: null, | 
					
						
							|  |  |  |                     data: stringify(obj, null, 2) | 
					
						
							|  |  |  |                   }) | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |               }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               fn({ | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                 appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                 token: strapi.token, | 
					
						
							|  |  |  |                 encrypted: strapi.rsa.encrypt({ | 
					
						
							|  |  |  |                   err: stringify('Unknow action', null, 2), | 
					
						
							|  |  |  |                   data: null | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |               }); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           } else { | 
					
						
							|  |  |  |             fn(stringify('Bad user token', null, 2), null); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         socket.on('err', function (data) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           strapi.log.warn(data.text); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 09:51:57 +02:00
										 |  |  |         cb(); | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         cb(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |     connectWithStudio: function (socket) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       strapi.rsa = new RSA({ | 
					
						
							|  |  |  |         b: 2048 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       fs.readFile(path.resolve(process.env[process.platform === 'win32' ? 'USERPROFILE' : 'HOME'], '.strapirc'), { | 
					
						
							|  |  |  |         encoding: 'utf8' | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |       }, function (err, config) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         if (err) { | 
					
						
							|  |  |  |           strapi.log.warn('Continuing without credentials.'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           config = JSON.parse(config); | 
					
						
							|  |  |  |           strapi.token = config.token; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           socket.emit('getPublicKey', null, function (publicKey) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             if (publicKey) { | 
					
						
							|  |  |  |               const key = new RSA(); | 
					
						
							|  |  |  |               let object; | 
					
						
							|  |  |  |               key.importKey(publicKey, 'public'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               if (strapi.config.environment === 'development') { | 
					
						
							|  |  |  |                 object = { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   appName: strapi.config.name, | 
					
						
							|  |  |  |                   publicKey: strapi.rsa.exportKey('private'), | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   secretKey: strapi.config.studio.secretKey, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   token: strapi.token, | 
					
						
							|  |  |  |                   env: strapi.config.environment | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |               } else { | 
					
						
							|  |  |  |                 object = { | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   appId: strapi.config.studio.appId, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   appName: strapi.config.name, | 
					
						
							|  |  |  |                   publicKey: strapi.rsa.exportKey('private'), | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |                   secretKey: strapi.config.studio.secretKey, | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                   env: strapi.config.environment | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 18:00:05 +02:00
										 |  |  |               socket.emit('check', key.encrypt(object)); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Subaction for config | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     handleConfig: function (data, cb) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       strapi.log.warn('We need to flush server.'); | 
					
						
							|  |  |  |       strapi.log.warn('Install dependencies if we have to.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cb(null, true); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Pull global strapi variable from local server | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     pullServer: function (data, cb) { | 
					
						
							|  |  |  |       const obj = {}; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       obj.token = strapi.token; | 
					
						
							|  |  |  |       obj.config = strapi.config; | 
					
						
							|  |  |  |       obj.models = strapi.models; | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |       obj.api = strapi.api; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       obj.templates = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cb(null, obj); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Rebuild dictionary | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     rebuild: function (data, cb) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       strapi.rebuild(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cb(null, true); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Remove file or folder | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     removeFileOrFolder: function (data, cb) { | 
					
						
							|  |  |  |       const arrayOfPromises = []; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data.hasOwnProperty('toRemove') && _.isArray(data.toRemove)) { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         const toRemove = function (path) { | 
					
						
							|  |  |  |           const deferred = Promise.defer(); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           fs.exists(path, function (exists) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             if (exists) { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |               fs.remove(path, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 if (err) { | 
					
						
							|  |  |  |                   deferred.reject(err); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                   deferred.resolve(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               deferred.reject('Unknow path `' + path + '`'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return deferred.promise; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         _.forEach(data.toRemove, function (fileOrFolder) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           arrayOfPromises.push(toRemove(fileOrFolder.path)); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Promise.all(arrayOfPromises) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           .then(function () { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             cb(null, true); | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           .catch(function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             cb(err, null); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         cb('Attribute `toRemove` is missing or is not an array', null); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Rename file or folder | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     renameFileOrFolder: function (data, cb) { | 
					
						
							|  |  |  |       const arrayOfPromises = []; | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data.hasOwnProperty('toRename') && _.isArray(data.toRename)) { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         const toRename = function (oldPath, newPath) { | 
					
						
							|  |  |  |           const deferred = Promise.defer(); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           fs.exists(oldPath, function (exists) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             if (exists) { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |               fs.copy(oldPath, newPath, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 if (err) { | 
					
						
							|  |  |  |                   deferred.reject(err); | 
					
						
							|  |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |                   fs.remove(oldPath, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                     if (err) { | 
					
						
							|  |  |  |                       deferred.reject(err); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     deferred.resolve(); | 
					
						
							|  |  |  |                   }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |               deferred.reject('Unknow path `' + path + '`'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return deferred.promise; | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         _.forEach(data.toRename, function (fileOrFolder) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           arrayOfPromises.push(toRename(fileOrFolder.oldPath, fileOrFolder.newPath)); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Promise.all(arrayOfPromises) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           .then(function () { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             cb(null, true); | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           .catch(function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             cb(err, null); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         cb('Attribute `toRename` is missing or is not an array', null); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Function to unzip file or folder to specific folder | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param {Object} data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return {Function} cb | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |     unzipper: function (data) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |       const deferred = Promise.defer(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |       crypto.pbkdf2(data.token, strapi.token, 4096, 32, 'sha256', function (err, derivedKey) { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           deferred.reject(err); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const fileToUnzip = path.resolve(strapi.config.appPath, '.tmp', derivedKey.toString('hex') + '.zip'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request({ | 
					
						
							|  |  |  |           method: 'POST', | 
					
						
							|  |  |  |           preambleCRLF: true, | 
					
						
							|  |  |  |           postambleCRLF: true, | 
					
						
							|  |  |  |           json: true, | 
					
						
							| 
									
										
										
										
											2015-10-06 16:13:00 +02:00
										 |  |  |           uri: strapi.config.studio.url + '/socket/download', | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           encoding: null, | 
					
						
							|  |  |  |           body: { | 
					
						
							|  |  |  |             token: strapi.token, | 
					
						
							|  |  |  |             fileId: data.token, | 
					
						
							|  |  |  |             src: data.src | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .pipe(fs.createWriteStream(fileToUnzip)) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         .on('close', function () { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           let folderDest; | 
					
						
							|  |  |  |           const folderOrFiletoRemove = path.resolve(data.dest); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-06 12:13:46 +02:00
										 |  |  |           if (data.src === 'api') { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             folderDest = folderOrFiletoRemove; | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             folderDest = path.resolve(data.dest, '..'); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |           fs.remove(folderOrFiletoRemove, function (err) { | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |             if (err) { | 
					
						
							|  |  |  |               deferred.reject(err); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  |             _.defer(function () { | 
					
						
							|  |  |  |               fs.createReadStream(fileToUnzip).pipe(unzip.Extract({ | 
					
						
							|  |  |  |                 path: folderDest | 
					
						
							|  |  |  |               })) | 
					
						
							|  |  |  |                 .on('close', function () { | 
					
						
							|  |  |  |                   fs.remove(fileToUnzip, function (err) { | 
					
						
							|  |  |  |                     if (err) { | 
					
						
							|  |  |  |                       deferred.reject(err); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     deferred.resolve(); | 
					
						
							|  |  |  |                   }); | 
					
						
							|  |  |  |                 }).on('error', function (err) { | 
					
						
							|  |  |  |                   deferred.reject(err); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2015-10-08 12:44:26 +02:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |           }); | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2015-10-05 18:29:04 +02:00
										 |  |  |         .on('error', function () { | 
					
						
							|  |  |  |           deferred.reject('Download ZIP or unzip not worked fine', null); | 
					
						
							| 
									
										
										
										
											2015-10-01 00:30:16 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return deferred.promise; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return hook; | 
					
						
							|  |  |  | }; |