feat: ExtensionBasedMapper no longer throws if there is no file

This commit is contained in:
Joachim Van Herwegen
2020-12-15 14:32:34 +01:00
parent 36eed5d620
commit d7434df808
5 changed files with 33 additions and 22 deletions

View File

@@ -350,8 +350,8 @@ export class FileDataAccessor implements DataAccessor {
await fsPromises.unlink(oldLink.filePath);
}
} catch (error: unknown) {
// Ignore it if the file didn't exist yet
if (!(error instanceof NotFoundHttpError)) {
// Ignore it if the file didn't exist yet and couldn't be unlinked
if (!isSystemError(error) || error.code !== 'ENOENT') {
throw error;
}
}

View File

@@ -4,7 +4,6 @@ import * as mime from 'mime-types';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { getLoggerFor } from '../../logging/LogUtil';
import { APPLICATION_OCTET_STREAM, TEXT_TURTLE } from '../../util/ContentTypes';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import {
encodeUriPathComponents,
@@ -98,22 +97,18 @@ export class ExtensionBasedMapper implements FileIdentifierMapper {
);
} catch {
// Parent folder does not exist (or is not a folder)
this.logger.warn(`No parent folder for ${identifier.path} found at ${folder}`);
throw new NotFoundHttpError();
}
// File doesn't exist
if (!fileName) {
this.logger.warn(`File for URL ${identifier.path} does not exist in ${folder}`);
throw new NotFoundHttpError();
// Matching file found
if (fileName) {
filePath = joinPath(folder, fileName);
}
filePath = joinPath(folder, fileName);
this.logger.info(`The path for ${identifier.path} is ${filePath}`);
return {
identifier,
filePath,
contentType: this.getContentTypeFromExtension(fileName),
contentType: this.getContentTypeFromExtension(filePath),
};
}

View File

@@ -29,7 +29,8 @@ export interface FileIdentifierMapper {
mapFilePathToUrl: (filePath: string, isContainer: boolean) => Promise<ResourceLink>;
/**
* Maps the given resource identifier / URL to a file path.
* Determines the content-type if no content-type was provided.
* Determines the content-type if no content-type was provided by finding the corresponding file.
* If there is no corresponding file a file path will be generated.
* For containers the content-type input gets ignored.
* @param identifier - The input identifier.
* @param contentType - The (optional) content-type of the resource.