diff --git a/core/server/blog/app.js b/core/server/blog/app.js index 4fe945c15d..f104e9f41f 100644 --- a/core/server/blog/app.js +++ b/core/server/blog/app.js @@ -51,7 +51,7 @@ module.exports = function setupBlogApp() { // Serve robots.txt if not found in theme blogApp.use(serveSharedFile('robots.txt', 'text/plain', utils.ONE_HOUR_S)); // Serve blog images using the storage adapter - blogApp.use('/content/images', storage.getStorage().serve()); + blogApp.use('/' + utils.url.STATIC_IMAGE_URL_PREFIX, storage.getStorage().serve()); // Theme static assets/files blogApp.use(staticTheme()); diff --git a/core/server/config/overrides.json b/core/server/config/overrides.json index afab6a7547..c0a0b299f4 100644 --- a/core/server/config/overrides.json +++ b/core/server/config/overrides.json @@ -3,7 +3,6 @@ "appRoot": ".", "corePath": "core/", "clientAssets": "core/built/assets", - "imagesRelPath": "content/images", "helperTemplates": "core/server/helpers/tpl/", "adminViews": "core/server/views/", "internalAppPath": "core/server/apps/", diff --git a/core/server/config/utils.js b/core/server/config/utils.js index a60cd8263f..5067f27a11 100644 --- a/core/server/config/utils.js +++ b/core/server/config/utils.js @@ -16,7 +16,6 @@ exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) { /** * transform all relative paths to absolute paths - * @TODO: imagesRelPath is a dirty little attribute (especially when looking at the usages) * @TODO: re-write this function a little bit so we don't have to add the parent path - that is hard to understand * * Path must be string. @@ -45,8 +44,7 @@ exports.makePathsAbsolute = function makePathsAbsolute(obj, parent) { } else { if (_.isString(configValue) && (configValue.match(/\/+|\\+/) || configValue === '.') && - (configValue[0] !== '/' && configValue[0] !== '\\') && - pathsKey !== 'imagesRelPath' + (configValue[0] !== '/' && configValue[0] !== '\\') ) { self.set(parent + ':' + pathsKey, path.join(__dirname + '/../../../', configValue)); } diff --git a/core/server/data/importer/handlers/image.js b/core/server/data/importer/handlers/image.js index 01576252ae..b9bbed6b35 100644 --- a/core/server/data/importer/handlers/image.js +++ b/core/server/data/importer/handlers/image.js @@ -16,7 +16,7 @@ ImageHandler = { loadFile: function (files, baseDir) { var store = storage.getStorage(), baseDirRegex = baseDir ? new RegExp('^' + baseDir + '/') : new RegExp(''), - imageFolderRegexes = _.map(config.get('paths').imagesRelPath.split('/'), function (dir) { + imageFolderRegexes = _.map(utils.url.STATIC_IMAGE_URL_PREFIX.split('/'), function (dir) { return new RegExp('^' + dir + '/'); }); @@ -37,7 +37,7 @@ ImageHandler = { return Promise.map(files, function (image) { return store.getUniqueFileName(store, image, image.targetDir).then(function (targetFilename) { - image.newPath = utils.url.urlJoin('/', utils.url.getSubdir(), config.get('paths').imagesRelPath, + image.newPath = utils.url.urlJoin('/', utils.url.getSubdir(), utils.url.STATIC_IMAGE_URL_PREFIX, path.relative(config.getContentPath('images'), targetFilename)); return image; diff --git a/core/server/middleware/serve-favicon.js b/core/server/middleware/serve-favicon.js index d123ad99c0..51451d1a22 100644 --- a/core/server/middleware/serve-favicon.js +++ b/core/server/middleware/serve-favicon.js @@ -35,7 +35,7 @@ function serveFavicon() { // we are using an express route to skip /content/images and the result is a image path // based on config.getContentPath('images') + req.path // in this case we don't use path rewrite, that's why we have to make it manually - filePath = settingsCache.get('icon').replace(/\/content\/images\//, ''); + filePath = settingsCache.get('icon').replace(new RegExp(utils.url.STATIC_IMAGE_URL_PREFIX), ''); var originalExtension = path.extname(filePath).toLowerCase(), requestedExtension = path.extname(req.path).toLowerCase(); diff --git a/core/server/storage/local-file-store.js b/core/server/storage/local-file-store.js index dfcd772ce6..643449cf95 100644 --- a/core/server/storage/local-file-store.js +++ b/core/server/storage/local-file-store.js @@ -38,7 +38,7 @@ LocalFileStore.prototype.save = function save(image, targetDir) { // For local file system storage can use relative path so add a slash var fullUrl = ( utils.url.urlJoin('/', utils.url.getSubdir(), - config.get('paths').imagesRelPath, + utils.url.STATIC_IMAGE_URL_PREFIX, path.relative(config.getContentPath('images'), targetFilename)) ).replace(new RegExp('\\' + path.sep, 'g'), '/'); diff --git a/core/server/utils/url.js b/core/server/utils/url.js index 2a522bef01..4647965d29 100644 --- a/core/server/utils/url.js +++ b/core/server/utils/url.js @@ -7,7 +7,8 @@ var moment = require('moment-timezone'), config = require('./../config'), settingsCache = require('./../api/settings').cache, // @TODO: unify this with routes.apiBaseUrl - apiPath = '/ghost/api/v0.1'; + apiPath = '/ghost/api/v0.1', + STATIC_IMAGE_URL_PREFIX = 'content/images'; /** getBaseUrl * Returns the base URL of the blog as set in the config. If called with secure options, returns the ssl URL. @@ -231,7 +232,7 @@ function urlFor(context, data, absolute) { secure = data.author.secure; } else if (context === 'image' && data.image) { urlPath = data.image; - imagePathRe = new RegExp('^' + getSubdir() + '/' + config.get('paths').imagesRelPath); + imagePathRe = new RegExp('^' + getSubdir() + '/' + STATIC_IMAGE_URL_PREFIX); absolute = imagePathRe.test(data.image) ? absolute : false; secure = data.image.secure; @@ -328,3 +329,14 @@ module.exports.urlJoin = urlJoin; module.exports.urlFor = urlFor; module.exports.urlPathForPost = urlPathForPost; module.exports.apiUrl = apiUrl; + +/** + * If you request **any** image in Ghost, it get's served via + * http://your-blog.com/content/images/2017/01/02/author.png + * + * /content/images/ is a static prefix for serving images! + * + * But internally the image is located for example in your custom content path: + * my-content/another-dir/images/2017/01/02/author.png + */ +module.exports.STATIC_IMAGE_URL_PREFIX = STATIC_IMAGE_URL_PREFIX; diff --git a/core/test/unit/config/index_spec.js b/core/test/unit/config/index_spec.js index 425d84af70..412d71d51d 100644 --- a/core/test/unit/config/index_spec.js +++ b/core/test/unit/config/index_spec.js @@ -94,7 +94,6 @@ describe('Config', function () { 'contentPath', 'corePath', 'internalAppPath', - 'imagesRelPath', 'adminViews', 'helperTemplates', 'clientAssets' @@ -106,7 +105,6 @@ describe('Config', function () { appRoot = path.resolve(__dirname, '../../../../'); pathConfig.should.have.property('appRoot', appRoot); - pathConfig.should.have.property('imagesRelPath', 'content/images'); }); it('should allow specific properties to be user defined', function () {