2019-06-18 08:13:55 -05:00
|
|
|
const urlUtils = require('../../lib/url-utils');
|
2017-09-05 07:13:22 -05:00
|
|
|
/**
|
2017-12-11 14:27:09 -05:00
|
|
|
* @TODO: move `events.js` to here - e.g. storageUtils.getStorage
|
2017-09-05 07:13:22 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes a given URL or path for an image to be readable by the local file storage
|
2017-11-09 05:11:54 -05:00
|
|
|
* as storage needs the path without `/content/images/` prefix
|
2017-09-05 07:13:22 -05:00
|
|
|
* 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) {
|
2017-11-09 05:11:54 -05:00
|
|
|
// The '/' in urlJoin is necessary to add the '/' to `content/images`, if no subdirectory is setup
|
2019-06-18 08:13:55 -05:00
|
|
|
const urlRegExp = new RegExp(`^${urlUtils.urlJoin(
|
2020-04-29 10:44:27 -05:00
|
|
|
urlUtils.urlFor('home', true),
|
|
|
|
urlUtils.getSubdir(),
|
|
|
|
'/',
|
|
|
|
urlUtils.STATIC_IMAGE_URL_PREFIX)}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const filePathRegExp = new RegExp(`^${urlUtils.urlJoin(
|
|
|
|
urlUtils.getSubdir(),
|
|
|
|
'/',
|
|
|
|
urlUtils.STATIC_IMAGE_URL_PREFIX)}`
|
|
|
|
);
|
2017-11-09 05:11:54 -05:00
|
|
|
|
|
|
|
if (imagePath.match(urlRegExp)) {
|
|
|
|
return imagePath.replace(urlRegExp, '');
|
|
|
|
} else if (imagePath.match(filePathRegExp)) {
|
|
|
|
return imagePath.replace(filePathRegExp, '');
|
2017-09-05 07:13:22 -05:00
|
|
|
} else {
|
|
|
|
return imagePath;
|
|
|
|
}
|
|
|
|
};
|
2017-11-09 05:11:54 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2018-09-10 06:40:32 -05:00
|
|
|
const localImagePath = this.getLocalFileStoragePath(imagePath);
|
2017-11-09 05:11:54 -05:00
|
|
|
|
|
|
|
if (localImagePath !== imagePath) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|