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
9 changed files
with
739 additions
and
13 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Representation } from '../ldp/representation/Representation'; | ||
import type { RepresentationPreference } from '../ldp/representation/RepresentationPreference'; | ||
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier'; | ||
import type { Conditions } from './Conditions'; | ||
import type { RepresentationConverter } from './conversion/RepresentationConverter'; | ||
import { PassthroughStore } from './PassthroughStore'; | ||
import type { ResourceStore } from './ResourceStore'; | ||
|
||
/** | ||
* Store that converts incoming data when required. | ||
* If the content-type of an incoming representation does not match one of the stored types it will be converted. | ||
*/ | ||
export class FixedConvertingStore extends PassthroughStore { | ||
private readonly types: string[]; | ||
private readonly converter: RepresentationConverter; | ||
|
||
public constructor(source: ResourceStore, converter: RepresentationConverter, types: string[]) { | ||
super(source); | ||
this.converter = converter; | ||
this.types = types; | ||
} | ||
|
||
public async addResource(container: ResourceIdentifier, representation: Representation, | ||
conditions?: Conditions): Promise<ResourceIdentifier> { | ||
// We can potentially run into problems here if we convert a turtle document where the base IRI is required, | ||
// since we don't know the resource IRI yet at this point. | ||
representation = await this.convertRepresentation(container, representation); | ||
return this.source.addResource(container, representation, conditions); | ||
} | ||
|
||
public async setRepresentation(identifier: ResourceIdentifier, representation: Representation, | ||
conditions?: Conditions): Promise<void> { | ||
representation = await this.convertRepresentation(identifier, representation); | ||
return this.source.setRepresentation(identifier, representation, conditions); | ||
} | ||
|
||
private async convertRepresentation(identifier: ResourceIdentifier, representation: Representation): | ||
Promise<Representation> { | ||
if (this.types.includes(representation.metadata.contentType!)) { | ||
return representation; | ||
} | ||
const preferences = this.types.map((type): RepresentationPreference => ({ value: type, weight: 1 })); | ||
return this.converter.handleSafe({ identifier, representation, preferences: { type: preferences }}); | ||
} | ||
} |
Oops, something went wrong.