mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feature: add FixedContentTypeMapper
This commit is contained in:
parent
971e4178d1
commit
f686b4853f
1
index.ts
1
index.ts
@ -104,6 +104,7 @@ export * from './src/storage/conversion/TypedRepresentationConverter';
|
|||||||
|
|
||||||
// Storage/Mapping
|
// Storage/Mapping
|
||||||
export * from './src/storage/mapping/ExtensionBasedMapper';
|
export * from './src/storage/mapping/ExtensionBasedMapper';
|
||||||
|
export * from './src/storage/mapping/FixedContentTypeMapper';
|
||||||
|
|
||||||
// Storage/Patch
|
// Storage/Patch
|
||||||
export * from './src/storage/patch/PatchHandler';
|
export * from './src/storage/patch/PatchHandler';
|
||||||
|
84
src/storage/mapping/FixedContentTypeMapper.ts
Normal file
84
src/storage/mapping/FixedContentTypeMapper.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import { posix } from 'path';
|
||||||
|
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
|
||||||
|
import { getLoggerFor } from '../../logging/LogUtil';
|
||||||
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
|
import {
|
||||||
|
encodeUriPathComponents,
|
||||||
|
ensureTrailingSlash,
|
||||||
|
trimTrailingSlashes,
|
||||||
|
} from '../../util/PathUtil';
|
||||||
|
import type { FileIdentifierMapper, ResourceLink } from '../FileIdentifierMapper';
|
||||||
|
import { getAbsolutePath, getRelativePath, validateRelativePath } from './MapperUtil';
|
||||||
|
|
||||||
|
const { normalize: normalizePath } = posix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A mapper that always returns a fixed content type for files.
|
||||||
|
*/
|
||||||
|
export class FixedContentTypeMapper implements FileIdentifierMapper {
|
||||||
|
protected readonly logger = getLoggerFor(this);
|
||||||
|
|
||||||
|
private readonly baseRequestURI: string;
|
||||||
|
private readonly rootFilepath: string;
|
||||||
|
private readonly contentType: string;
|
||||||
|
|
||||||
|
public constructor(base: string, rootFilepath: string, contentType: string) {
|
||||||
|
this.baseRequestURI = trimTrailingSlashes(base);
|
||||||
|
this.rootFilepath = trimTrailingSlashes(normalizePath(rootFilepath));
|
||||||
|
this.contentType = contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> {
|
||||||
|
const path = getRelativePath(this.baseRequestURI, identifier, this.logger);
|
||||||
|
validateRelativePath(path, identifier, this.logger);
|
||||||
|
|
||||||
|
const filePath = getAbsolutePath(this.rootFilepath, path);
|
||||||
|
|
||||||
|
// Container
|
||||||
|
if (identifier.path.endsWith('/')) {
|
||||||
|
this.logger.debug(`URL ${identifier.path} points to the container ${filePath}`);
|
||||||
|
return {
|
||||||
|
identifier,
|
||||||
|
filePath,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only allow the configured content type
|
||||||
|
if (contentType && contentType !== this.contentType) {
|
||||||
|
throw new UnsupportedHttpError(`Unsupported content type ${contentType}, only ${this.contentType} is allowed`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.info(`The path for ${identifier.path} is ${filePath}`);
|
||||||
|
return {
|
||||||
|
identifier,
|
||||||
|
filePath,
|
||||||
|
contentType: this.contentType,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async mapFilePathToUrl(filePath: string, isContainer: boolean): Promise<ResourceLink> {
|
||||||
|
if (!filePath.startsWith(this.rootFilepath)) {
|
||||||
|
this.logger.error(`Trying to access file ${filePath} outside of ${this.rootFilepath}`);
|
||||||
|
throw new Error(`File ${filePath} is not part of the file storage at ${this.rootFilepath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const relative = filePath.slice(this.rootFilepath.length);
|
||||||
|
if (isContainer) {
|
||||||
|
const path = ensureTrailingSlash(this.baseRequestURI + encodeUriPathComponents(relative));
|
||||||
|
this.logger.info(`Container filepath ${filePath} maps to URL ${path}`);
|
||||||
|
return {
|
||||||
|
identifier: { path },
|
||||||
|
filePath,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = trimTrailingSlashes(this.baseRequestURI + encodeUriPathComponents(relative));
|
||||||
|
this.logger.info(`File ${filePath} maps to URL ${path}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
identifier: { path },
|
||||||
|
filePath,
|
||||||
|
contentType: this.contentType,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdenti
|
|||||||
import type { Logger } from '../../logging/Logger';
|
import type { Logger } from '../../logging/Logger';
|
||||||
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
import { decodeUriPathComponents } from '../../util/Util';
|
import { decodeUriPathComponents } from '../../util/PathUtil';
|
||||||
|
|
||||||
const { join: joinPath } = posix;
|
const { join: joinPath } = posix;
|
||||||
|
|
||||||
|
97
test/unit/storage/mapping/FixedContentTypeMapper.test.ts
Normal file
97
test/unit/storage/mapping/FixedContentTypeMapper.test.ts
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import { FixedContentTypeMapper } from '../../../../src/storage/mapping/FixedContentTypeMapper';
|
||||||
|
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
|
||||||
|
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||||
|
import { trimTrailingSlashes } from '../../../../src/util/PathUtil';
|
||||||
|
|
||||||
|
jest.mock('fs');
|
||||||
|
|
||||||
|
describe('An FixedContentTypeMapper', (): void => {
|
||||||
|
const base = 'http://test.com/';
|
||||||
|
const rootFilepath = 'uploads/';
|
||||||
|
const mapper = new FixedContentTypeMapper(base, rootFilepath, 'text/turtle');
|
||||||
|
|
||||||
|
describe('mapUrlToFilePath', (): void => {
|
||||||
|
it('throws 404 if the input path does not contain the base.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: 'invalid' })).rejects.toThrow(NotFoundHttpError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
|
||||||
|
.rejects.toThrow(new UnsupportedHttpError('URL needs a / after the base'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
|
||||||
|
.rejects.toThrow(new UnsupportedHttpError('Disallowed /.. segment in URL'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}container/` })).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}container/` },
|
||||||
|
filePath: `${rootFilepath}container/`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('always returns the configured content-type.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test` })).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test` },
|
||||||
|
filePath: `${rootFilepath}test`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` })).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test.ttl` },
|
||||||
|
filePath: `${rootFilepath}test.ttl`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test.txt` },
|
||||||
|
filePath: `${rootFilepath}test.txt`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('generates a file path if supported content-type was provided.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` }, 'text/turtle')).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test.ttl` },
|
||||||
|
filePath: `${rootFilepath}test.ttl`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws 400 if the given content-type is not supported.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` }, 'application/n-quads')).rejects
|
||||||
|
.toThrow(new UnsupportedHttpError(`Unsupported content type application/n-quads, only text/turtle is allowed`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('mapFilePathToUrl', (): void => {
|
||||||
|
it('throws an error if the input path does not contain the root file path.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapFilePathToUrl('invalid', true)).rejects.toThrow(Error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a generated identifier for directories.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapFilePathToUrl(`${rootFilepath}container/`, true)).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}container/` },
|
||||||
|
filePath: `${rootFilepath}container/`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns files with the configured content-type.', async(): Promise<void> => {
|
||||||
|
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test` },
|
||||||
|
filePath: `${rootFilepath}test`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.ttl`, false)).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test.ttl` },
|
||||||
|
filePath: `${rootFilepath}test.ttl`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).resolves.toEqual({
|
||||||
|
identifier: { path: `${base}test.txt` },
|
||||||
|
filePath: `${rootFilepath}test.txt`,
|
||||||
|
contentType: 'text/turtle',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user