From b17e242fc67f1164a5f7ca7df39df9a96512dc63 Mon Sep 17 00:00:00 2001 From: Bill Fienberg Date: Mon, 10 Sep 2018 06:40:32 -0500 Subject: [PATCH] ES6 migration: server/adapters/storage (#9700) refs #9589 - Replace vars with const/lets - Replace concatenated strings with ES6 Template Literals - Use ES6 object shorthand --- .../adapters/storage/LocalFileStorage.js | 41 +++++++++---------- core/server/adapters/storage/index.js | 22 +++++----- core/server/adapters/storage/utils.js | 17 ++++++-- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/core/server/adapters/storage/LocalFileStorage.js b/core/server/adapters/storage/LocalFileStorage.js index efff346263..da46eb9d09 100644 --- a/core/server/adapters/storage/LocalFileStorage.js +++ b/core/server/adapters/storage/LocalFileStorage.js @@ -1,7 +1,7 @@ // # Local File System Image Storage module // The (default) module for storing images, using the local file system -var serveStatic = require('express').static, +const serveStatic = require('express').static, fs = require('fs-extra'), path = require('path'), Promise = require('bluebird'), @@ -29,40 +29,39 @@ class LocalFileStore extends StorageBase { * @returns {*} */ save(image, targetDir) { - var targetFilename, - self = this; + let targetFilename; // NOTE: the base implementation of `getTargetDir` returns the format this.storagePath/YYYY/MM targetDir = targetDir || this.getTargetDir(this.storagePath); - return this.getUniqueFileName(image, targetDir).then(function (filename) { + return this.getUniqueFileName(image, targetDir).then((filename) => { targetFilename = filename; return fs.mkdirs(targetDir); - }).then(function () { + }).then(() => { return fs.copy(image.path, targetFilename); - }).then(function () { + }).then(() => { // The src for the image must be in URI format, not a file system path, which in Windows uses \ // For local file system storage can use relative path so add a slash - var fullUrl = ( + const fullUrl = ( urlService.utils.urlJoin('/', urlService.utils.getSubdir(), urlService.utils.STATIC_IMAGE_URL_PREFIX, - path.relative(self.storagePath, targetFilename)) - ).replace(new RegExp('\\' + path.sep, 'g'), '/'); + path.relative(this.storagePath, targetFilename)) + ).replace(new RegExp(`\\${path.sep}`, 'g'), '/'); return fullUrl; - }).catch(function (e) { + }).catch((e) => { return Promise.reject(e); }); } exists(fileName, targetDir) { - var filePath = path.join(targetDir || this.storagePath, fileName); + const filePath = path.join(targetDir || this.storagePath, fileName); return fs.stat(filePath) - .then(function () { + .then(() => { return true; }) - .catch(function () { + .catch(() => { return false; }); } @@ -75,21 +74,21 @@ class LocalFileStore extends StorageBase { * @returns {serveStaticContent} */ serve() { - var self = this; + const {storagePath} = this; return function serveStaticContent(req, res, next) { - var startedAtMoment = moment(); + const startedAtMoment = moment(); return serveStatic( - self.storagePath, + storagePath, { maxAge: constants.ONE_YEAR_MS, fallthrough: false, - onEnd: function onEnd() { + onEnd: () => { common.logging.info('LocalFileStorage.serve', req.path, moment().diff(startedAtMoment, 'ms') + 'ms'); } } - )(req, res, function (err) { + )(req, res, (err) => { if (err) { if (err.statusCode === 404) { return next(new common.errors.NotFoundError({ @@ -127,10 +126,10 @@ class LocalFileStore extends StorageBase { // remove trailing slashes options.path = (options.path || '').replace(/\/$|\\$/, ''); - var targetPath = path.join(this.storagePath, options.path); + const targetPath = path.join(this.storagePath, options.path); - return new Promise(function (resolve, reject) { - fs.readFile(targetPath, function (err, bytes) { + return new Promise((resolve, reject) => { + fs.readFile(targetPath, (err, bytes) => { if (err) { if (err.code === 'ENOENT') { return reject(new common.errors.NotFoundError({ diff --git a/core/server/adapters/storage/index.js b/core/server/adapters/storage/index.js index 23c589b6ad..b873531a05 100644 --- a/core/server/adapters/storage/index.js +++ b/core/server/adapters/storage/index.js @@ -1,4 +1,4 @@ -var _ = require('lodash'), +const _ = require('lodash'), StorageBase = require('ghost-storage-base'), config = require('../../config'), common = require('../../lib/common'), @@ -8,8 +8,8 @@ var _ = require('lodash'), * type: images */ function getStorage() { - var storageChoice = config.get('storage:active'), - storageConfig, + const storageChoice = config.get('storage:active'); + let storageConfig, CustomStorage, customStorage; @@ -29,7 +29,7 @@ function getStorage() { // CASE: load adapter from custom path (.../content/storage) try { - CustomStorage = require(config.getContentPath('storage') + storageChoice); + CustomStorage = require(`${config.getContentPath('storage')}${storageChoice}`); } catch (err) { if (err.message.match(/strict mode/gi)) { throw new common.errors.IncorrectUsageError({ @@ -38,33 +38,33 @@ function getStorage() { err: err }); // CASE: if module not found it can be an error within the adapter (cannot find bluebird for example) - } else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(config.getContentPath('storage') + storageChoice) === -1) { + } else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(`${config.getContentPath('storage')}${storageChoice}`) === -1) { throw new common.errors.IncorrectUsageError({ message: 'We have detected an error in your custom storage adapter.', - err: err + err }); // CASE: only throw error if module does exist } else if (err.code !== 'MODULE_NOT_FOUND') { throw new common.errors.IncorrectUsageError({ message: 'We have detected an unknown error in your custom storage adapter.', - err: err + err }); } } // CASE: check in the default storage path try { - CustomStorage = CustomStorage || require(config.get('paths').internalStoragePath + storageChoice); + CustomStorage = CustomStorage || require(`${config.get('paths').internalStoragePath}${storageChoice}`); } catch (err) { if (err.code === 'MODULE_NOT_FOUND') { throw new common.errors.IncorrectUsageError({ - err: err, - context: 'We cannot find your adapter in: ' + config.getContentPath('storage') + ' or: ' + config.get('paths').internalStoragePath + err, + context: `We cannot find your adapter in: ${config.getContentPath('storage')} or: ${config.get('paths').internalStoragePath}` }); } else { throw new common.errors.IncorrectUsageError({ message: 'We have detected an error in your custom storage adapter.', - err: err + err }); } } diff --git a/core/server/adapters/storage/utils.js b/core/server/adapters/storage/utils.js index d3b562c9c0..de2736b9e3 100644 --- a/core/server/adapters/storage/utils.js +++ b/core/server/adapters/storage/utils.js @@ -1,4 +1,4 @@ -var urlService = require('../../services/url'); +const urlService = require('../../services/url'); /** * @TODO: move `events.js` to here - e.g. storageUtils.getStorage @@ -15,8 +15,17 @@ var urlService = require('../../services/url'); */ exports.getLocalFileStoragePath = function getLocalFileStoragePath(imagePath) { // The '/' in urlJoin is necessary to add the '/' to `content/images`, if no subdirectory is setup - var urlRegExp = new RegExp('^' + urlService.utils.urlJoin(urlService.utils.urlFor('home', true), urlService.utils.getSubdir(), '/', urlService.utils.STATIC_IMAGE_URL_PREFIX)), - filePathRegExp = new RegExp('^' + urlService.utils.urlJoin(urlService.utils.getSubdir(), '/', urlService.utils.STATIC_IMAGE_URL_PREFIX)); + const urlRegExp = new RegExp(`^${urlService.utils.urlJoin( + urlService.utils.urlFor('home', true), + urlService.utils.getSubdir(), + '/', + urlService.utils.STATIC_IMAGE_URL_PREFIX)}` + ), + filePathRegExp = new RegExp(`^${urlService.utils.urlJoin( + urlService.utils.getSubdir(), + '/', + urlService.utils.STATIC_IMAGE_URL_PREFIX)}` + ); if (imagePath.match(urlRegExp)) { return imagePath.replace(urlRegExp, ''); @@ -34,7 +43,7 @@ exports.getLocalFileStoragePath = function getLocalFileStoragePath(imagePath) { */ exports.isLocalImage = function isLocalImage(imagePath) { - var localImagePath = this.getLocalFileStoragePath(imagePath); + const localImagePath = this.getLocalFileStoragePath(imagePath); if (localImagePath !== imagePath) { return true;