2017-12-14 03:33:01 +03:00
|
|
|
const crypto = require('crypto'),
|
|
|
|
config = require('../../config'),
|
2017-12-14 22:46:53 +03:00
|
|
|
imageLib = require('../../lib/image'),
|
2017-12-11 21:14:05 +03:00
|
|
|
urlService = require('../../services/url'),
|
2017-12-14 03:33:01 +03:00
|
|
|
packageInfo = require('../../../../package.json');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
/**
|
|
|
|
* Serve either uploaded favicon or default
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function getFaviconUrl() {
|
2017-12-14 22:46:53 +03:00
|
|
|
return imageLib.blogIcon.getIconUrl();
|
2017-04-10 12:30:21 +03:00
|
|
|
}
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
function getAssetUrl(path, hasMinFile) {
|
|
|
|
// CASE: favicon - this is special path with its own functionality
|
|
|
|
if (path.match(/\/?favicon\.(ico|png)$/)) {
|
|
|
|
// @TODO, resolve this - we should only be resolving subdirectory and extension.
|
|
|
|
return getFaviconUrl();
|
|
|
|
}
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// CASE: Build the output URL
|
|
|
|
// Add subdirectory...
|
2017-12-11 21:14:05 +03:00
|
|
|
var output = urlService.utils.urlJoin(urlService.utils.getSubdir(), '/');
|
2016-10-08 01:05:36 +03:00
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// Optionally add /assets/
|
|
|
|
if (!path.match(/^public/) && !path.match(/^asset/)) {
|
2017-12-11 21:14:05 +03:00
|
|
|
output = urlService.utils.urlJoin(output, 'assets/');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// replace ".foo" with ".min.foo" if configured
|
|
|
|
if (hasMinFile && config.get('useMinFiles') !== false) {
|
2016-02-21 21:48:44 +03:00
|
|
|
path = path.replace(/\.([^\.]*)$/, '.min.$1');
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// Add the path for the requested asset
|
2017-12-11 21:14:05 +03:00
|
|
|
output = urlService.utils.urlJoin(output, path);
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// Ensure we have an assetHash
|
|
|
|
// @TODO rework this!
|
|
|
|
if (!config.get('assetHash')) {
|
2017-12-14 03:33:01 +03:00
|
|
|
config.set('assetHash', (crypto.createHash('md5').update(packageInfo.version + Date.now()).digest('hex')).substring(0, 10));
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// Finally add the asset hash to the output URL
|
|
|
|
output += '?v=' + config.get('assetHash');
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getAssetUrl;
|