2016-09-12 14:53:04 +03:00
|
|
|
var config = require('../../config'),
|
2017-02-27 18:53:04 +03:00
|
|
|
settingsCache = require('../../settings/cache'),
|
2016-09-23 14:05:44 +03:00
|
|
|
utils = require('../../utils');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2016-02-21 21:48:44 +03:00
|
|
|
function getAssetUrl(path, isAdmin, minify) {
|
2016-01-17 13:07:52 +03:00
|
|
|
var output = '';
|
|
|
|
|
2016-11-14 17:38:55 +03:00
|
|
|
output += utils.url.urlJoin(utils.url.getSubdir(), '/');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-04-07 15:21:41 +03:00
|
|
|
if (!path.match(/^favicon\.(ico|png)$/) && !path.match(/^public/) && !path.match(/^asset/)) {
|
2016-01-17 13:07:52 +03:00
|
|
|
if (isAdmin) {
|
2016-11-14 17:38:55 +03:00
|
|
|
output = utils.url.urlJoin(output, 'ghost/');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
2016-10-08 01:05:36 +03:00
|
|
|
|
2016-11-14 17:38:55 +03:00
|
|
|
output = utils.url.urlJoin(output, 'assets/');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
2017-01-26 21:01:19 +03:00
|
|
|
// Serve either uploaded favicon or default
|
2017-04-04 12:06:38 +03:00
|
|
|
// for favicon, we don't care anymore about the `/` leading slash, as we don't support theme favicons
|
2017-01-26 21:01:19 +03:00
|
|
|
if (path.match(/\/?favicon\.(ico|png)$/)) {
|
|
|
|
if (isAdmin) {
|
|
|
|
output = utils.url.urlJoin(utils.url.getSubdir(), '/favicon.ico');
|
|
|
|
} else {
|
2017-02-03 16:15:11 +03:00
|
|
|
if (settingsCache.get('icon')) {
|
|
|
|
output = utils.url.urlJoin(utils.url.getSubdir(), utils.url.urlFor('image', {image: settingsCache.get('icon')}));
|
|
|
|
} else {
|
|
|
|
output = utils.url.urlJoin(utils.url.getSubdir(), '/favicon.ico');
|
|
|
|
}
|
2017-01-26 21:01:19 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-21 21:48:44 +03:00
|
|
|
// Get rid of any leading slash on the path
|
|
|
|
path = path.replace(/^\//, '');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
// replace ".foo" with ".min.foo" in production
|
|
|
|
if (minify) {
|
2016-02-21 21:48:44 +03:00
|
|
|
path = path.replace(/\.([^\.]*)$/, '.min.$1');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 21:01:19 +03:00
|
|
|
if (!path.match(/^favicon\.(ico|png)$/)) {
|
|
|
|
// we don't want to concat the path with our favicon url
|
|
|
|
output += path;
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2016-09-13 21:28:20 +03:00
|
|
|
if (!config.get('assetHash')) {
|
2016-09-23 14:05:44 +03:00
|
|
|
config.set('assetHash', utils.generateAssetHash());
|
2016-09-13 21:28:20 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 18:41:14 +03:00
|
|
|
output = output + '?v=' + config.get('assetHash');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getAssetUrl;
|