change: Make RepresentationMetadata accept a ResourceIdentifier.

Closes https://github.com/solid/community-server/issues/388
This commit is contained in:
Ruben Verborgh
2020-12-10 16:24:38 +00:00
committed by Joachim Van Herwegen
parent 6ee56a6d67
commit accfc2e58d
13 changed files with 53 additions and 39 deletions

View File

@@ -66,7 +66,7 @@ export class AclInitializer extends Initializer {
acl:mode acl:Control;
acl:accessTo <${this.baseUrl}>;
acl:default <${this.baseUrl}>.`;
const metadata = new RepresentationMetadata(rootAcl.path, { [CONTENT_TYPE]: TEXT_TURTLE });
const metadata = new RepresentationMetadata(rootAcl, { [CONTENT_TYPE]: TEXT_TURTLE });
this.logger.debug(`Installing root ACL document at ${rootAcl.path}`);
await this.store.setRepresentation(
rootAcl,

View File

@@ -2,7 +2,10 @@ import { DataFactory, Store } from 'n3';
import type { BlankNode, Literal, NamedNode, Quad, Term } from 'rdf-js';
import { getLoggerFor } from '../../logging/LogUtil';
import { toObjectTerm, toNamedNode, isTerm } from '../../util/UriUtil';
import type { ResourceIdentifier } from './ResourceIdentifier';
import { isResourceIdentifier } from './ResourceIdentifier';
export type MetadataIdentifier = ResourceIdentifier | NamedNode | BlankNode;
export type MetadataOverrideValue = NamedNode | Literal | string | (NamedNode | Literal | string)[];
/**
@@ -23,7 +26,7 @@ export class RepresentationMetadata {
*
* `@ignored` tag is necessary for Components-Generator.js
*/
public constructor(identifier?: NamedNode | BlankNode | string, overrides?: Record<string, MetadataOverrideValue>);
public constructor(identifier?: MetadataIdentifier, overrides?: Record<string, MetadataOverrideValue>);
/**
* @param metadata - Starts as a copy of the input metadata.
@@ -38,12 +41,12 @@ export class RepresentationMetadata {
public constructor(overrides?: Record<string, MetadataOverrideValue>);
public constructor(
input?: NamedNode | BlankNode | string | RepresentationMetadata | Record<string, MetadataOverrideValue>,
input?: MetadataIdentifier | RepresentationMetadata | Record<string, MetadataOverrideValue>,
overrides?: Record<string, MetadataOverrideValue>,
) {
this.store = new Store();
if (typeof input === 'string') {
this.id = DataFactory.namedNode(input);
if (isResourceIdentifier(input)) {
this.id = DataFactory.namedNode(input.path);
} else if (isTerm(input)) {
this.id = input;
} else if (input instanceof RepresentationMetadata) {

View File

@@ -7,3 +7,10 @@ export interface ResourceIdentifier {
*/
path: string;
}
/**
* Determines whether the object is a `ResourceIdentifier`.
*/
export const isResourceIdentifier = function(object: any): object is ResourceIdentifier {
return object && (typeof object.path === 'string');
};

View File

@@ -51,7 +51,7 @@ export class TemplatedResourcesGenerator implements ResourcesGenerator {
representation: {
binary: true,
data: guardedStreamFrom([]),
metadata: new RepresentationMetadata(link.identifier.path),
metadata: new RepresentationMetadata(link.identifier),
},
};
@@ -74,7 +74,7 @@ export class TemplatedResourcesGenerator implements ResourcesGenerator {
private async generateDocument(filePath: string, mapper: FileIdentifierMapper, options: Dict<string>):
Promise<Resource> {
const link = await mapper.mapFilePathToUrl(filePath, false);
const metadata = new RepresentationMetadata(link.identifier.path);
const metadata = new RepresentationMetadata(link.identifier);
metadata.contentType = link.contentType;
const raw = await fsPromises.readFile(filePath, 'utf8');

View File

@@ -370,7 +370,7 @@ export class DataAccessorBasedStore implements ResourceStore {
return {
binary: true,
data: guardedStreamFrom([]),
metadata: new RepresentationMetadata(container.path),
metadata: new RepresentationMetadata(container),
};
}
}

View File

@@ -246,7 +246,7 @@ export class FileDataAccessor implements DataAccessor {
*/
private async getBaseMetadata(link: ResourceLink, stats: Stats, isContainer: boolean):
Promise<RepresentationMetadata> {
const metadata = new RepresentationMetadata(link.identifier.path)
const metadata = new RepresentationMetadata(link.identifier)
.addQuads(await this.getRawMetadata(link.identifier));
metadata.addQuads(generateResourceQuads(metadata.identifier as NamedNode, isContainer));
metadata.addQuads(this.generatePosixQuads(metadata.identifier as NamedNode, stats));

View File

@@ -28,7 +28,7 @@ export class InMemoryDataAccessor implements DataAccessor {
public constructor(base: string) {
this.base = ensureTrailingSlash(base);
const metadata = new RepresentationMetadata(this.base);
const metadata = new RepresentationMetadata({ path: this.base });
metadata.addQuads(generateResourceQuads(DataFactory.namedNode(this.base), true));
this.store = { entries: {}, metadata };
}

View File

@@ -94,7 +94,7 @@ export class SparqlDataAccessor implements DataAccessor {
throw new NotFoundHttpError();
}
const metadata = new RepresentationMetadata(identifier.path).addQuads(quads);
const metadata = new RepresentationMetadata(identifier).addQuads(quads);
if (!isContainerIdentifier(identifier)) {
metadata.contentType = INTERNAL_QUADS;
}

View File

@@ -108,7 +108,7 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
this.logger.debug(`${store.size} quads will be stored to ${identifier.path}.`);
// Write the result
const metadata = new RepresentationMetadata(identifier.path, { [CONTENT_TYPE]: INTERNAL_QUADS });
const metadata = new RepresentationMetadata(identifier, { [CONTENT_TYPE]: INTERNAL_QUADS });
const representation: Representation = {
binary: false,
data: guardStream(store.match() as Readable),