2017-12-11 21:14:05 +03:00
|
|
|
var urlService = require('../../services/url');
|
2017-09-05 15:13:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @TODO: move `index.js` to here - e.g. storageUtils.getStorage
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes a given URL or path for an image to be readable by the local file storage
|
2017-11-09 13:11:54 +03:00
|
|
|
* as storage needs the path without `/content/images/` prefix
|
2017-09-05 15:13:22 +03: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 13:11:54 +03:00
|
|
|
// The '/' in urlJoin is necessary to add the '/' to `content/images`, if no subdirectory is setup
|
2017-12-11 21:14:05 +03:00
|
|
|
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));
|
2017-11-09 13:11:54 +03:00
|
|
|
|
|
|
|
if (imagePath.match(urlRegExp)) {
|
|
|
|
return imagePath.replace(urlRegExp, '');
|
|
|
|
} else if (imagePath.match(filePathRegExp)) {
|
|
|
|
return imagePath.replace(filePathRegExp, '');
|
2017-09-05 15:13:22 +03:00
|
|
|
} else {
|
|
|
|
return imagePath;
|
|
|
|
}
|
|
|
|
};
|
2017-11-09 13:11:54 +03: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) {
|
|
|
|
var localImagePath = this.getLocalFileStoragePath(imagePath);
|
|
|
|
|
|
|
|
if (localImagePath !== imagePath) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|