mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 03:44:29 +03:00
abda6e6338
closes #10773 - The refactoring is a substitute for `urlService.utils` used previously throughout the codebase and now extracted into the separate module in Ghost-SDK - Added url-utils stubbing utility for test suites - Some tests had to be refactored to avoid double mocks (when url's are being reset inside of rested 'describe' groups)
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
const urlUtils = require('../../lib/url-utils');
|
|
/**
|
|
* @TODO: move `events.js` to here - e.g. storageUtils.getStorage
|
|
*/
|
|
|
|
/**
|
|
* Sanitizes a given URL or path for an image to be readable by the local file storage
|
|
* as storage needs the path without `/content/images/` prefix
|
|
* Always returns {string} url
|
|
* @param {string} imagePath
|
|
* @returns {string} imagePath
|
|
* @description Takes a url or filepath and returns a filepath with is readable
|
|
* for the local file storage.
|
|
*/
|
|
exports.getLocalFileStoragePath = function getLocalFileStoragePath(imagePath) {
|
|
// The '/' in urlJoin is necessary to add the '/' to `content/images`, if no subdirectory is setup
|
|
const urlRegExp = new RegExp(`^${urlUtils.urlJoin(
|
|
urlUtils.urlFor('home', true),
|
|
urlUtils.getSubdir(),
|
|
'/',
|
|
urlUtils.STATIC_IMAGE_URL_PREFIX)}`
|
|
),
|
|
filePathRegExp = new RegExp(`^${urlUtils.urlJoin(
|
|
urlUtils.getSubdir(),
|
|
'/',
|
|
urlUtils.STATIC_IMAGE_URL_PREFIX)}`
|
|
);
|
|
|
|
if (imagePath.match(urlRegExp)) {
|
|
return imagePath.replace(urlRegExp, '');
|
|
} else if (imagePath.match(filePathRegExp)) {
|
|
return imagePath.replace(filePathRegExp, '');
|
|
} else {
|
|
return imagePath;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @description compares the imagePath with a regex that reflects our local file storage
|
|
* @param {String} imagePath as URL or filepath
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isLocalImage = function isLocalImage(imagePath) {
|
|
const localImagePath = this.getLocalFileStoragePath(imagePath);
|
|
|
|
if (localImagePath !== imagePath) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|