2020-04-29 18:44:27 +03:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const config = require('../../server/config');
|
|
|
|
const imageLib = require('../../server/lib/image');
|
|
|
|
const urlUtils = require('../../server/lib/url-utils');
|
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...
|
2020-04-29 18:44:27 +03:00
|
|
|
let output = urlUtils.urlJoin(urlUtils.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/)) {
|
2019-06-18 16:13:55 +03:00
|
|
|
output = urlUtils.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) {
|
2018-09-17 21:49:30 +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
|
2019-06-18 16:13:55 +03:00
|
|
|
output = urlUtils.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')) {
|
2019-06-05 11:06:25 +03:00
|
|
|
config.set('assetHash', (crypto.createHash('md5').update(Date.now().toString()).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;
|