-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { types } from 'mime-types'; | ||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError'; | ||
import { FileResourceStore } from './FileResourceStore'; | ||
import { ResourceMapper } from './ResourceMapper'; | ||
|
||
export class FileResourceMapper implements ResourceMapper { | ||
private readonly fileStore: FileResourceStore; | ||
private readonly types: Record<string, any>; | ||
|
||
public constructor(fileStore: FileResourceStore, overrideTypes = { acl: 'text/turtle', metadata: 'text/turtle' }) { | ||
this.fileStore = fileStore; | ||
this.types = { ...types, ...overrideTypes }; | ||
} | ||
|
||
/** | ||
* Strips the baseRequestURI from the identifier and checks if the stripped base URI matches the store's one. | ||
* @param identifier - Incoming identifier. | ||
* | ||
* @throws {@link NotFoundHttpError} | ||
* If the identifier does not match the baseRequestURI path of the store. | ||
*/ | ||
public mapUrlToFilePath(identifier: ResourceIdentifier): string { | ||
if (!identifier.path.startsWith(this.fileStore.baseRequestURI)) { | ||
throw new NotFoundHttpError(); | ||
} | ||
return identifier.path.slice(this.fileStore.baseRequestURI.length); | ||
} | ||
|
||
/** | ||
* Strips the rootFilepath path from the filepath and adds the baseRequestURI in front of it. | ||
* @param path - The filepath. | ||
* | ||
* @throws {@Link Error} | ||
* If the filepath does not match the rootFilepath path of the store. | ||
*/ | ||
public mapFilePathToUrl(path: string): string { | ||
if (!path.startsWith(this.fileStore.rootFilepath)) { | ||
throw new Error(`File ${path} is not part of the file storage at ${this.fileStore.rootFilepath}.`); | ||
} | ||
return this.fileStore.baseRequestURI + path.slice(this.fileStore.rootFilepath.length); | ||
} | ||
|
||
public getContentTypeFromExtension(path: string): string { | ||
const extension = /\.([^./]+)$/u.exec(path); | ||
return (extension && this.types[extension[1].toLowerCase()]) || false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata'; | ||
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
|
||
/** | ||
* Supports mapping a file to an URL and back. | ||
*/ | ||
export interface ResourceMapper { | ||
/** | ||
* Maps the given file to an URL. | ||
* @param file - The input file. | ||
* Maps the given file path to an URL. | ||
* @param file - The input file path. | ||
* | ||
* @returns A promise resolving to the corresponding URL and metadata of the representation. | ||
* @returns The URL as a string. | ||
*/ | ||
mapFilePathToUrl: (file: File) => Promise<{ url: URL; metadata: RepresentationMetadata }>; | ||
mapFilePathToUrl: (filePath: string) => string; | ||
/** | ||
* Maps the given URL and metadata to a file. | ||
* Maps the given resource identifier / URL to a file path. | ||
* @param url - The input URL. | ||
* @param metadata - The representation metadata. | ||
* | ||
* @returns A promise resolving to the corresponding file. | ||
* @returns The file path as a string. | ||
*/ | ||
mapUrlToFilePath: (url: URL, metadata: RepresentationMetadata) => Promise<File>; | ||
mapUrlToFilePath: (identifier: ResourceIdentifier) => string; | ||
/** | ||
* Maps the given path to a contentType; | ||
* @param path - The input file path. | ||
* | ||
* @returns The content type as a string. | ||
*/ | ||
getContentTypeFromExtension: (path: string) => string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters