feat: Add containsResource utility.

This commit is contained in:
Ruben Verborgh 2021-01-15 00:12:27 +01:00 committed by Joachim Van Herwegen
parent ee50f40062
commit af47083f64
5 changed files with 27 additions and 61 deletions

View File

@ -3,8 +3,8 @@ import { BasicRepresentation } from '../ldp/representation/BasicRepresentation';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { getLoggerFor } from '../logging/LogUtil';
import type { ResourceStore } from '../storage/ResourceStore';
import { containsResource } from '../storage/StoreUtil';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/PathUtil';
import { Initializer } from './Initializer';
@ -30,22 +30,10 @@ export class AclInitializer extends Initializer {
public async handle(): Promise<void> {
const rootAcl = await this.aclManager.getAclDocument({ path: this.baseUrl });
if (!await this.hasRootAclDocument(rootAcl)) {
if (!await containsResource(this.store, rootAcl)) {
await this.setRootAclDocument(rootAcl);
}
}
protected async hasRootAclDocument(rootAcl: ResourceIdentifier): Promise<boolean> {
try {
const result = await this.store.getRepresentation(rootAcl, {});
} else {
this.logger.debug(`Existing root ACL document found at ${rootAcl.path}`);
result.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}

View File

@ -4,8 +4,8 @@ import { RepresentationMetadata } from '../ldp/representation/RepresentationMeta
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { getLoggerFor } from '../logging/LogUtil';
import type { ResourceStore } from '../storage/ResourceStore';
import { containsResource } from '../storage/StoreUtil';
import { TEXT_TURTLE } from '../util/ContentTypes';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { ensureTrailingSlash } from '../util/PathUtil';
import { generateResourceQuads } from '../util/ResourceUtil';
import { PIM, RDF } from '../util/Vocabularies';
@ -31,27 +31,12 @@ export class RootContainerInitializer extends Initializer {
}
public async handle(): Promise<void> {
if (!await this.hasRootContainer()) {
this.logger.debug(`Checking for root container at ${this.baseId.path}`);
if (!await containsResource(this.store, this.baseId)) {
await this.createRootContainer();
}
}
/**
* Verify if a root container already exists in a ResourceStore.
*/
protected async hasRootContainer(): Promise<boolean> {
try {
this.logger.debug(`Checking for root container at ${this.baseId.path}`);
const result = await this.store.getRepresentation(this.baseId, {});
} else {
this.logger.debug(`Existing root container found at ${this.baseId.path}`);
result.data.destroy();
return true;
} catch (error: unknown) {
if (!(error instanceof NotFoundHttpError)) {
throw error;
}
}
return false;
}
/**

View File

@ -1,8 +1,8 @@
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { getLoggerFor } from '../logging/LogUtil';
import type { ResourceStore } from '../storage/ResourceStore';
import { containsResource } from '../storage/StoreUtil';
import { ConflictHttpError } from '../util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import type { Agent } from './agent/Agent';
import type { IdentifierGenerator } from './generate/IdentifierGenerator';
import type { ResourcesGenerator } from './generate/ResourcesGenerator';
@ -33,15 +33,8 @@ export class GeneratedPodManager implements PodManager {
public async createPod(agent: Agent): Promise<ResourceIdentifier> {
const podIdentifier = this.idGenerator.generate(agent.login);
this.logger.info(`Creating pod ${podIdentifier.path}`);
try {
const result = await this.store.getRepresentation(podIdentifier, {});
result.data.destroy();
if (await containsResource(this.store, podIdentifier)) {
throw new ConflictHttpError(`There already is a resource at ${podIdentifier.path}`);
} catch (error: unknown) {
// We want the identifier to not exist
if (!(error instanceof NotFoundHttpError)) {
throw error;
}
}
const resources = this.resourcesGenerator.generate(podIdentifier, agent);

16
src/storage/StoreUtil.ts Normal file
View File

@ -0,0 +1,16 @@
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import type { ResourceStore } from '../storage/ResourceStore';
import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
export async function containsResource(store: ResourceStore, identifier: ResourceIdentifier): Promise<boolean> {
try {
const result = await store.getRepresentation(identifier, {});
result.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}

View File

@ -1,6 +1,6 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
import { containsResource } from '../../storage/StoreUtil';
import type { ResourceStore } from '../ResourceStore';
import type { PreferenceSupport } from './PreferenceSupport';
import { RouterRule } from './RouterRule';
@ -41,27 +41,11 @@ export class ConvertingRouterRule extends RouterRule {
} else {
// No content-type given so we can only check if one of the stores has data for the identifier
store = await this.findStore(async(entry): Promise<boolean> =>
this.hasResource(entry.store, input.identifier));
containsResource(entry.store, input.identifier));
}
return store;
}
/**
* Helper function that checks if the given store contains the given identifier or not.
*/
private async hasResource(store: ResourceStore, identifier: ResourceIdentifier): Promise<boolean> {
try {
const response = await store.getRepresentation(identifier, {});
response.data.destroy();
return true;
} catch (error: unknown) {
if (error instanceof NotFoundHttpError) {
return false;
}
throw error;
}
}
/**
* Helper function that runs the given callback function for all the stores
* and returns the first one that does not throw an error.