mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
Add generic QuadToRdfConverter
This commit is contained in:
committed by
Joachim Van Herwegen
parent
4273c8bc04
commit
2c3ae4eded
31
src/storage/conversion/QuadToRdfConverter.ts
Normal file
31
src/storage/conversion/QuadToRdfConverter.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Readable } from 'stream';
|
||||
import rdfSerializer from 'rdf-serialize';
|
||||
import { Representation } from '../../ldp/representation/Representation';
|
||||
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
|
||||
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../util/ContentTypes';
|
||||
import { checkRequest, matchingTypes } from './ConversionUtil';
|
||||
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Converts `internal/quads` to most major RDF serializations.
|
||||
*/
|
||||
export class QuadToRdfConverter extends RepresentationConverter {
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
checkRequest(input, [ CONTENT_TYPE_QUADS ], await rdfSerializer.getContentTypes());
|
||||
}
|
||||
|
||||
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||
return this.quadsToRdf(input.representation, input.preferences);
|
||||
}
|
||||
|
||||
private async quadsToRdf(quads: Representation, preferences: RepresentationPreferences): Promise<Representation> {
|
||||
const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value;
|
||||
const metadata: RepresentationMetadata = { ...quads.metadata, contentType };
|
||||
return {
|
||||
dataType: DATA_TYPE_BINARY,
|
||||
data: rdfSerializer.serialize(quads.data, { contentType }) as Readable,
|
||||
metadata,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user