Skip to content

Commit

Permalink
feat: Add ContentTypeReplacer to conversion chain
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Nov 8, 2021
1 parent fa94c7d commit fdd42bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/util/representation-conversion/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"@type": "IfNeededConverter",
"comment": "Only continue converting if the requester cannot accept the available content type"
},
{ "@id": "urn:solid-server:default:ContentTypeReplacer" },
{
"comment": "Automatically finds a path through a set of converters from one type to another.",
"@id": "urn:solid-server:default:ChainedConverter",
"@type": "ChainedConverter",
"converters": [
{ "@id": "urn:solid-server:default:ContentTypeReplacer" },
{ "@id": "urn:solid-server:default:RdfToQuadConverter" },
{ "@id": "urn:solid-server:default:QuadToRdfConverter" },
{ "@id": "urn:solid-server:default:ContainerToTemplateConverter" },
Expand Down
23 changes: 14 additions & 9 deletions src/storage/conversion/ContentTypeReplacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ValuePreferences } from '../../http/representation/RepresentationP
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
import { matchesMediaType, getConversionTarget } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { RepresentationConverter } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';

/**
* A {@link RepresentationConverter} that changes the content type
Expand All @@ -13,7 +13,7 @@ import { RepresentationConverter } from './RepresentationConverter';
* Useful for when a content type is binary-compatible with another one;
* for instance, all JSON-LD files are valid JSON files.
*/
export class ContentTypeReplacer extends RepresentationConverter {
export class ContentTypeReplacer extends TypedRepresentationConverter {
private readonly contentTypeMap: Record<string, ValuePreferences> = {};

/**
Expand All @@ -40,15 +40,22 @@ export class ContentTypeReplacer extends RepresentationConverter {
}
}

public async getOutputTypes(contentType: string): Promise<ValuePreferences> {
const supported = Object.keys(this.contentTypeMap)
.filter((type): boolean => matchesMediaType(contentType, type))
.map((type): ValuePreferences => this.contentTypeMap[type]);
return Object.assign({} as ValuePreferences, ...supported);
}

public async canHandle({ representation, preferences }: RepresentationConverterArgs): Promise<void> {
this.getReplacementType(representation.metadata.contentType, preferences.type);
await this.getReplacementType(representation.metadata.contentType, preferences.type);
}

/**
* Changes the content type on the representation.
*/
public async handle({ representation, preferences }: RepresentationConverterArgs): Promise<Representation> {
const contentType = this.getReplacementType(representation.metadata.contentType, preferences.type);
const contentType = await this.getReplacementType(representation.metadata.contentType, preferences.type);
const metadata = new RepresentationMetadata(representation.metadata, contentType);
return { ...representation, metadata };
}
Expand All @@ -61,11 +68,9 @@ export class ContentTypeReplacer extends RepresentationConverter {
* Find a replacement content type that matches the preferences,
* or throws an error if none was found.
*/
private getReplacementType(contentType = 'unknown', preferred: ValuePreferences = {}): string {
const supported = Object.keys(this.contentTypeMap)
.filter((type): boolean => matchesMediaType(contentType, type))
.map((type): ValuePreferences => this.contentTypeMap[type]);
const match = getConversionTarget(Object.assign({} as ValuePreferences, ...supported), preferred);
private async getReplacementType(contentType = 'unknown', preferred: ValuePreferences = {}): Promise<string> {
const supported = await this.getOutputTypes(contentType);
const match = getConversionTarget(supported, preferred);
if (!match) {
throw new NotImplementedHttpError(`Cannot convert from ${contentType} to ${Object.keys(preferred)}`);
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/storage/conversion/ContentTypeReplacer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ describe('A ContentTypeReplacer', (): void => {
expect(result.data).toBe(data);
expect(result.metadata.contentType).toBe('application/trig');
});

it('returns all matching output types.', async(): Promise<void> => {
await expect(converter.getOutputTypes('application/n-triples')).resolves.toEqual({
'text/turtle': 1,
'application/trig': 1,
'application/n-quads': 1,
'application/octet-stream': 1,
'internal/anything': 1,
});
});
});

0 comments on commit fdd42bb

Please sign in to comment.