diff --git a/core/server/adapters/storage/LocalFileStorage.js b/core/server/adapters/storage/LocalFileStorage.js index f2a0393a48..d10dd45233 100644 --- a/core/server/adapters/storage/LocalFileStorage.js +++ b/core/server/adapters/storage/LocalFileStorage.js @@ -29,27 +29,25 @@ class LocalFileStore extends StorageBase { /** * Saves a buffer in the targetPath - * - buffer is an instance of Buffer - * - returns a Promise which returns the full URL to retrieve the data + * @param {Buffer} buffer is an instance of Buffer + * @param {String} targetPath path to which the buffer should be written + * @returns {Promise} a URL to retrieve the data */ - saveRaw(buffer, targetPath) { + async saveRaw(buffer, targetPath) { const storagePath = path.join(this.storagePath, targetPath); const targetDir = path.dirname(storagePath); - return fs.mkdirs(targetDir) - .then(() => { - return fs.writeFile(storagePath, buffer); - }) - .then(() => { - // For local file system storage can use relative path so add a slash - const fullUrl = ( - urlUtils.urlJoin('/', urlUtils.getSubdir(), - urlUtils.STATIC_IMAGE_URL_PREFIX, - targetPath) - ).replace(new RegExp(`\\${path.sep}`, 'g'), '/'); + await fs.mkdirs(targetDir); + await fs.writeFile(storagePath, buffer); - return fullUrl; - }); + // For local file system storage can use relative path so add a slash + const fullUrl = ( + urlUtils.urlJoin('/', urlUtils.getSubdir(), + urlUtils.STATIC_IMAGE_URL_PREFIX, + targetPath) + ).replace(new RegExp(`\\${path.sep}`, 'g'), '/'); + + return fullUrl; } /** @@ -57,34 +55,32 @@ class LocalFileStore extends StorageBase { * - image is the express image object * - returns a promise which ultimately returns the full url to the uploaded image * - * @param image - * @param targetDir - * @returns {*} + * @param {StorageBase.Image} image + * @param {String} targetDir + * @returns {Promise} */ - save(image, targetDir) { + async save(image, targetDir) { 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((filename) => { - targetFilename = filename; - return fs.mkdirs(targetDir); - }).then(() => { - return fs.copy(image.path, targetFilename); - }).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 - const fullUrl = ( - urlUtils.urlJoin('/', urlUtils.getSubdir(), - urlUtils.STATIC_IMAGE_URL_PREFIX, - path.relative(this.storagePath, targetFilename)) - ).replace(new RegExp(`\\${path.sep}`, 'g'), '/'); + const filename = await this.getUniqueFileName(image, targetDir); - return fullUrl; - }).catch((e) => { - return Promise.reject(e); - }); + targetFilename = filename; + await fs.mkdirs(targetDir); + + await fs.copy(image.path, targetFilename); + + // 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 + const fullUrl = ( + urlUtils.urlJoin('/', urlUtils.getSubdir(), + urlUtils.STATIC_IMAGE_URL_PREFIX, + path.relative(this.storagePath, targetFilename)) + ).replace(new RegExp(`\\${path.sep}`, 'g'), '/'); + + return fullUrl; } exists(fileName, targetDir) { diff --git a/test/unit/server/adapters/storage/LocalFileStorage.test.js b/test/unit/server/adapters/storage/LocalFileStorage.test.js index 8f4aa67de0..6baabd01b8 100644 --- a/test/unit/server/adapters/storage/LocalFileStorage.test.js +++ b/test/unit/server/adapters/storage/LocalFileStorage.test.js @@ -6,12 +6,12 @@ const moment = require('moment'); const Promise = require('bluebird'); const path = require('path'); const LocalFileStore = require('../../../../../core/server/adapters/storage/LocalFileStorage'); -let localFileStore; const configUtils = require('../../../../utils/configUtils'); describe('Local File System Storage', function () { let image; let momentStub; + let localFileStore; function fakeDate(mm, yyyy) { const month = parseInt(mm, 10);