diff --git a/src/init/AclInitializer.ts b/src/init/AclInitializer.ts index b310b1b30..fe8632f5f 100644 --- a/src/init/AclInitializer.ts +++ b/src/init/AclInitializer.ts @@ -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 { 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 { - 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; } } diff --git a/src/init/RootContainerInitializer.ts b/src/init/RootContainerInitializer.ts index 32e798a18..c24bd048f 100644 --- a/src/init/RootContainerInitializer.ts +++ b/src/init/RootContainerInitializer.ts @@ -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 { - 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 { - 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; } /** diff --git a/src/pods/GeneratedPodManager.ts b/src/pods/GeneratedPodManager.ts index 72e188762..616a8a805 100644 --- a/src/pods/GeneratedPodManager.ts +++ b/src/pods/GeneratedPodManager.ts @@ -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 { 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); diff --git a/src/storage/StoreUtil.ts b/src/storage/StoreUtil.ts new file mode 100644 index 000000000..93440c24f --- /dev/null +++ b/src/storage/StoreUtil.ts @@ -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 { + try { + const result = await store.getRepresentation(identifier, {}); + result.data.destroy(); + return true; + } catch (error: unknown) { + if (error instanceof NotFoundHttpError) { + return false; + } + throw error; + } +} diff --git a/src/storage/routing/ConvertingRouterRule.ts b/src/storage/routing/ConvertingRouterRule.ts index 6aff25975..1ee17c7c2 100644 --- a/src/storage/routing/ConvertingRouterRule.ts +++ b/src/storage/routing/ConvertingRouterRule.ts @@ -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 => - 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 { - 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.