Skip to content

Commit

Permalink
Merge 009cde1 into 0998970
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Apr 22, 2024
2 parents 0998970 + 009cde1 commit c1374b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
31 changes: 19 additions & 12 deletions src/pods/generate/BaseResourcesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,36 @@ export class BaseResourcesGenerator implements TemplatedResourcesGenerator {
data = await this.processFile(link, options);
metadata.contentType = link.contentType;
}
// Do not yield a container resource if it already exists
if (!isContainerIdentifier(link.identifier) || !await this.store.hasResource(link.identifier)) {

// Add metadata from .meta file if there is one
if (metaLink) {
const rawMetadata = await this.generateMetadata(metaLink, options);
if (rawMetadata.contentType) {
// Prevent having 2 content types
metadata.contentType = undefined;
}
metadata.setMetadata(rawMetadata);
this.logger.debug(`Adding metadata for ${metaLink.identifier.path}`);
}

const shouldYield = !isContainerIdentifier(link.identifier) || !await this.store.hasResource(link.identifier);
if (shouldYield) {
this.logger.debug(`Generating resource ${link.identifier.path}`);
yield {
identifier: link.identifier,
representation: new BasicRepresentation(data ?? [], metadata),
};
}

// Add metadata from .meta file if there is one
if (metaLink) {
const rawMetadata = await this.generateMetadata(metaLink, options);
if (!rawMetadata.contentType) {
// Make sure this does not remove the content-type if none is explicitly defined
rawMetadata.contentType = metadata.contentType;
}
// Still need to yield metadata in case the actual resource is not being yielded.
// We also do this for containers as existing containers can't be edited in the same way.
if (metaLink && (!shouldYield || isContainerIdentifier(link.identifier))) {
const metaIdentifier = this.metadataStrategy.getAuxiliaryIdentifier(link.identifier);
const descriptionMeta = new RepresentationMetadata(metaIdentifier);
addResourceMetadata(rawMetadata, isContainerIdentifier(link.identifier));
addResourceMetadata(metadata, isContainerIdentifier(link.identifier));
this.logger.debug(`Generating resource ${metaIdentifier.path}`);
yield {
identifier: metaIdentifier,
representation: new BasicRepresentation(rawMetadata.quads(), descriptionMeta, INTERNAL_QUADS),
representation: new BasicRepresentation(metadata.quads(), metaIdentifier, INTERNAL_QUADS),
};
}
}
Expand Down
24 changes: 12 additions & 12 deletions test/unit/pods/generate/BaseResourcesGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DataFactory } from 'n3';
import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier';
import { BaseResourcesGenerator } from '../../../../src/pods/generate/BaseResourcesGenerator';
import type {
Expand All @@ -10,7 +11,6 @@ import { asyncToArray } from '../../../../src/util/IterableUtil';
import { ensureTrailingSlash, joinFilePath, trimTrailingSlashes } from '../../../../src/util/PathUtil';
import { readableToQuads, readableToString } from '../../../../src/util/StreamUtil';
import { HandlebarsTemplateEngine } from '../../../../src/util/templates/HandlebarsTemplateEngine';
import { CONTENT_TYPE_TERM } from '../../../../src/util/Vocabularies';
import { SimpleSuffixStrategy } from '../../../util/SimpleSuffixStrategy';
import { mockFileSystem } from '../../../util/Util';

Expand Down Expand Up @@ -110,7 +110,8 @@ describe('A BaseResourcesGenerator', (): void => {

it('adds metadata from .meta files.', async(): Promise<void> => {
const meta = '<> <pre:has> "metadata".';
cache.data = { '.meta': meta, container: { 'template.meta': meta, template }};
const metaType = '<> <http://www.w3.org/ns/ma-ont#format> "text/plain".';
cache.data = { '.meta': meta, container: { 'template.meta': meta, template, type: 'dummy', 'type.meta': metaType }};

// Not using options since our dummy template generator generates invalid turtle
const result = await asyncToArray(generator.generate(rootFilePath, location, { webId }));
Expand All @@ -120,7 +121,7 @@ describe('A BaseResourcesGenerator', (): void => {
{ path: `${location.path}.meta` },
{ path: `${location.path}container/` },
{ path: `${location.path}container/template` },
{ path: `${location.path}container/template.meta` },
{ path: `${location.path}container/type` },
]);

// Root has the 1 raw metadata triple (with <> changed to its identifier) and content-type
Expand All @@ -137,18 +138,17 @@ describe('A BaseResourcesGenerator', (): void => {
expect(contMetadata.identifier.value).toBe(`${location.path}container/`);
expect(contMetadata.quads()).toHaveLength(0);

// Document has the 1 raw metadata triple (with <> changed to its identifier) and content-type
// Document has the new metadata
const docMetadata = result[3].representation.metadata;
expect(docMetadata.identifier.value).toBe(`${location.path}container/template`);
expect(docMetadata.contentType).toBe('text/turtle');
const docMetadataQuads = await readableToQuads(result[4].representation.data);
const expDocMetadataQuads = docMetadataQuads.getQuads(docMetadata.identifier, 'pre:has', null, null);
expect(expDocMetadataQuads).toHaveLength(1);
expect(expDocMetadataQuads[0].object.value).toBe('metadata');
// Metadata will replace existing metadata so need to make sure content-type is still there
const contentDocMetadataQuads = docMetadataQuads.getQuads(docMetadata.identifier, CONTENT_TYPE_TERM, null, null);
expect(contentDocMetadataQuads).toHaveLength(1);
expect(contentDocMetadataQuads[0].object.value).toBe('text/turtle');
expect(docMetadata.contentType).toBe('text/turtle');
expect(docMetadata.get(DataFactory.namedNode('pre:has'))?.value).toBe('metadata');

// Type document has new content type
const typeMetadata = result[4].representation.metadata;
expect(typeMetadata.identifier.value).toBe(`${location.path}container/type`);
expect(typeMetadata.contentType).toBe('text/plain');
});

it('does not create container when it already exists.', async(): Promise<void> => {
Expand Down

0 comments on commit c1374b1

Please sign in to comment.