feat: Have FixedContentTypeMapper ignore .meta

This commit is contained in:
surilindur 2023-08-28 11:11:24 +02:00 committed by Joachim Van Herwegen
parent d401cc862b
commit 9e682f5c4f
3 changed files with 16 additions and 5 deletions

View File

@ -24,6 +24,8 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper {
protected readonly rootFilepath: string; protected readonly rootFilepath: string;
// Extension to use as a fallback when the media type is not supported (could be made configurable). // Extension to use as a fallback when the media type is not supported (could be made configurable).
protected readonly unknownMediaTypeExtension = 'unknown'; protected readonly unknownMediaTypeExtension = 'unknown';
// Path suffix for metadata
private readonly metadataSuffix = '.meta';
public constructor(base: string, rootFilepath: string) { public constructor(base: string, rootFilepath: string) {
this.baseRequestURI = trimTrailingSlashes(base); this.baseRequestURI = trimTrailingSlashes(base);
@ -44,7 +46,7 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper {
Promise<ResourceLink> { Promise<ResourceLink> {
let path = this.getRelativePath(identifier); let path = this.getRelativePath(identifier);
if (isMetadata) { if (isMetadata) {
path += '.meta'; path += this.metadataSuffix;
} }
this.validateRelativePath(path, identifier); this.validateRelativePath(path, identifier);
@ -125,7 +127,7 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper {
} }
const isMetadata = this.isMetadataPath(filePath); const isMetadata = this.isMetadataPath(filePath);
if (isMetadata) { if (isMetadata) {
url = url.slice(0, -'.meta'.length); url = url.slice(0, -this.metadataSuffix.length);
} }
return { identifier: { path: url }, filePath, contentType, isMetadata }; return { identifier: { path: url }, filePath, contentType, isMetadata };
} }
@ -213,6 +215,6 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper {
* Checks if the given path is a metadata path. * Checks if the given path is a metadata path.
*/ */
protected isMetadataPath(path: string): boolean { protected isMetadataPath(path: string): boolean {
return path.endsWith('.meta'); return path.endsWith(this.metadataSuffix);
} }
} }

View File

@ -64,8 +64,8 @@ export class FixedContentTypeMapper extends BaseFileIdentifierMapper {
} }
protected async getDocumentUrl(relative: string): Promise<string> { protected async getDocumentUrl(relative: string): Promise<string> {
// Handle path suffix // Handle path suffix, but ignore metadata files
if (this.pathSuffix) { if (this.pathSuffix && !this.isMetadataPath(relative)) {
if (relative.endsWith(this.pathSuffix)) { if (relative.endsWith(this.pathSuffix)) {
relative = relative.slice(0, -this.pathSuffix.length); relative = relative.slice(0, -this.pathSuffix.length);
} else { } else {

View File

@ -183,6 +183,15 @@ describe('An FixedContentTypeMapper', (): void => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).rejects.toThrow(NotFoundHttpError); await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).rejects.toThrow(NotFoundHttpError);
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).rejects.toThrow(NotFoundHttpError); await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).rejects.toThrow(NotFoundHttpError);
}); });
it('returns a generate file path for metadata regardless of the suffix.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}.meta`, false)).resolves.toEqual({
identifier: { path: `${base}` },
filePath: `${rootFilepath}.meta`,
contentType: 'text/turtle',
isMetadata: true,
});
});
}); });
}); });