2020-04-29 18:44:27 +03:00
|
|
|
const crypto = require('crypto');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../shared/config');
|
2020-05-26 21:11:23 +03:00
|
|
|
const {blogIcon} = require('../../server/lib/image');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../shared/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() {
|
2020-05-26 21:11:23 +03:00
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2022-02-07 18:07:18 +03:00
|
|
|
// if url has # make sure the hash is at th right place
|
|
|
|
let anchor;
|
|
|
|
if (path.match('#')) {
|
|
|
|
const index = output.indexOf('#');
|
|
|
|
anchor = output.substring(index);
|
|
|
|
output = output.slice(0, index);
|
|
|
|
}
|
|
|
|
|
2017-04-10 12:30:21 +03:00
|
|
|
// Finally add the asset hash to the output URL
|
|
|
|
output += '?v=' + config.get('assetHash');
|
|
|
|
|
2022-02-07 18:07:18 +03:00
|
|
|
if (anchor) {
|
|
|
|
output += anchor;
|
|
|
|
}
|
2016-01-17 13:07:52 +03:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getAssetUrl;
|