mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Stop creating meta files for each new resource #1217
This commit is contained in:
committed by
Joachim Van Herwegen
parent
f0f900edfb
commit
fbbccb0cf1
@@ -287,6 +287,7 @@ export * from './storage/accessors/AtomicDataAccessor';
|
||||
export * from './storage/accessors/AtomicFileDataAccessor';
|
||||
export * from './storage/accessors/DataAccessor';
|
||||
export * from './storage/accessors/FileDataAccessor';
|
||||
export * from './storage/accessors/FilterMetadataDataAccessor';
|
||||
export * from './storage/accessors/InMemoryDataAccessor';
|
||||
export * from './storage/accessors/PassthroughDataAccessor';
|
||||
export * from './storage/accessors/SparqlDataAccessor';
|
||||
|
||||
55
src/storage/accessors/FilterMetadataDataAccessor.ts
Normal file
55
src/storage/accessors/FilterMetadataDataAccessor.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { Readable } from 'stream';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import type { FilterPattern } from '../../util/QuadUtil';
|
||||
import type { DataAccessor } from './DataAccessor';
|
||||
import { PassthroughDataAccessor } from './PassthroughDataAccessor';
|
||||
|
||||
/**
|
||||
* A FilterMetadataDataAccessor wraps a DataAccessor such that specific metadata properties
|
||||
* can be filtered before passing on the call to the wrapped DataAccessor.
|
||||
*/
|
||||
export class FilterMetadataDataAccessor extends PassthroughDataAccessor {
|
||||
private readonly filters: FilterPattern[];
|
||||
|
||||
/**
|
||||
* Construct an instance of FilterMetadataDataAccessor.
|
||||
*
|
||||
* @param accessor - The DataAccessor to wrap.
|
||||
* @param filters - Filter patterns to be used for metadata removal.
|
||||
*/
|
||||
public constructor(accessor: DataAccessor, filters: FilterPattern[]) {
|
||||
super(accessor);
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
public async writeDocument(
|
||||
identifier: ResourceIdentifier,
|
||||
data: Guarded<Readable>,
|
||||
metadata: RepresentationMetadata,
|
||||
): Promise<void> {
|
||||
this.applyFilters(metadata);
|
||||
return this.accessor.writeDocument(identifier, data, metadata);
|
||||
}
|
||||
|
||||
public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise<void> {
|
||||
this.applyFilters(metadata);
|
||||
return this.accessor.writeContainer(identifier, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function that removes metadata entries,
|
||||
* based on the configured filter patterns.
|
||||
*
|
||||
* @param metadata - Metadata for the request.
|
||||
*/
|
||||
private applyFilters(metadata: RepresentationMetadata): void {
|
||||
for (const filter of this.filters) {
|
||||
// Find the matching quads.
|
||||
const matchingQuads = metadata.quads(filter.subject, filter.predicate, filter.object);
|
||||
// Remove the resulset.
|
||||
metadata.removeQuads(matchingQuads);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import type { Readable } from 'stream';
|
||||
import type { NamedNode } from '@rdfjs/types';
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import type { ParserOptions } from 'n3';
|
||||
import { StreamParser, StreamWriter } from 'n3';
|
||||
import type { Quad } from 'rdf-js';
|
||||
import type { Guarded } from './GuardedStream';
|
||||
import { guardedStreamFrom, pipeSafely } from './StreamUtil';
|
||||
import { toNamedTerm } from './TermUtil';
|
||||
|
||||
/**
|
||||
* Helper function for serializing an array of quads, with as result a Readable object.
|
||||
@@ -42,3 +44,23 @@ export function uniqueQuads(quads: Quad[]): Quad[] {
|
||||
return result;
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a triple pattern to be used as a filter.
|
||||
*/
|
||||
export class FilterPattern {
|
||||
public readonly subject: NamedNode | null;
|
||||
public readonly predicate: NamedNode | null;
|
||||
public readonly object: NamedNode | null;
|
||||
|
||||
/**
|
||||
* @param subject - Optionally filter based on a specific subject.
|
||||
* @param predicate - Optionally filter based on a predicate.
|
||||
* @param object - Optionally filter based on a specific object.
|
||||
*/
|
||||
public constructor(subject?: string, predicate?: string, object?: string) {
|
||||
this.subject = typeof subject !== 'undefined' ? toNamedTerm(subject) : null;
|
||||
this.predicate = typeof predicate !== 'undefined' ? toNamedTerm(predicate) : null;
|
||||
this.object = typeof object !== 'undefined' ? toNamedTerm(object) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,6 +179,7 @@ export const XSD = createUriAndTermNamespace('http://www.w3.org/2001/XMLSchema#'
|
||||
);
|
||||
|
||||
// Alias for commonly used types
|
||||
export const CONTENT_LENGTH = HH['content-length'];
|
||||
export const CONTENT_LENGTH_TERM = HH.terms['content-length'];
|
||||
export const CONTENT_TYPE = MA.format;
|
||||
export const CONTENT_TYPE_TERM = MA.terms.format;
|
||||
|
||||
Reference in New Issue
Block a user