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;
}
/**

View File

@ -1,20 +1,157 @@
import fs from 'fs';
import { ExtensionBasedMapper } from '../../../src/storage/ExtensionBasedMapper';
import { ConflictHttpError } from '../../../src/util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
import { trimTrailingSlashes } from '../../../src/util/Util';
jest.mock('fs');
describe('An ExtensionBasedMapper', (): void => {
const base = 'http://test.com/';
const rootFilepath = 'uploads/';
const resourceMapper = new ExtensionBasedMapper(base, rootFilepath);
const mapper = new ExtensionBasedMapper(base, rootFilepath);
let fsPromises: { [ id: string ]: jest.Mock };
it('returns the correct url of a file.', async(): Promise<void> => {
let result = resourceMapper.mapFilePathToUrl(`${rootFilepath}test.txt`);
expect(result).toEqual(`${base}test.txt`);
result = resourceMapper.mapFilePathToUrl(`${rootFilepath}image.jpg`);
expect(result).toEqual(`${base}image.jpg`);
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
fs.promises = {
readdir: jest.fn(),
} as any;
fsPromises = fs.promises as any;
});
it('errors when filepath does not contain rootFilepath.', async(): Promise<void> => {
expect((): string => resourceMapper.mapFilePathToUrl('random/test.txt')).toThrow(Error);
expect((): string => resourceMapper.mapFilePathToUrl('test.txt')).toThrow(Error);
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('rejects URLs that end with "$.{extension}".', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test$.txt` }))
.rejects.toThrow(new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension.'));
});
it('throws 404 when looking in a folder that does not exist.', async(): Promise<void> => {
fsPromises.readdir.mockImplementation((): void => {
throw new Error('does not exist');
});
await expect(mapper.mapUrlToFilePath({ path: `${base}no/test.txt` })).rejects.toThrow(NotFoundHttpError);
});
it('throws 404 when looking for a file that does not exist.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.ttl' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).rejects.toThrow(NotFoundHttpError);
});
it('determines the content-type based on the extension.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.txt' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('matches even if the content-type does not match the extension.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.txt$.ttl' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('generates a file path if the content-type was provided.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/plain')).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('adds an extension if the given extension does not match the given content-type.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/turtle')).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('throws 400 if the given content-type is not recognized.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'fake/data'))
.rejects.toThrow(new UnsupportedHttpError(`Unsupported content-type fake/data.`));
});
});
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 a generated identifier for files with corresponding content-type.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('removes appended extensions.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt$.ttl`, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('sets the content-type to application/octet-stream if there is no extension.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).resolves.toEqual({
identifier: { path: `${base}test` },
filePath: `${rootFilepath}test`,
contentType: 'application/octet-stream',
});
});
});
describe('extractDocumentName', (): void => {
it('throws an error if the input corresponds to root.', async(): Promise<void> => {
expect((): any => mapper.extractDocumentName({ path: base })).toThrow(ConflictHttpError);
expect((): any => mapper.extractDocumentName({ path: trimTrailingSlashes(base) }))
.toThrow(ConflictHttpError);
});
it('parses the identifier into container file path and document name.', async(): Promise<void> => {
expect(mapper.extractDocumentName({ path: `${base}test` })).toEqual({
containerPath: rootFilepath,
documentName: 'test',
});
expect(mapper.extractDocumentName({ path: `${base}test/` })).toEqual({
containerPath: `${rootFilepath}test/`,
});
});
});
});

View File

@ -47,6 +47,14 @@ describe('A FileResourceStore', (): void => {
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
fs.promises = {
rmdir: jest.fn(),
lstat: jest.fn(),
readdir: jest.fn(),
mkdir: jest.fn(),
unlink: jest.fn(),
access: jest.fn(),
} as any;
store = new FileResourceStore(
new ExtensionBasedMapper(base, rootFilepath),
@ -93,18 +101,18 @@ describe('A FileResourceStore', (): void => {
});
it('errors if a resource was not found.', async(): Promise<void> => {
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
(fsPromises.lstat as jest.Mock).mockImplementation((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.readdir as jest.Mock).mockImplementation((): any => []);
await expect(store.getRepresentation({ path: 'http://wrong.com/wrong' })).rejects.toThrow(NotFoundHttpError);
await expect(store.getRepresentation({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
await expect(store.getRepresentation({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
await expect(store.addResource({ path: 'http://wrong.com/wrong' }, representation))
.rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: 'wrong' })).rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
await expect(store.setRepresentation({ path: 'http://wrong.com/' }, representation))
.rejects.toThrow(NotFoundHttpError);
});
@ -153,6 +161,7 @@ describe('A FileResourceStore', (): void => {
it('errors for container creation with path to non container.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ 'foo' ]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
@ -161,33 +170,39 @@ describe('A FileResourceStore', (): void => {
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
});
it('errors 405 for POST invalid path ending without slash.', async(): Promise<void> => {
it('errors 404 for POST invalid path ending without slash and 405 for valid.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
representation.metadata.add(HTTP.slug, 'myContainer/');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
.rejects.toThrow(NotFoundHttpError);
expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
representation.metadata.set(RDF.type, LDP.Resource);
representation.metadata.set(HTTP.slug, 'file.txt');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
.rejects.toThrow(NotFoundHttpError);
expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ 'existingresource' ]);
representation.metadata.removeAll(RDF.type);
await expect(store.addResource({ path: `${base}existingresource` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
expect(fsPromises.lstat as jest.Mock).toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.mkdir as jest.Mock).mockImplementation((): void => {
throw new Error('not a directory');
});
representation.metadata.removeAll(RDF.type);
await expect(store.addResource({ path: `${base}existingresource/container/` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock)
.toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
});
it('can set data.', async(): Promise<void> => {
@ -203,6 +218,7 @@ describe('A FileResourceStore', (): void => {
// Mock: Get
stats = { ...stats };
stats.isFile = jest.fn((): any => true);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([]));
@ -228,6 +244,7 @@ describe('A FileResourceStore', (): void => {
it('can delete data.', async(): Promise<void> => {
// Mock the fs functions.
// Delete
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
stats.isFile = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
@ -236,6 +253,7 @@ describe('A FileResourceStore', (): void => {
});
// Mock: Get
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ ]);
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
@ -325,6 +343,7 @@ describe('A FileResourceStore', (): void => {
it('errors 404 when accessing non resource (file/directory), e.g. special files.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockReturnValue(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ '14' ]);
// Tests
await expect(store.deleteResource({ path: `${base}dev/pts/14` })).rejects.toThrow(NotFoundHttpError);
@ -476,6 +495,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ '.htaccess' ]);
const result = await store.getRepresentation({ path: `${base}.htaccess` });
expect(result).toEqual({
@ -516,6 +536,7 @@ describe('A FileResourceStore', (): void => {
stats.isDirectory = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.mkdir as jest.Mock).mockReturnValue(true);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'foo' ]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
@ -545,6 +566,8 @@ describe('A FileResourceStore', (): void => {
const filePath: string = (fs.createWriteStream as jest.Mock).mock.calls[0][0];
expect(filePath.startsWith(rootFilepath)).toBeTruthy();
const name = filePath.slice(rootFilepath.length);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ name ]);
const result = await store.getRepresentation({ path: `${base}${name}` });
expect(result).toEqual({
binary: true,