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:
committed by
Joachim Van Herwegen
parent
971e4178d1
commit
f686b4853f
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 { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||
import { decodeUriPathComponents } from '../../util/Util';
|
||||
import { decodeUriPathComponents } from '../../util/PathUtil';
|
||||
|
||||
const { join: joinPath } = posix;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user