feat: Have ExtensionBasedMapper handle extensions correctly

This commit is contained in:
Joachim Van Herwegen
2020-10-02 17:15:52 +02:00
parent 0644f8d245
commit b47dc3f7f6
5 changed files with 387 additions and 119 deletions

View File

@@ -1,11 +1,13 @@
import { promises as fsPromises } from 'fs';
import { posix } from 'path';
import { types } from 'mime-types';
import * as mime from 'mime-types';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { APPLICATION_OCTET_STREAM, TEXT_TURTLE } from '../util/ContentTypes';
import { ConflictHttpError } from '../util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../util/errors/UnsupportedHttpError';
import { trimTrailingSlashes } from '../util/Util';
import type { FileIdentifierMapper } from './FileIdentifierMapper';
import type { FileIdentifierMapper, ResourceLink } from './FileIdentifierMapper';
const { join: joinPath, normalize: normalizePath } = posix;
@@ -22,52 +24,136 @@ export interface ResourcePath {
documentName?: string;
}
/**
* A mapper that stores the content-type of resources in the file path extension.
* In case the extension of the identifier does not correspond to the correct content-type,
* a new extension will be appended (with a `$` in front of it).
* E.g. if the path is `input.ttl` with content-type `text/plain`, the path would actually be `input.ttl$.txt`.
* This new extension is stripped again when generating an identifier.
*/
export class ExtensionBasedMapper implements FileIdentifierMapper {
private readonly base: string;
private readonly prootFilepath: string;
private readonly baseRequestURI: string;
private readonly rootFilepath: string;
private readonly types: Record<string, any>;
public constructor(base: string, rootFilepath: string, overrideTypes = { acl: TEXT_TURTLE, metadata: TEXT_TURTLE }) {
this.base = base;
this.prootFilepath = rootFilepath;
this.types = { ...types, ...overrideTypes };
}
public get baseRequestURI(): string {
return trimTrailingSlashes(this.base);
}
public get rootFilepath(): string {
return trimTrailingSlashes(this.prootFilepath);
this.baseRequestURI = trimTrailingSlashes(base);
this.rootFilepath = trimTrailingSlashes(normalizePath(rootFilepath));
this.types = { ...mime.types, ...overrideTypes };
}
/**
* Strips the baseRequestURI from the identifier and checks if the stripped base URI matches the store's one.
* @param identifier - Incoming identifier.
* Maps the given resource identifier / URL to a file path.
* Determines the content-type if no content-type was provided.
* For containers the content-type input gets ignored.
* @param identifier - The input identifier.
* @param contentType - The (optional) content-type of the resource.
*
* @throws {@link NotFoundHttpError}
* If the identifier does not match the baseRequestURI path of the store.
*
* @returns Absolute path of the file.
* @returns A ResourceLink with all the necessary metadata.
*/
public mapUrlToFilePath(identifier: ResourceIdentifier, id = ''): string {
return this.getAbsolutePath(this.getRelativePath(identifier), id);
}
public async mapUrlToFilePath(identifier: ResourceIdentifier, contentType?: string): Promise<ResourceLink> {
let path = this.getRelativePath(identifier);
/**
* Strips the rootFilepath path from the filepath and adds the baseRequestURI in front of it.
* @param path - The file path.
*
* @throws {@Link Error}
* If the file path does not match the rootFilepath path of the store.
*
* @returns Url of the file.
*/
public mapFilePathToUrl(path: string): string {
if (!path.startsWith(this.rootFilepath)) {
throw new Error(`File ${path} is not part of the file storage at ${this.rootFilepath}.`);
if (!path.startsWith('/')) {
throw new UnsupportedHttpError('URL needs a / after the base.');
}
return this.baseRequestURI + path.slice(this.rootFilepath.length);
if (path.includes('/..')) {
throw new UnsupportedHttpError('Disallowed /.. segment in URL.');
}
path = this.getAbsolutePath(path);
// Container
if (identifier.path.endsWith('/')) {
return {
identifier,
filePath: path,
};
}
// Would conflict with how new extensions get stored
if (/\$\.\w+$/u.test(path)) {
throw new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension.');
}
// Existing file
if (!contentType) {
const [ , folder, documentName ] = /^(.*\/)(.*)$/u.exec(path)!;
let fileName: string | undefined;
try {
const files = await fsPromises.readdir(folder);
fileName = files.find(
(file): boolean =>
file.startsWith(documentName) && /^(?:\$\..+)?$/u.test(file.slice(documentName.length)),
);
} catch {
// Parent folder does not exist (or is not a folder)
throw new NotFoundHttpError();
}
// File doesn't exist
if (!fileName) {
throw new NotFoundHttpError();
}
return {
identifier,
filePath: joinPath(folder, fileName),
contentType: this.getContentTypeFromExtension(fileName),
};
}
// If the extension of the identifier matches a different content-type than the one that is given,
// we need to add a new extension to match the correct type.
if (contentType !== this.getContentTypeFromExtension(path)) {
const extension = mime.extension(contentType);
if (!extension) {
throw new UnsupportedHttpError(`Unsupported content-type ${contentType}.`);
}
path = `${path}$.${extension}`;
}
return {
identifier,
filePath: path,
contentType,
};
}
/**
* Maps the given file path to an URL and determines the content-type
* @param filePath - The input file path.
* @param isContainer - If the path corresponds to a file.
*
* @returns A ResourceLink with all the necessary metadata.
*/
public async mapFilePathToUrl(filePath: string, isContainer: boolean): Promise<ResourceLink> {
if (!filePath.startsWith(this.rootFilepath)) {
throw new Error(`File ${filePath} is not part of the file storage at ${this.rootFilepath}.`);
}
let relative = filePath.slice(this.rootFilepath.length);
if (isContainer) {
return {
identifier: { path: encodeURI(this.baseRequestURI + relative) },
filePath,
};
}
// Files
const extension = this.getExtension(relative);
const contentType = this.getContentTypeFromExtension(relative);
if (extension && relative.endsWith(`$.${extension}`)) {
relative = relative.slice(0, -(extension.length + 2));
}
return {
identifier: { path: encodeURI(this.baseRequestURI + relative) },
filePath,
contentType,
};
}
/**
@@ -76,9 +162,19 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
*
* @returns Content type of the file.
*/
public getContentTypeFromExtension(path: string): string {
private getContentTypeFromExtension(path: string): string {
const extension = this.getExtension(path);
return (extension && this.types[extension.toLowerCase()]) || APPLICATION_OCTET_STREAM;
}
/**
* Extracts the extension (without dot) from a path.
* Custom functin since `path.extname` does not work on all cases (e.g. ".acl")
* @param path - Input path to parse.
*/
private getExtension(path: string): string | null {
const extension = /\.([^./]+)$/u.exec(path);
return (extension && this.types[extension[1].toLowerCase()]) || APPLICATION_OCTET_STREAM;
return extension && extension[1];
}
/**
@@ -88,7 +184,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
*
* @returns Absolute path of the file.
*/
public getAbsolutePath(path: string, identifier = ''): string {
private getAbsolutePath(path: string, identifier = ''): string {
return joinPath(this.rootFilepath, path, identifier);
}
@@ -105,7 +201,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
if (!identifier.path.startsWith(this.baseRequestURI)) {
throw new NotFoundHttpError();
}
return identifier.path.slice(this.baseRequestURI.length);
return decodeURI(identifier.path).slice(this.baseRequestURI.length);
}
/**
@@ -116,7 +212,7 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
* @throws {@link ConflictHttpError}
* If the root identifier is passed.
*
* @returns A ResourcePath object containing path and (optional) slug fields.
* @returns A ResourcePath object containing (absolute) path and (optional) slug fields.
*/
public extractDocumentName(identifier: ResourceIdentifier): ResourcePath {
const [ , containerPath, documentName ] = /^(.*\/)([^/]+\/?)?$/u.exec(this.getRelativePath(identifier)) ?? [];
@@ -125,9 +221,9 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
throw new ConflictHttpError('Container with that identifier already exists (root).');
}
return {
containerPath: normalizePath(containerPath),
containerPath: this.getAbsolutePath(normalizePath(containerPath)),
// If documentName is not undefined, return normalized documentName
// If documentName is defined, return normalized documentName
documentName: typeof documentName === 'string' ? normalizePath(documentName) : undefined,
};
}

View File

@@ -1,21 +1,40 @@
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
export interface ResourceLink {
/**
* Identifier of a resource.
*/
identifier: ResourceIdentifier;
/**
* File path of a resource.
*/
filePath: string;
/**
* Content-type for a data resource (not defined for containers).
*/
contentType?: string;
}
/**
* Supports mapping a file to an URL and back.
*/
export interface FileIdentifierMapper {
/**
* Maps the given file path to an URL.
* @param file - The input file path.
* Maps the given file path to an URL and determines the content-type
* @param filePath - The input file path.
* @param isContainer - If the path corresponds to a file.
*
* @returns The URL as a string.
* @returns A ResourceLink with all the necessary metadata.
*/
mapFilePathToUrl: (filePath: string) => string;
mapFilePathToUrl: (filePath: string, isContainer: boolean) => Promise<ResourceLink>;
/**
* Maps the given resource identifier / URL to a file path.
* @param url - The input URL.
* Determines the content-type if no content-type was provided.
* For containers the content-type input gets ignored.
* @param identifier - The input identifier.
* @param contentType - The (optional) content-type of the resource.
*
* @returns The file path as a string.
* @returns A ResourceLink with all the necessary metadata.
*/
mapUrlToFilePath: (identifier: ResourceIdentifier) => string;
mapUrlToFilePath: (identifier: ResourceIdentifier, contentType?: string) => Promise<ResourceLink>;
}

View File

@@ -19,6 +19,7 @@ import { CONTENT_TYPE, DCTERMS, HTTP, POSIX, RDF, XSD } from '../util/UriConstan
import { toTypedLiteral } from '../util/UriUtil';
import { ensureTrailingSlash } from '../util/Util';
import type { ExtensionBasedMapper } from './ExtensionBasedMapper';
import type { ResourceLink } from './FileIdentifierMapper';
import type { ResourceStore } from './ResourceStore';
const { join: joinPath } = posix;
@@ -58,7 +59,7 @@ export class FileResourceStore implements ResourceStore {
}
// Get the path from the request URI, all metadata triples if any, and the Slug and Link header values.
const path = this.resourceMapper.getRelativePath(container);
const path = (await this.resourceMapper.mapUrlToFilePath(container)).filePath;
const slug = representation.metadata.get(HTTP.slug)?.value;
const types = representation.metadata.getAll(RDF.type);
@@ -72,6 +73,7 @@ export class FileResourceStore implements ResourceStore {
if (raw.length > 0) {
metadata = this.metadataController.serializeQuads(raw);
}
return isContainer ?
this.createContainer(path, newIdentifier, path.endsWith('/'), metadata) :
this.createFile(path, newIdentifier, representation.data, path.endsWith('/'), metadata);
@@ -88,7 +90,7 @@ export class FileResourceStore implements ResourceStore {
}
// Get the file status of the path defined by the request URI mapped to the corresponding filepath.
path = this.resourceMapper.getAbsolutePath(path);
path = (await this.resourceMapper.mapUrlToFilePath(identifier)).filePath;
let stats;
try {
stats = await fsPromises.lstat(path);
@@ -115,20 +117,20 @@ export class FileResourceStore implements ResourceStore {
*/
public async getRepresentation(identifier: ResourceIdentifier): Promise<Representation> {
// Get the file status of the path defined by the request URI mapped to the corresponding filepath.
const path = this.resourceMapper.mapUrlToFilePath(identifier);
const resourceLink = await this.resourceMapper.mapUrlToFilePath(identifier);
let stats;
try {
stats = await fsPromises.lstat(path);
stats = await fsPromises.lstat(resourceLink.filePath);
} catch {
throw new NotFoundHttpError();
}
// Get the file or directory representation of the path according to its status.
if (stats.isFile()) {
return await this.getFileRepresentation(path, stats);
return await this.getFileRepresentation(resourceLink, stats);
}
if (stats.isDirectory()) {
return await this.getDirectoryRepresentation(ensureTrailingSlash(path), stats);
return await this.getDirectoryRepresentation(resourceLink, stats);
}
throw new NotFoundHttpError();
}
@@ -209,25 +211,24 @@ export class FileResourceStore implements ResourceStore {
/**
* Helper function to get the representation of a file in the file system.
* It loads the quads from the corresponding metadata file if it exists.
* @param path - The path to the file.
* @param resourceLink - The path information of the resource.
* @param stats - The Stats of the file.
*
* @returns The corresponding Representation.
*/
private async getFileRepresentation(path: string, stats: Stats): Promise<Representation> {
const readStream = createReadStream(path);
const contentType = this.resourceMapper.getContentTypeFromExtension(path);
private async getFileRepresentation(resourceLink: ResourceLink, stats: Stats): Promise<Representation> {
const readStream = createReadStream(resourceLink.filePath);
let rawMetadata: Quad[] = [];
try {
const readMetadataStream = createReadStream(`${path}.metadata`);
const readMetadataStream = createReadStream(`${resourceLink.filePath}.metadata`);
rawMetadata = await this.metadataController.parseQuads(readMetadataStream);
} catch {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
}
const metadata = new RepresentationMetadata(this.resourceMapper.mapFilePathToUrl(path)).addQuads(rawMetadata)
const metadata = new RepresentationMetadata(resourceLink.identifier.path).addQuads(rawMetadata)
.set(DCTERMS.modified, toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime))
.set(POSIX.size, toTypedLiteral(stats.size, XSD.integer));
metadata.contentType = contentType;
metadata.contentType = resourceLink.contentType;
return { metadata, data: readStream, binary: true };
}
@@ -236,23 +237,23 @@ export class FileResourceStore implements ResourceStore {
* It loads the quads from the corresponding metadata file if it exists
* and generates quad representations for all its children.
*
* @param path - The path to the directory.
* @param resourceLink - The path information of the resource.
* @param stats - The Stats of the directory.
*
* @returns The corresponding Representation.
*/
private async getDirectoryRepresentation(path: string, stats: Stats): Promise<Representation> {
const files = await fsPromises.readdir(path);
private async getDirectoryRepresentation(resourceLink: ResourceLink, stats: Stats): Promise<Representation> {
const files = await fsPromises.readdir(resourceLink.filePath);
const quads: Quad[] = [];
const containerURI = this.resourceMapper.mapFilePathToUrl(path);
const containerURI = resourceLink.identifier.path;
quads.push(...this.metadataController.generateResourceQuads(containerURI, stats));
quads.push(...await this.getDirChildrenQuadRepresentation(files, path, containerURI));
quads.push(...await this.getDirChildrenQuadRepresentation(files, resourceLink.filePath, containerURI));
let rawMetadata: Quad[] = [];
try {
const readMetadataStream = createReadStream(joinPath(path, '.metadata'));
const readMetadataStream = createReadStream(joinPath(resourceLink.filePath, '.metadata'));
rawMetadata = await this.metadataController.parseQuads(readMetadataStream);
} catch {
// Metadata file doesn't exist so lets keep `rawMetaData` an empty array.
@@ -282,14 +283,15 @@ export class FileResourceStore implements ResourceStore {
const childURIs: string[] = [];
for (const childName of files) {
try {
const childURI = this.resourceMapper.mapFilePathToUrl(joinPath(path, childName));
const childStats = await fsPromises.lstat(joinPath(path, childName));
if (!childStats.isFile() && !childStats.isDirectory()) {
continue;
}
const childLink = await this.resourceMapper
.mapFilePathToUrl(joinPath(path, childName), childStats.isDirectory());
quads.push(...this.metadataController.generateResourceQuads(childURI, childStats));
childURIs.push(childURI);
quads.push(...this.metadataController.generateResourceQuads(childLink.identifier.path, childStats));
childURIs.push(childLink.identifier.path);
} catch {
// Skip the child if there is an error.
}
@@ -312,9 +314,7 @@ export class FileResourceStore implements ResourceStore {
// (Re)write file for the resource if no container with that identifier exists.
let stats;
try {
stats = await fsPromises.lstat(
this.resourceMapper.getAbsolutePath(path, newIdentifier),
);
stats = await fsPromises.lstat(joinPath(path, newIdentifier));
} catch {
await this.createFile(path, newIdentifier, data, true, metadata);
return;
@@ -335,9 +335,7 @@ export class FileResourceStore implements ResourceStore {
private async setDirectoryRepresentation(path: string, newIdentifier: string, metadata?: Readable): Promise<void> {
// Create a container if the identifier doesn't exist yet.
try {
await fsPromises.access(
this.resourceMapper.getAbsolutePath(path, newIdentifier),
);
await fsPromises.access(joinPath(path, newIdentifier));
throw new ConflictHttpError('Resource with that identifier already exists.');
} catch (error: unknown) {
if (error instanceof ConflictHttpError) {
@@ -367,12 +365,7 @@ export class FileResourceStore implements ResourceStore {
}
// Get the file status of the filepath of the directory where the file is to be created.
let stats;
try {
stats = await fsPromises.lstat(this.resourceMapper.getAbsolutePath(path));
} catch {
throw new MethodNotAllowedHttpError();
}
const stats = await fsPromises.lstat(path);
// Only create the file if the provided filepath is a valid directory.
if (!stats.isDirectory()) {
@@ -380,17 +373,17 @@ export class FileResourceStore implements ResourceStore {
} else {
// If metadata is specified, save it in a corresponding metadata file.
if (metadata) {
await this.createDataFile(this.resourceMapper.getAbsolutePath(path, `${resourceName}.metadata`), metadata);
await this.createDataFile(joinPath(path, `${resourceName}.metadata`), metadata);
}
// If no error thrown from above, indicating failed metadata file creation, create the actual resource file.
try {
const fullPath = this.resourceMapper.getAbsolutePath(path, resourceName);
const fullPath = joinPath(path, resourceName);
await this.createDataFile(fullPath, data);
return { path: this.resourceMapper.mapFilePathToUrl(fullPath) };
return (await this.resourceMapper.mapFilePathToUrl(fullPath, false)).identifier;
} catch (error: unknown) {
// Normal file has not been created so we don't want the metadata file to remain.
await fsPromises.unlink(this.resourceMapper.getAbsolutePath(path, `${resourceName}.metadata`));
await fsPromises.unlink(joinPath(path, `${resourceName}.metadata`));
throw error;
}
}
@@ -407,12 +400,12 @@ export class FileResourceStore implements ResourceStore {
*/
private async createContainer(path: string, containerName: string,
allowRecursiveCreation: boolean, metadata?: Readable): Promise<ResourceIdentifier> {
const fullPath = ensureTrailingSlash(this.resourceMapper.getAbsolutePath(path, containerName));
const fullPath = joinPath(path, containerName);
// If recursive creation is not allowed, check if the parent container exists and then create the child directory.
try {
if (!allowRecursiveCreation) {
const stats = await fsPromises.lstat(this.resourceMapper.getAbsolutePath(path));
const stats = await fsPromises.lstat(path);
if (!stats.isDirectory()) {
throw new MethodNotAllowedHttpError('The given path is not a valid container.');
}
@@ -436,7 +429,7 @@ export class FileResourceStore implements ResourceStore {
throw error;
}
}
return { path: this.resourceMapper.mapFilePathToUrl(fullPath) };
return (await this.resourceMapper.mapFilePathToUrl(fullPath, true)).identifier;
}
/**