mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Respect root path for static assets and HTML links
* feat: rootpath for static assets and links1 * fix: static asset handler respects root path * fix: use rootPath for links * tests: fix the tests after adding consuctor params * feat: change matchregex instead of stored URLs * feat: baseUrl for handlebar engine and templates * feat: full baseUrl passed to templates * test: fix integration tests + templates * chore: implement requested changes * docs: Describe TemplateEngine interface changes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
This commit is contained in:
@@ -5,7 +5,7 @@ import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { APPLICATION_OCTET_STREAM } from '../../util/ContentTypes';
|
||||
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||
import { joinFilePath, resolveAssetPath } from '../../util/PathUtil';
|
||||
import { ensureTrailingSlash, joinFilePath, resolveAssetPath, trimLeadingSlashes } from '../../util/PathUtil';
|
||||
import { pipeSafely } from '../../util/StreamUtil';
|
||||
import type { HttpHandlerInput } from '../HttpHandler';
|
||||
import { HttpHandler } from '../HttpHandler';
|
||||
@@ -29,22 +29,24 @@ export class StaticAssetHandler extends HttpHandler {
|
||||
* where URL paths ending in a slash are interpreted as entire folders.
|
||||
* @param options - Cache expiration time in seconds.
|
||||
*/
|
||||
public constructor(assets: Record<string, string>, options: { expires?: number } = {}) {
|
||||
public constructor(assets: Record<string, string>, baseUrl: string, options: { expires?: number } = {}) {
|
||||
super();
|
||||
this.mappings = {};
|
||||
const rootPath = ensureTrailingSlash(new URL(baseUrl).pathname);
|
||||
|
||||
for (const [ url, path ] of Object.entries(assets)) {
|
||||
this.mappings[url] = resolveAssetPath(path);
|
||||
this.mappings[trimLeadingSlashes(url)] = resolveAssetPath(path);
|
||||
}
|
||||
this.pathMatcher = this.createPathMatcher(assets);
|
||||
this.pathMatcher = this.createPathMatcher(rootPath);
|
||||
this.expires = Number.isInteger(options.expires) ? Math.max(0, options.expires!) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a regular expression that matches the URL paths.
|
||||
*/
|
||||
private createPathMatcher(assets: Record<string, string>): RegExp {
|
||||
private createPathMatcher(rootPath: string): RegExp {
|
||||
// Sort longest paths first to ensure the longest match has priority
|
||||
const paths = Object.keys(assets)
|
||||
const paths = Object.keys(this.mappings)
|
||||
.sort((pathA, pathB): number => pathB.length - pathA.length);
|
||||
|
||||
// Collect regular expressions for files and folders separately
|
||||
@@ -55,7 +57,7 @@ export class StaticAssetHandler extends HttpHandler {
|
||||
}
|
||||
|
||||
// Either match an exact document or a file within a folder (stripping the query string)
|
||||
return new RegExp(`^(?:(${files.join('|')})|(${folders.join('|')})([^?]+))(?:\\?.*)?$`, 'u');
|
||||
return new RegExp(`^${rootPath}(?:(${files.join('|')})|(${folders.join('|')})([^?]+))(?:\\?.*)?$`, 'u');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user