From 9e682f5c4f8ecd222ae633137ca455b1b9c5ce16 Mon Sep 17 00:00:00 2001 From: surilindur Date: Mon, 28 Aug 2023 11:11:24 +0200 Subject: [PATCH] feat: Have FixedContentTypeMapper ignore .meta --- src/storage/mapping/BaseFileIdentifierMapper.ts | 8 +++++--- src/storage/mapping/FixedContentTypeMapper.ts | 4 ++-- test/unit/storage/mapping/FixedContentTypeMapper.test.ts | 9 +++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/storage/mapping/BaseFileIdentifierMapper.ts b/src/storage/mapping/BaseFileIdentifierMapper.ts index 2f88c373c..921ffb601 100644 --- a/src/storage/mapping/BaseFileIdentifierMapper.ts +++ b/src/storage/mapping/BaseFileIdentifierMapper.ts @@ -24,6 +24,8 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper { protected readonly rootFilepath: string; // Extension to use as a fallback when the media type is not supported (could be made configurable). protected readonly unknownMediaTypeExtension = 'unknown'; + // Path suffix for metadata + private readonly metadataSuffix = '.meta'; public constructor(base: string, rootFilepath: string) { this.baseRequestURI = trimTrailingSlashes(base); @@ -44,7 +46,7 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper { Promise { let path = this.getRelativePath(identifier); if (isMetadata) { - path += '.meta'; + path += this.metadataSuffix; } this.validateRelativePath(path, identifier); @@ -125,7 +127,7 @@ export class BaseFileIdentifierMapper implements FileIdentifierMapper { } const isMetadata = this.isMetadataPath(filePath); if (isMetadata) { - url = url.slice(0, -'.meta'.length); + url = url.slice(0, -this.metadataSuffix.length); } 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. */ protected isMetadataPath(path: string): boolean { - return path.endsWith('.meta'); + return path.endsWith(this.metadataSuffix); } } diff --git a/src/storage/mapping/FixedContentTypeMapper.ts b/src/storage/mapping/FixedContentTypeMapper.ts index 3ca5556a7..0fb6c1545 100644 --- a/src/storage/mapping/FixedContentTypeMapper.ts +++ b/src/storage/mapping/FixedContentTypeMapper.ts @@ -64,8 +64,8 @@ export class FixedContentTypeMapper extends BaseFileIdentifierMapper { } protected async getDocumentUrl(relative: string): Promise { - // Handle path suffix - if (this.pathSuffix) { + // Handle path suffix, but ignore metadata files + if (this.pathSuffix && !this.isMetadataPath(relative)) { if (relative.endsWith(this.pathSuffix)) { relative = relative.slice(0, -this.pathSuffix.length); } else { diff --git a/test/unit/storage/mapping/FixedContentTypeMapper.test.ts b/test/unit/storage/mapping/FixedContentTypeMapper.test.ts index bc24b83a2..0a2414db3 100644 --- a/test/unit/storage/mapping/FixedContentTypeMapper.test.ts +++ b/test/unit/storage/mapping/FixedContentTypeMapper.test.ts @@ -183,6 +183,15 @@ describe('An FixedContentTypeMapper', (): void => { await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, 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 => { + await expect(mapper.mapFilePathToUrl(`${rootFilepath}.meta`, false)).resolves.toEqual({ + identifier: { path: `${base}` }, + filePath: `${rootFilepath}.meta`, + contentType: 'text/turtle', + isMetadata: true, + }); + }); }); });