diff --git a/packages/strapi-plugin-upload/services/Upload.js b/packages/strapi-plugin-upload/services/Upload.js index c3fdc41d4a..c4d79d3441 100644 --- a/packages/strapi-plugin-upload/services/Upload.js +++ b/packages/strapi-plugin-upload/services/Upload.js @@ -7,10 +7,21 @@ */ const fs = require('fs'); +const crypto = require('crypto'); const _ = require('lodash'); const toArray = require('stream-to-array'); const uuid = require('uuid/v4'); +function niceHash(buffer) { + return crypto + .createHash('sha256') + .update(buffer) + .digest('base64') + .replace(/=/g, '') + .replace(/\//g, '-') + .replace(/\+/, '_'); +} + module.exports = { bufferize: async files => { if (_.isEmpty(files) === 0) { @@ -23,28 +34,22 @@ module.exports = { // transform all files in buffer return Promise.all( files.map(async stream => { - const parts = stream.path - ? await toArray(fs.createReadStream(stream.path)) - : await toArray(stream.stream); - + const parts = await toArray(fs.createReadStream(stream.path)); const buffers = parts.map( part => (_.isBuffer(part) ? part : Buffer.from(part)), ); - if (!stream.path) { - stream.name = stream.filename; - stream.type = stream.mimetype; - stream.size = parseInt(stream.encoding.replace('bit', '')); - } + const buffer = Buffer.concat(buffers); return { name: stream.name, + sha256: niceHash(buffer), hash: uuid().replace(/-/g, ''), ext: stream.name.split('.').length > 1 ? `.${_.last(stream.name.split('.'))}` : '', - buffer: Buffer.concat(buffers), + buffer, mime: stream.type, size: (stream.size / 1000).toFixed(2), }; diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index c073cfdad3..c830eca734 100755 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -111,27 +111,21 @@ class Strapi extends EventEmitter { // Update source admin. await admin.call(this); // Launch server. - this.server.listen(this.config.port, err => { + this.server.listen(this.config.port, async (err) => { if (err) { - this.log.debug('⚠️ Server wasn\'t able to start properly.'); + this.log.debug(`⚠️ Server wasn't able to start properly.`); this.log.error(err); return this.stop(); } this.log.info('Time: ' + new Date()); - this.log.info( - 'Launched in: ' + (Date.now() - this.config.launchedAt) + ' ms', - ); + this.log.info('Launched in: ' + (Date.now() - this.config.launchedAt) + ' ms'); this.log.info('Environment: ' + this.config.environment); this.log.info('Process PID: ' + process.pid); - this.log.info( - `Version: ${this.config.info.strapi} (node v${ - this.config.info.node - })`, - ); + this.log.info(`Version: ${this.config.info.strapi} (node v${this.config.info.node})`); this.log.info('To shut down your server, press + C at any time'); console.log(); - this.log.info(`☄️ Admin panel: ${this.config.url}/admin`); + this.log.info(`☄️ Admin panel: ${this.config.admin.url}`); this.log.info(`⚡️ Server: ${this.config.url}`); console.log(); @@ -141,11 +135,15 @@ class Strapi extends EventEmitter { if (cb && typeof cb === 'function') { cb(); } + + if (this.config.environment === 'development' && get(this.config.currentEnvironment, 'server.admin.autoOpen', true) !== false) { + await utils.openBrowser.call(this); + } }); } catch (err) { - this.log.debug('⛔️ Server wasn\'t able to start properly.'); + this.log.debug(`⛔️ Server wasn't able to start properly.`); this.log.error(err); - console.error(err); + console.log(err); this.stop(); } } @@ -157,17 +155,15 @@ class Strapi extends EventEmitter { const key = conn.remoteAddress + ':' + conn.remotePort; connections[key] = conn; - conn.on('close', function() { + conn.on('close', function () { delete connections[key]; }); }); this.server.on('error', err => { if (err.code === 'EADDRINUSE') { - this.log.debug('⛔️ Server wasn\'t able to start properly.'); - this.log.error( - `The port ${err.port} is already used by another application.`, - ); + this.log.debug(`⛔️ Server wasn't able to start properly.`); + this.log.error(`The port ${err.port} is already used by another application.`); this.stop(); return; } @@ -191,8 +187,7 @@ class Strapi extends EventEmitter { if ( cluster.isWorker && this.config.environment === 'development' && - get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === - true + get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === true ) { process.send('stop'); } @@ -231,10 +226,7 @@ class Strapi extends EventEmitter { await store.call(this); // Initialize hooks and middlewares. - await Promise.all([ - initializeMiddlewares.call(this), - initializeHooks.call(this), - ]); + await Promise.all([initializeMiddlewares.call(this), initializeHooks.call(this)]); // Harmonize plugins configuration. await plugins.call(this); @@ -245,7 +237,7 @@ class Strapi extends EventEmitter { shouldReload: 0 }; - const reload = function() { + const reload = function () { if (state.shouldReload > 0) { // Reset the reloading state state.shouldReload -= 1; @@ -256,11 +248,7 @@ class Strapi extends EventEmitter { if ( cluster.isWorker && this.config.environment === 'development' && - get( - this.config, - 'currentEnvironment.server.autoReload.enabled', - true, - ) === true + get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === true ) { process.send('reload'); } @@ -295,8 +283,8 @@ class Strapi extends EventEmitter { const timeoutMs = this.config.bootstrapTimeout || 3500; const timer = setTimeout(() => { this.log.warn( - `Bootstrap is taking unusually long to execute its callback ${timeoutMs} miliseconds).`, - ); + `Bootstrap is taking unusually long to execute its callback ${timeoutMs} miliseconds).`, + ); this.log.warn('Perhaps you forgot to call it?'); }, timeoutMs); @@ -305,9 +293,7 @@ class Strapi extends EventEmitter { try { fn(err => { if (ranBootstrapFn) { - this.log.error( - 'You called the callback in `strapi.config.boostrap` more than once!', - ); + this.log.error('You called the callback in `strapi.config.boostrap` more than once!'); return reject(); } @@ -319,9 +305,7 @@ class Strapi extends EventEmitter { }); } catch (e) { if (ranBootstrapFn) { - this.log.error( - 'The bootstrap function threw an error after its callback was called.', - ); + this.log.error('The bootstrap function threw an error after its callback was called.'); return reject(e); } @@ -334,9 +318,7 @@ class Strapi extends EventEmitter { }); return Promise.all( - Object.values(this.plugins).map(x => - execBootstrap(get(x, 'config.functions.bootstrap')), - ), + Object.values(this.plugins).map(x => execBootstrap(get(x, 'config.functions.bootstrap'))), ).then(() => execBootstrap(this.config.functions.bootstrap)); } @@ -356,16 +338,14 @@ class Strapi extends EventEmitter { query(entity, plugin) { if (!entity) { return this.log.error( - 'You can\'t call the query method without passing the model\'s name as a first argument.', + `You can't call the query method without passing the model's name as a first argument.`, ); } const model = entity.toLowerCase(); const Model = - get(strapi.plugins, [plugin, 'models', model]) || - get(strapi, ['models', model]) || - undefined; + get(strapi.plugins, [plugin, 'models', model]) || get(strapi, ['models', model]) || undefined; if (!Model) { return this.log.error(`The model ${model} can't be found.`); @@ -374,9 +354,7 @@ class Strapi extends EventEmitter { const connector = Model.orm; if (!connector) { - return this.log.error( - `Impossible to determine the use ORM for the model ${model}.`, - ); + return this.log.error(`Impossible to determine the use ORM for the model ${model}.`); } // Get stack trace. @@ -388,9 +366,7 @@ class Strapi extends EventEmitter { let pluginPath = undefined; if (file.indexOf('strapi-plugin-') !== -1) { - pluginPath = file - .split(path.sep) - .filter(x => x.indexOf('strapi-plugin-') !== -1)[0]; + pluginPath = file.split(path.sep).filter(x => x.indexOf('strapi-plugin-') !== -1)[0]; } else if (file.indexOf(path.sep + 'plugins' + path.sep) !== -1) { const pathTerms = file.split(path.sep); const index = pathTerms.indexOf('plugins'); @@ -401,22 +377,15 @@ class Strapi extends EventEmitter { } if (!pluginPath) { - return this.log.error( - 'Impossible to find the plugin where `strapi.query` has been called.', - ); + return this.log.error('Impossible to find the plugin where `strapi.query` has been called.'); } // Get plugin name. const pluginName = pluginPath.replace('strapi-plugin-', '').toLowerCase(); - const queries = get( - this.plugins, - `${pluginName}.config.queries.${connector}`, - ); + const queries = get(this.plugins, `${pluginName}.config.queries.${connector}`); if (!queries) { - return this.log.error( - `There is no query available for the model ${model}.`, - ); + return this.log.error(`There is no query available for the model ${model}.`); } // Bind queries with the current model to allow the use of `this`. @@ -427,7 +396,7 @@ class Strapi extends EventEmitter { { orm: connector, primaryKey: Model.primaryKey, - associations: Model.associations, + associations: Model.associations }, ); @@ -435,4 +404,4 @@ class Strapi extends EventEmitter { } } -module.exports = new Strapi(); +module.exports = new Strapi(); \ No newline at end of file