mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Allow filtering in ConstantConverter based on type
This commit is contained in:
@@ -23,6 +23,14 @@ export interface ConstantConverterOptions {
|
||||
* The minimum requested quality/preference before this should trigger.
|
||||
*/
|
||||
minQuality?: number;
|
||||
/**
|
||||
* Media ranges for which the conversion should happen.
|
||||
*/
|
||||
enabledMediaRanges?: string[];
|
||||
/**
|
||||
* Media ranges for which the conversion should not happen.
|
||||
*/
|
||||
disabledMediaRanges?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,6 +65,8 @@ export class ConstantConverter extends RepresentationConverter {
|
||||
container: options.container ?? true,
|
||||
document: options.document ?? true,
|
||||
minQuality: options.minQuality ?? 0,
|
||||
enabledMediaRanges: options.enabledMediaRanges ?? [ '*/*' ],
|
||||
disabledMediaRanges: options.disabledMediaRanges ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,10 +93,19 @@ export class ConstantConverter extends RepresentationConverter {
|
||||
throw new NotImplementedHttpError(`Preference is lower than the specified minimum quality`);
|
||||
}
|
||||
|
||||
const sourceContentType = representation.metadata.contentType ?? '';
|
||||
// Do not replace the representation if it already has our content type
|
||||
if (matchesMediaType(representation.metadata.contentType ?? '', this.contentType)) {
|
||||
if (matchesMediaType(sourceContentType, this.contentType)) {
|
||||
throw new NotImplementedHttpError(`Representation is already ${this.contentType}`);
|
||||
}
|
||||
|
||||
// Only replace the representation if it matches the media range settings
|
||||
if (!this.options.enabledMediaRanges.some((type): boolean => matchesMediaType(sourceContentType, type))) {
|
||||
throw new NotImplementedHttpError(`${sourceContentType} is not one of the enabled media types.`);
|
||||
}
|
||||
if (this.options.disabledMediaRanges.some((type): boolean => matchesMediaType(sourceContentType, type))) {
|
||||
throw new NotImplementedHttpError(`${sourceContentType} is one of the disabled media types.`);
|
||||
}
|
||||
}
|
||||
|
||||
public async handle({ representation }: RepresentationConverterArgs): Promise<Representation> {
|
||||
|
||||
Reference in New Issue
Block a user