mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add support for quota limits
* feat: implemented SizeReporter and FileSizeReporter * test: FileSizeReporter tests * feat: added QuotedDataAccessor * test: added extra test to check recursiveness of filesizereporter * feat: added QuotaStrategy interface * feat: further progress in different files * feat: wrote doc, tests and improved code * feat: fixed bugs and code is now runnable and buildable * feat: finished implementation * fix: revert accidental chanegs * fix: fileSizeReported did not count container size * fix: bug calculating container sizes fixed * test: FileSizeReporter tests * test: QuotaDataValidator tests * test: QuotaError tests * fix: removed console.log * doc: added doc to several files * doc: changed doc for QuotaStrategy to new implementation * fix: improved content length regex * feat: improved GlobalQuotaStrategy code * fix: made FileSizeReported readonly * feat: added comments to quota-file.json * fix: changed default tempFilePath variable * test: included new tempFilePath variable in testing * chore: created seperate command for start:file:quota to pass tests * feat: removed all sync fs calls from FileSizeReporter * feat: minor changes in multple files * fix: changed function signatures to be in line with others * feat: optimized quota data validation * feat: improved FileSizeReporter code * fix: corrected calculation of containersizes and fixed erroring edgecase * feat: save content-length as number in metadata * feat: added comments and changed GlobalQuotaStrategy constructor * feat: changed file names and added small comment * test: AtomicFileDataAccessor tests * test: completed FileSizeReporter tests * fix: content-length is now saved correctly in RepresentationMetadata * feat: adapted content length metadata + tests * fix: removed tempFilePath variable * fix: reverted .gitignore * fix: forgot to remove tempFilePath variable from componentsjs config * test: GlobalQuotaStrategy tests * feat: replaced DataValidator with Validator * feat: reworked DataValidator * feat: added calcultateChunkSize() to SizeReporter * test: updated FileSizeReporter tests * fix: tempFile location now relative to rootFilePath * test: QuotaDataValidator tests * fix: corrected FileSizeReporter tests * fix: adapted FileSizeReporter tests * fix: FileSizeReporter bug on Windows * fix: regex linting error * feat: changed Validator class * feat: added PodQuotaStrategy to enable suota on a per pod basis * chore: bump context versions * fix: Capitalized comments in json file * chore: renamed ValidatorArgs to ValidatorInput * chore: order all exports * fix: made TODO comment clearer * chore: added seperated config files for global and pod based quota + fixed comments * chore: made minor changes to comments * feat: added PassthroughDataAccessor * feat: added PasstroughtDataAccessor + tests * fix: added invalid header check to ContentLengthParser * chore: improved mocks * chore: move quota limit higher up in config * fix: atomicity issue in AtomicFileDataAccessor * chore: moved .internal folder to config from FileSizeReporter * fix: improved algorithm to ignore folders while calculating file size in FileSizeReporter * fix: changes to support containers in the future * fix: added error handling to prevent reading of unexistent files * feat: added generic type to SizeReporter to calculate chunk sizes * test: use mocked DataAccessor * chore: added some comments to test and made minor improvement * fix: fs mock rename * chore: QuotaStrategy.estimateSize refactor * chore: move trackAvailableSpace to abstract class QuotaStrategy * fix: improved test case * test: quota integration tests * chore: edited some comments * chore: change lstat to stat * feat: moved estimateSize to SizeReporter to be consistent with calcultateChunkSize * test: finish up tests to reach coverage * fix: basic config * fix: minor changes to test CI run * fix: small fix for windows * fix: improved writing to file * chore: linting errors * chore: rename trackAvailableSpace * test: improved integration tests * test: logging info for test debugging * test: extra logging for debugging * test: logging for debugging * test: logging for debugging * test: logging for debugging * test: improved Quota integration test setup * test: improve quota tests for CI run * test: debugging Quota test * test: uncommented global quota test * test: changed global quota parameters * test: logging for debugging * test: logging cleanup * chore: minor changes, mostly typo fixes * chore: remove console.log * fix: getting inconsistent results * chore: try fix index.ts CI error * chore: try fix CI error * chore: try fix CI error * chore: revert last commits * chore: fix inconsistent files with origin * test: minor test improvements * chore: minor refactors and improvements * fix: added extra try catch for breaking bug * chore: improve config * chore: minor code improvements * test: use mockFs * feat: add extra check in podQuotaStrategy * chore: replace handle by handleSafe in ValidatingDataAccessor * chore: typo * test: improved Quota integration tests * test: made comment in test more correct * fix: rm -> rmdir for backwards compatibility * fix: fsPromises issue * chore: leave out irrelevant config * chore: removed start script from package.json * fix: Small fixes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
This commit is contained in:
@@ -58,7 +58,10 @@ export class ComposedAuxiliaryStrategy implements AuxiliaryStrategy {
|
||||
|
||||
public async validate(representation: Representation): Promise<void> {
|
||||
if (this.validator) {
|
||||
return this.validator.handleSafe(representation);
|
||||
await this.validator.handleSafe({
|
||||
representation,
|
||||
identifier: { path: representation.metadata.identifier.value },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { RepresentationConverter } from '../../storage/conversion/Represent
|
||||
import { INTERNAL_QUADS } from '../../util/ContentTypes';
|
||||
import { cloneRepresentation } from '../../util/ResourceUtil';
|
||||
import type { Representation } from '../representation/Representation';
|
||||
import type { ValidatorInput } from './Validator';
|
||||
import { Validator } from './Validator';
|
||||
|
||||
/**
|
||||
@@ -17,12 +18,11 @@ export class RdfValidator extends Validator {
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public async handle(representation: Representation): Promise<void> {
|
||||
public async handle({ representation, identifier }: ValidatorInput): Promise<Representation> {
|
||||
// If the data already is quads format we know it's RDF
|
||||
if (representation.metadata.contentType === INTERNAL_QUADS) {
|
||||
return;
|
||||
return representation;
|
||||
}
|
||||
const identifier = { path: representation.metadata.identifier.value };
|
||||
const preferences = { type: { [INTERNAL_QUADS]: 1 }};
|
||||
let result;
|
||||
try {
|
||||
@@ -39,5 +39,7 @@ export class RdfValidator extends Validator {
|
||||
}
|
||||
// Drain stream to make sure data was parsed correctly
|
||||
await arrayifyStream(result.data);
|
||||
|
||||
return representation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { AsyncHandler } from '../../util/handlers/AsyncHandler';
|
||||
import type { Representation } from '../representation/Representation';
|
||||
import type { ResourceIdentifier } from '../representation/ResourceIdentifier';
|
||||
|
||||
export type ValidatorInput = {
|
||||
representation: Representation;
|
||||
identifier: ResourceIdentifier;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic interface for classes that validate Representations in some way.
|
||||
*/
|
||||
export abstract class Validator extends AsyncHandler<Representation> { }
|
||||
export abstract class Validator extends AsyncHandler<ValidatorInput, Representation> { }
|
||||
|
||||
23
src/http/input/metadata/ContentLengthParser.ts
Normal file
23
src/http/input/metadata/ContentLengthParser.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { getLoggerFor } from '../../../logging/LogUtil';
|
||||
import type { HttpRequest } from '../../../server/HttpRequest';
|
||||
import type { RepresentationMetadata } from '../../representation/RepresentationMetadata';
|
||||
import { MetadataParser } from './MetadataParser';
|
||||
|
||||
/**
|
||||
* Parser for the `content-length` header.
|
||||
*/
|
||||
export class ContentLengthParser extends MetadataParser {
|
||||
protected readonly logger = getLoggerFor(this);
|
||||
|
||||
public async handle(input: { request: HttpRequest; metadata: RepresentationMetadata }): Promise<void> {
|
||||
const contentLength = input.request.headers['content-length'];
|
||||
if (contentLength) {
|
||||
const length = /^\s*(\d+)\s*(?:;.*)?$/u.exec(contentLength)?.[1];
|
||||
if (length) {
|
||||
input.metadata.contentLength = Number(length);
|
||||
} else {
|
||||
this.logger.warn(`Invalid content-length header found: ${contentLength}.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@ import { DataFactory, Store } from 'n3';
|
||||
import type { BlankNode, DefaultGraph, Literal, NamedNode, Quad, Term } from 'rdf-js';
|
||||
import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { InternalServerError } from '../../util/errors/InternalServerError';
|
||||
import { toNamedTerm, toObjectTerm, toCachedNamedNode, isTerm } from '../../util/TermUtil';
|
||||
import { CONTENT_TYPE, CONTENT_TYPE_TERM } from '../../util/Vocabularies';
|
||||
import { toNamedTerm, toObjectTerm, toCachedNamedNode, isTerm, toLiteral } from '../../util/TermUtil';
|
||||
import { CONTENT_TYPE, CONTENT_TYPE_TERM, CONTENT_LENGTH_TERM, XSD } from '../../util/Vocabularies';
|
||||
import type { ResourceIdentifier } from './ResourceIdentifier';
|
||||
import { isResourceIdentifier } from './ResourceIdentifier';
|
||||
|
||||
@@ -316,4 +316,18 @@ export class RepresentationMetadata {
|
||||
public set contentType(input) {
|
||||
this.set(CONTENT_TYPE_TERM, input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand for the CONTENT_LENGTH predicate.
|
||||
*/
|
||||
public get contentLength(): number | undefined {
|
||||
const length = this.get(CONTENT_LENGTH_TERM);
|
||||
return length?.value ? Number(length.value) : undefined;
|
||||
}
|
||||
|
||||
public set contentLength(input) {
|
||||
if (input) {
|
||||
this.set(CONTENT_LENGTH_TERM, toLiteral(input, XSD.terms.integer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/index.ts
20
src/index.ts
@@ -21,10 +21,10 @@ export * from './authorization/permissions/MethodModesExtractor';
|
||||
export * from './authorization/permissions/SparqlPatchModesExtractor';
|
||||
|
||||
// Authorization
|
||||
export * from './authorization/OwnerPermissionReader';
|
||||
export * from './authorization/AllStaticReader';
|
||||
export * from './authorization/Authorizer';
|
||||
export * from './authorization/AuxiliaryReader';
|
||||
export * from './authorization/OwnerPermissionReader';
|
||||
export * from './authorization/PathBasedReader';
|
||||
export * from './authorization/PermissionBasedAuthorizer';
|
||||
export * from './authorization/PermissionReader';
|
||||
@@ -57,6 +57,7 @@ export * from './http/input/identifier/OriginalUrlExtractor';
|
||||
export * from './http/input/identifier/TargetExtractor';
|
||||
|
||||
// HTTP/Input/Metadata
|
||||
export * from './http/input/metadata/ContentLengthParser';
|
||||
export * from './http/input/metadata/ContentTypeParser';
|
||||
export * from './http/input/metadata/LinkRelParser';
|
||||
export * from './http/input/metadata/MetadataParser';
|
||||
@@ -248,10 +249,14 @@ export * from './server/util/RedirectAllHttpHandler';
|
||||
export * from './server/util/RouterHandler';
|
||||
|
||||
// Storage/Accessors
|
||||
export * from './storage/accessors/AtomicDataAccessor';
|
||||
export * from './storage/accessors/AtomicFileDataAccessor';
|
||||
export * from './storage/accessors/DataAccessor';
|
||||
export * from './storage/accessors/FileDataAccessor';
|
||||
export * from './storage/accessors/InMemoryDataAccessor';
|
||||
export * from './storage/accessors/PassthroughDataAccessor';
|
||||
export * from './storage/accessors/SparqlDataAccessor';
|
||||
export * from './storage/accessors/ValidatingDataAccessor';
|
||||
|
||||
// Storage/Conversion
|
||||
export * from './storage/conversion/BaseTypedRepresentationConverter';
|
||||
@@ -295,6 +300,11 @@ export * from './storage/patch/RepresentationPatcher';
|
||||
export * from './storage/patch/RepresentationPatchHandler';
|
||||
export * from './storage/patch/SparqlUpdatePatcher';
|
||||
|
||||
// Storage/Quota
|
||||
export * from './storage/quota/GlobalQuotaStrategy';
|
||||
export * from './storage/quota/PodQuotaStrategy';
|
||||
export * from './storage/quota/QuotaStrategy';
|
||||
|
||||
// Storage/Routing
|
||||
export * from './storage/routing/BaseUrlRouterRule';
|
||||
export * from './storage/routing/ConvertingRouterRule';
|
||||
@@ -302,6 +312,14 @@ export * from './storage/routing/PreferenceSupport';
|
||||
export * from './storage/routing/RegexRouterRule';
|
||||
export * from './storage/routing/RouterRule';
|
||||
|
||||
// Storage/Size-Reporter
|
||||
export * from './storage/size-reporter/FileSizeReporter';
|
||||
export * from './storage/size-reporter/Size';
|
||||
export * from './storage/size-reporter/SizeReporter';
|
||||
|
||||
// Storage/Validators
|
||||
export * from './storage/validators/QuotaValidator';
|
||||
|
||||
// Storage
|
||||
export * from './storage/AtomicResourceStore';
|
||||
export * from './storage/BaseResourceStore';
|
||||
|
||||
10
src/storage/accessors/AtomicDataAccessor.ts
Normal file
10
src/storage/accessors/AtomicDataAccessor.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { DataAccessor } from './DataAccessor';
|
||||
|
||||
/**
|
||||
* The AtomicDataAccessor interface has identical function signatures as
|
||||
* the DataAccessor, with the additional constraint that every function call
|
||||
* must be atomic in its effect: either the call fully succeeds, reaching the
|
||||
* desired new state; or it fails, upon which the resulting state remains
|
||||
* identical to the one before the call.
|
||||
*/
|
||||
export interface AtomicDataAccessor extends DataAccessor { }
|
||||
62
src/storage/accessors/AtomicFileDataAccessor.ts
Normal file
62
src/storage/accessors/AtomicFileDataAccessor.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { mkdirSync, promises as fsPromises } from 'fs';
|
||||
import type { Readable } from 'stream';
|
||||
import { v4 } from 'uuid';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import { joinFilePath } from '../../util/PathUtil';
|
||||
import type { FileIdentifierMapper } from '../mapping/FileIdentifierMapper';
|
||||
import type { AtomicDataAccessor } from './AtomicDataAccessor';
|
||||
import { FileDataAccessor } from './FileDataAccessor';
|
||||
|
||||
/**
|
||||
* AtomicDataAccessor that uses the file system to store documents as files and containers as folders.
|
||||
* Data will first be written to a temporary location and only if no errors occur
|
||||
* will the data be written to the desired location.
|
||||
*/
|
||||
export class AtomicFileDataAccessor extends FileDataAccessor implements AtomicDataAccessor {
|
||||
private readonly tempFilePath: string;
|
||||
|
||||
public constructor(resourceMapper: FileIdentifierMapper, rootFilePath: string, tempFilePath: string) {
|
||||
super(resourceMapper);
|
||||
this.tempFilePath = joinFilePath(rootFilePath, tempFilePath);
|
||||
// Cannot use fsPromises in constructor
|
||||
mkdirSync(this.tempFilePath, { recursive: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given data as a file (and potential metadata as additional file).
|
||||
* Data will first be written to a temporary file and if no errors occur only then the
|
||||
* file will be moved to desired destination.
|
||||
* If the stream errors it is made sure the temporary file will be deleted.
|
||||
* The metadata file will only be written if the data was written successfully.
|
||||
*/
|
||||
public async writeDocument(identifier: ResourceIdentifier, data: Guarded<Readable>, metadata: RepresentationMetadata):
|
||||
Promise<void> {
|
||||
const link = await this.resourceMapper.mapUrlToFilePath(identifier, false, metadata.contentType);
|
||||
|
||||
// Generate temporary file name
|
||||
const tempFilePath = joinFilePath(this.tempFilePath, `temp-${v4()}.txt`);
|
||||
|
||||
try {
|
||||
await this.writeDataFile(tempFilePath, data);
|
||||
|
||||
// Check if we already have a corresponding file with a different extension
|
||||
await this.verifyExistingExtension(link);
|
||||
|
||||
// When no quota errors occur move the file to its desired location
|
||||
await fsPromises.rename(tempFilePath, link.filePath);
|
||||
} catch (error: unknown) {
|
||||
// Delete the data already written
|
||||
try {
|
||||
if ((await this.getStats(tempFilePath)).isFile()) {
|
||||
await fsPromises.unlink(tempFilePath);
|
||||
}
|
||||
} catch {
|
||||
throw error;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
await this.writeMetadata(link, metadata);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import type { DataAccessor } from './DataAccessor';
|
||||
* DataAccessor that uses the file system to store documents as files and containers as folders.
|
||||
*/
|
||||
export class FileDataAccessor implements DataAccessor {
|
||||
private readonly resourceMapper: FileIdentifierMapper;
|
||||
protected readonly resourceMapper: FileIdentifierMapper;
|
||||
|
||||
public constructor(resourceMapper: FileIdentifierMapper) {
|
||||
this.resourceMapper = resourceMapper;
|
||||
@@ -149,7 +149,7 @@ export class FileDataAccessor implements DataAccessor {
|
||||
* @throws NotFoundHttpError
|
||||
* If the file/folder doesn't exist.
|
||||
*/
|
||||
private async getStats(path: string): Promise<Stats> {
|
||||
protected async getStats(path: string): Promise<Stats> {
|
||||
try {
|
||||
return await fsPromises.stat(path);
|
||||
} catch (error: unknown) {
|
||||
@@ -192,7 +192,7 @@ export class FileDataAccessor implements DataAccessor {
|
||||
*
|
||||
* @returns True if data was written to a file.
|
||||
*/
|
||||
private async writeMetadata(link: ResourceLink, metadata: RepresentationMetadata): Promise<boolean> {
|
||||
protected async writeMetadata(link: ResourceLink, metadata: RepresentationMetadata): Promise<boolean> {
|
||||
// These are stored by file system conventions
|
||||
metadata.remove(RDF.terms.type, LDP.terms.Resource);
|
||||
metadata.remove(RDF.terms.type, LDP.terms.Container);
|
||||
@@ -327,7 +327,7 @@ export class FileDataAccessor implements DataAccessor {
|
||||
*
|
||||
* @param link - ResourceLink corresponding to the new resource data.
|
||||
*/
|
||||
private async verifyExistingExtension(link: ResourceLink): Promise<void> {
|
||||
protected async verifyExistingExtension(link: ResourceLink): Promise<void> {
|
||||
try {
|
||||
// Delete the old file with the (now) wrong extension
|
||||
const oldLink = await this.resourceMapper.mapUrlToFilePath(link.identifier, false);
|
||||
@@ -347,11 +347,14 @@ export class FileDataAccessor implements DataAccessor {
|
||||
* @param path - The filepath of the file to be created.
|
||||
* @param data - The data to be put in the file.
|
||||
*/
|
||||
private async writeDataFile(path: string, data: Readable): Promise<void> {
|
||||
protected async writeDataFile(path: string, data: Readable): Promise<void> {
|
||||
return new Promise((resolve, reject): any => {
|
||||
const writeStream = createWriteStream(path);
|
||||
data.pipe(writeStream);
|
||||
data.on('error', reject);
|
||||
data.on('error', (error): void => {
|
||||
reject(error);
|
||||
writeStream.end();
|
||||
});
|
||||
|
||||
writeStream.on('error', reject);
|
||||
writeStream.on('finish', resolve);
|
||||
|
||||
49
src/storage/accessors/PassthroughDataAccessor.ts
Normal file
49
src/storage/accessors/PassthroughDataAccessor.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import type { Readable } from 'stream';
|
||||
import type { Representation } from '../../http/representation/Representation';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import type { AtomicDataAccessor } from './AtomicDataAccessor';
|
||||
import type { DataAccessor } from './DataAccessor';
|
||||
|
||||
/**
|
||||
* DataAccessor that calls the corresponding functions of the source DataAccessor.
|
||||
* Can be extended by data accessors that do not want to override all functions
|
||||
* by implementing a decorator pattern.
|
||||
*/
|
||||
export class PassthroughDataAccessor implements DataAccessor {
|
||||
protected readonly accessor: AtomicDataAccessor;
|
||||
|
||||
public constructor(accessor: DataAccessor) {
|
||||
this.accessor = accessor;
|
||||
}
|
||||
|
||||
public async writeDocument(identifier: ResourceIdentifier, data: Guarded<Readable>, metadata: RepresentationMetadata):
|
||||
Promise<void> {
|
||||
return this.accessor.writeDocument(identifier, data, metadata);
|
||||
}
|
||||
|
||||
public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise<void> {
|
||||
return this.accessor.writeContainer(identifier, metadata);
|
||||
}
|
||||
|
||||
public async canHandle(representation: Representation): Promise<void> {
|
||||
return this.accessor.canHandle(representation);
|
||||
}
|
||||
|
||||
public async getData(identifier: ResourceIdentifier): Promise<Guarded<Readable>> {
|
||||
return this.accessor.getData(identifier);
|
||||
}
|
||||
|
||||
public async getMetadata(identifier: ResourceIdentifier): Promise<RepresentationMetadata> {
|
||||
return this.accessor.getMetadata(identifier);
|
||||
}
|
||||
|
||||
public getChildren(identifier: ResourceIdentifier): AsyncIterableIterator<RepresentationMetadata> {
|
||||
return this.accessor.getChildren(identifier);
|
||||
}
|
||||
|
||||
public async deleteResource(identifier: ResourceIdentifier): Promise<void> {
|
||||
return this.accessor.deleteResource(identifier);
|
||||
}
|
||||
}
|
||||
40
src/storage/accessors/ValidatingDataAccessor.ts
Normal file
40
src/storage/accessors/ValidatingDataAccessor.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { Readable } from 'stream';
|
||||
import type { Validator } from '../../http/auxiliary/Validator';
|
||||
import { BasicRepresentation } from '../../http/representation/BasicRepresentation';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import type { DataAccessor } from './DataAccessor';
|
||||
import { PassthroughDataAccessor } from './PassthroughDataAccessor';
|
||||
|
||||
/**
|
||||
* A ValidatingDataAccessor wraps a DataAccessor such that the data stream is validated while being written.
|
||||
* An AtomicDataAccessor can be used to prevent data being written in case validation fails.
|
||||
*/
|
||||
export class ValidatingDataAccessor extends PassthroughDataAccessor {
|
||||
private readonly validator: Validator;
|
||||
|
||||
public constructor(accessor: DataAccessor, validator: Validator) {
|
||||
super(accessor);
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
public async writeDocument(
|
||||
identifier: ResourceIdentifier,
|
||||
data: Guarded<Readable>,
|
||||
metadata: RepresentationMetadata,
|
||||
): Promise<void> {
|
||||
const pipedRep = await this.validator.handleSafe({
|
||||
representation: new BasicRepresentation(data, metadata),
|
||||
identifier,
|
||||
});
|
||||
return this.accessor.writeDocument(identifier, pipedRep.data, metadata);
|
||||
}
|
||||
|
||||
public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise<void> {
|
||||
// A container's data mainly resides in its metadata,
|
||||
// of which we can't calculate the disk size of at this point in the code.
|
||||
// Extra info can be found here: https://github.com/solid/community-server/pull/973#discussion_r723376888
|
||||
return this.accessor.writeContainer(identifier, metadata);
|
||||
}
|
||||
}
|
||||
19
src/storage/quota/GlobalQuotaStrategy.ts
Normal file
19
src/storage/quota/GlobalQuotaStrategy.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { Size } from '../size-reporter/Size';
|
||||
import type { SizeReporter } from '../size-reporter/SizeReporter';
|
||||
import { QuotaStrategy } from './QuotaStrategy';
|
||||
|
||||
/**
|
||||
* The GlobalQuotaStrategy sets a limit on the amount of data stored on the server globally.
|
||||
*/
|
||||
export class GlobalQuotaStrategy extends QuotaStrategy {
|
||||
private readonly base: string;
|
||||
|
||||
public constructor(limit: Size, reporter: SizeReporter<any>, base: string) {
|
||||
super(reporter, limit);
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
protected async getTotalSpaceUsed(): Promise<Size> {
|
||||
return this.reporter.getSize({ path: this.base });
|
||||
}
|
||||
}
|
||||
66
src/storage/quota/PodQuotaStrategy.ts
Normal file
66
src/storage/quota/PodQuotaStrategy.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError';
|
||||
import type { IdentifierStrategy } from '../../util/identifiers/IdentifierStrategy';
|
||||
import { RDF, PIM } from '../../util/Vocabularies';
|
||||
import type { DataAccessor } from '../accessors/DataAccessor';
|
||||
import type { Size } from '../size-reporter/Size';
|
||||
import type { SizeReporter } from '../size-reporter/SizeReporter';
|
||||
import { QuotaStrategy } from './QuotaStrategy';
|
||||
|
||||
/**
|
||||
* The PodQuotaStrategy sets a limit on the amount of data stored on a per pod basis
|
||||
*/
|
||||
export class PodQuotaStrategy extends QuotaStrategy {
|
||||
private readonly identifierStrategy: IdentifierStrategy;
|
||||
private readonly accessor: DataAccessor;
|
||||
|
||||
public constructor(
|
||||
limit: Size,
|
||||
reporter: SizeReporter<any>,
|
||||
identifierStrategy: IdentifierStrategy,
|
||||
accessor: DataAccessor,
|
||||
) {
|
||||
super(reporter, limit);
|
||||
this.identifierStrategy = identifierStrategy;
|
||||
this.accessor = accessor;
|
||||
}
|
||||
|
||||
protected async getTotalSpaceUsed(identifier: ResourceIdentifier): Promise<Size> {
|
||||
const pimStorage = await this.searchPimStorage(identifier);
|
||||
|
||||
// No storage was found containing this identifier, so we assume this identifier points to an internal location.
|
||||
// Quota does not apply here so there is always available space.
|
||||
if (!pimStorage) {
|
||||
return { amount: Number.MAX_SAFE_INTEGER, unit: this.limit.unit };
|
||||
}
|
||||
|
||||
return this.reporter.getSize(pimStorage);
|
||||
}
|
||||
|
||||
/** Finds the closest parent container that has pim:storage as metadata */
|
||||
private async searchPimStorage(identifier: ResourceIdentifier): Promise<ResourceIdentifier | undefined> {
|
||||
if (this.identifierStrategy.isRootContainer(identifier)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let metadata: RepresentationMetadata;
|
||||
const parent = this.identifierStrategy.getParentContainer(identifier);
|
||||
|
||||
try {
|
||||
metadata = await this.accessor.getMetadata(identifier);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof NotFoundHttpError) {
|
||||
// Resource and/or its metadata do not exist
|
||||
return this.searchPimStorage(parent);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
const hasPimStorageMetadata = metadata!.getAll(RDF.type)
|
||||
.some((term): boolean => term.value === PIM.Storage);
|
||||
|
||||
return hasPimStorageMetadata ? identifier : this.searchPimStorage(parent);
|
||||
}
|
||||
}
|
||||
|
||||
105
src/storage/quota/QuotaStrategy.ts
Normal file
105
src/storage/quota/QuotaStrategy.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
// These two eslint lines are needed to store 'this' in a variable so it can be used
|
||||
// in the PassThrough of createQuotaGuard
|
||||
/* eslint-disable @typescript-eslint/no-this-alias */
|
||||
/* eslint-disable consistent-this */
|
||||
import { PassThrough } from 'stream';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import { PayloadHttpError } from '../../util/errors/PayloadHttpError';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import { guardStream } from '../../util/GuardedStream';
|
||||
import type { Size } from '../size-reporter/Size';
|
||||
import type { SizeReporter } from '../size-reporter/SizeReporter';
|
||||
|
||||
/**
|
||||
* A QuotaStrategy is used when we want to set a limit to the amount of data that can be
|
||||
* stored on the server.
|
||||
* This can range from a limit for the whole server to a limit on a per pod basis.
|
||||
* The way the size of a resource is calculated is implemented by the implementing classes.
|
||||
* This can be bytes, quads, file count, ...
|
||||
*/
|
||||
export abstract class QuotaStrategy {
|
||||
public readonly reporter: SizeReporter<any>;
|
||||
public readonly limit: Size;
|
||||
|
||||
public constructor(reporter: SizeReporter<any>, limit: Size) {
|
||||
this.reporter = reporter;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available space when writing data to the given identifier.
|
||||
* If the given resource already exists it will deduct the already taken up
|
||||
* space by that resource since it is going to be overwritten and thus counts
|
||||
* as available space.
|
||||
*
|
||||
* @param identifier - the identifier of the resource of which you want the available space
|
||||
* @returns the available space and the unit of the space as a Size object
|
||||
*/
|
||||
public async getAvailableSpace(identifier: ResourceIdentifier): Promise<Size> {
|
||||
const totalUsed = await this.getTotalSpaceUsed(identifier);
|
||||
|
||||
// Ignore identifiers where quota does not apply
|
||||
if (totalUsed.amount === Number.MAX_SAFE_INTEGER) {
|
||||
return totalUsed;
|
||||
}
|
||||
|
||||
// When a file is overwritten the space the file takes up right now should also
|
||||
// be counted as available space as it will disappear/be overwritten
|
||||
totalUsed.amount -= (await this.reporter.getSize(identifier)).amount;
|
||||
|
||||
return {
|
||||
amount: this.limit.amount - totalUsed.amount,
|
||||
unit: this.limit.unit,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the currently used/occupied space.
|
||||
*
|
||||
* @param identifier - the identifier that should be used to calculate the total
|
||||
* @returns a Size object containing the requested value.
|
||||
* If quota is not relevant for this identifier, Size.amount should be Number.MAX_SAFE_INTEGER
|
||||
*/
|
||||
protected abstract getTotalSpaceUsed(identifier: ResourceIdentifier): Promise<Size>;
|
||||
|
||||
/**
|
||||
* Get an estimated size of the resource
|
||||
*
|
||||
* @param metadata - the metadata that might include the size
|
||||
* @returns a Size object containing the estimated size and unit of the resource
|
||||
*/
|
||||
public async estimateSize(metadata: RepresentationMetadata): Promise<Size | undefined> {
|
||||
const estimate = await this.reporter.estimateSize(metadata);
|
||||
return estimate ? { unit: this.limit.unit, amount: estimate } : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Passthrough stream that will keep track of the available space.
|
||||
* If the quota is exceeded the stream will emit an error and destroy itself.
|
||||
* Like other Passthrough instances this will simply pass on the chunks, when the quota isn't exceeded.
|
||||
*
|
||||
* @param identifier - the identifier of the resource in question
|
||||
* @returns a Passthrough instance that errors when quota is exceeded
|
||||
*/
|
||||
public async createQuotaGuard(identifier: ResourceIdentifier): Promise<Guarded<PassThrough>> {
|
||||
let total = 0;
|
||||
const strategy = this;
|
||||
const { reporter } = this;
|
||||
|
||||
return guardStream(new PassThrough({
|
||||
async transform(this, chunk: any, enc: string, done: () => void): Promise<void> {
|
||||
total += await reporter.calculateChunkSize(chunk);
|
||||
const availableSpace = await strategy.getAvailableSpace(identifier);
|
||||
if (availableSpace.amount < total) {
|
||||
this.destroy(new PayloadHttpError(
|
||||
`Quota exceeded by ${total - availableSpace.amount} ${availableSpace.unit} during write`,
|
||||
));
|
||||
}
|
||||
|
||||
this.push(chunk);
|
||||
done();
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
87
src/storage/size-reporter/FileSizeReporter.ts
Normal file
87
src/storage/size-reporter/FileSizeReporter.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { Stats } from 'fs';
|
||||
import { promises as fsPromises } from 'fs';
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import { joinFilePath, normalizeFilePath, trimTrailingSlashes } from '../../util/PathUtil';
|
||||
import type { FileIdentifierMapper } from '../mapping/FileIdentifierMapper';
|
||||
import type { Size } from './Size';
|
||||
import { UNIT_BYTES } from './Size';
|
||||
import type { SizeReporter } from './SizeReporter';
|
||||
|
||||
/**
|
||||
* SizeReporter that is used to calculate sizes of resources for a file based system.
|
||||
*/
|
||||
export class FileSizeReporter implements SizeReporter<string> {
|
||||
private readonly fileIdentifierMapper: FileIdentifierMapper;
|
||||
private readonly ignoreFolders: RegExp[];
|
||||
private readonly rootFilePath: string;
|
||||
|
||||
public constructor(fileIdentifierMapper: FileIdentifierMapper, rootFilePath: string, ignoreFolders?: string[]) {
|
||||
this.fileIdentifierMapper = fileIdentifierMapper;
|
||||
this.ignoreFolders = ignoreFolders ? ignoreFolders.map((folder: string): RegExp => new RegExp(folder, 'u')) : [];
|
||||
this.rootFilePath = normalizeFilePath(rootFilePath);
|
||||
}
|
||||
|
||||
/** The FileSizeReporter will always return data in the form of bytes */
|
||||
public getUnit(): string {
|
||||
return UNIT_BYTES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the given resource ( and its children ) in bytes
|
||||
*/
|
||||
public async getSize(identifier: ResourceIdentifier): Promise<Size> {
|
||||
const fileLocation = (await this.fileIdentifierMapper.mapUrlToFilePath(identifier, false)).filePath;
|
||||
|
||||
return { unit: this.getUnit(), amount: await this.getTotalSize(fileLocation) };
|
||||
}
|
||||
|
||||
public async calculateChunkSize(chunk: string): Promise<number> {
|
||||
return chunk.length;
|
||||
}
|
||||
|
||||
/** The estimated size of a resource in this reporter is simply the content-length header */
|
||||
public async estimateSize(metadata: RepresentationMetadata): Promise<number | undefined> {
|
||||
return metadata.contentLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total size of a resource and its children if present
|
||||
*
|
||||
* @param fileLocation - the resource of which you want the total size of ( on disk )
|
||||
* @returns a number specifying how many bytes are used by the resource
|
||||
*/
|
||||
private async getTotalSize(fileLocation: string): Promise<number> {
|
||||
let stat: Stats;
|
||||
|
||||
// Check if the file exists
|
||||
try {
|
||||
stat = await fsPromises.stat(fileLocation);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If the file's location points to a file, simply return the file's size
|
||||
if (stat.isFile()) {
|
||||
return stat.size;
|
||||
}
|
||||
|
||||
// If the location DOES exist and is NOT a file it should be a directory
|
||||
// recursively add all sizes of children to the total
|
||||
const childFiles = await fsPromises.readdir(fileLocation);
|
||||
const rootFilePathLength = trimTrailingSlashes(this.rootFilePath).length;
|
||||
|
||||
return await childFiles.reduce(async(acc: Promise<number>, current): Promise<number> => {
|
||||
const childFileLocation = normalizeFilePath(joinFilePath(fileLocation, current));
|
||||
let result = await acc;
|
||||
|
||||
// Exclude internal files
|
||||
if (!this.ignoreFolders.some((folder: RegExp): boolean =>
|
||||
folder.test(childFileLocation.slice(rootFilePathLength)))) {
|
||||
result += await this.getTotalSize(childFileLocation);
|
||||
}
|
||||
|
||||
return result;
|
||||
}, Promise.resolve(stat.size));
|
||||
}
|
||||
}
|
||||
9
src/storage/size-reporter/Size.ts
Normal file
9
src/storage/size-reporter/Size.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Describes the size of something by stating how much of a certain unit is present.
|
||||
*/
|
||||
export interface Size {
|
||||
unit: string;
|
||||
amount: number;
|
||||
}
|
||||
|
||||
export const UNIT_BYTES = 'bytes';
|
||||
44
src/storage/size-reporter/SizeReporter.ts
Normal file
44
src/storage/size-reporter/SizeReporter.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { RepresentationMetadata } from '../../http/representation/RepresentationMetadata';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import type { Size } from './Size';
|
||||
|
||||
/**
|
||||
* A SizeReporter's only purpose (at the moment) is to calculate the size
|
||||
* of a resource. How the size is calculated or what unit it is in is defined by
|
||||
* the class implementing this interface.
|
||||
* One might use the amount of bytes and another might use the amount of triples
|
||||
* stored in a resource.
|
||||
*/
|
||||
export interface SizeReporter<T> {
|
||||
|
||||
/**
|
||||
* Get the unit as a string in which a SizeReporter returns data
|
||||
*/
|
||||
getUnit: () => string;
|
||||
|
||||
/**
|
||||
* Get the size of a given resource
|
||||
*
|
||||
* @param identifier - the resource of which you want the size
|
||||
* @returns The size of the resource as a Size object calculated recursively
|
||||
* if the identifier leads to a container
|
||||
*/
|
||||
getSize: (identifier: ResourceIdentifier) => Promise<Size>;
|
||||
|
||||
/**
|
||||
* Calculate the size of a chunk based on which SizeReporter is being used
|
||||
*
|
||||
* @param chunk - the chunk of which you want the size
|
||||
* @returns the size of the passed chunk as a number
|
||||
*/
|
||||
calculateChunkSize: (chunk: T) => Promise<number>;
|
||||
|
||||
/**
|
||||
* Estimate the size of a body / request by looking at its metadata
|
||||
*
|
||||
* @param metadata - the metadata of the resource you want an estimated size of
|
||||
* @returns the estimated size of the body / request or undefined if no
|
||||
* meaningful estimation can be made
|
||||
*/
|
||||
estimateSize: (metadata: RepresentationMetadata) => Promise<number | undefined>;
|
||||
}
|
||||
61
src/storage/validators/QuotaValidator.ts
Normal file
61
src/storage/validators/QuotaValidator.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Readable, PassThrough } from 'stream';
|
||||
import { Validator } from '../../http/auxiliary/Validator';
|
||||
import type { ValidatorInput } from '../../http/auxiliary/Validator';
|
||||
import type { Representation } from '../../http/representation/Representation';
|
||||
import { PayloadHttpError } from '../../util/errors/PayloadHttpError';
|
||||
import type { Guarded } from '../../util/GuardedStream';
|
||||
import { guardStream } from '../../util/GuardedStream';
|
||||
import { pipeSafely } from '../../util/StreamUtil';
|
||||
import type { QuotaStrategy } from '../quota/QuotaStrategy';
|
||||
|
||||
/**
|
||||
* The QuotaValidator validates data streams by making sure they would not exceed the limits of a QuotaStrategy.
|
||||
*/
|
||||
export class QuotaValidator extends Validator {
|
||||
private readonly strategy: QuotaStrategy;
|
||||
|
||||
public constructor(strategy: QuotaStrategy) {
|
||||
super();
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
public async handle({ representation, identifier }: ValidatorInput): Promise<Representation> {
|
||||
const { data, metadata } = representation;
|
||||
|
||||
// 1. Get the available size
|
||||
const availableSize = await this.strategy.getAvailableSpace(identifier);
|
||||
|
||||
// 2. Check if the estimated size is bigger then the available size
|
||||
const estimatedSize = await this.strategy.estimateSize(metadata);
|
||||
|
||||
if (estimatedSize && availableSize.amount < estimatedSize.amount) {
|
||||
return {
|
||||
...representation,
|
||||
data: guardStream(new Readable({
|
||||
read(this): void {
|
||||
this.destroy(new PayloadHttpError(
|
||||
`Quota exceeded: Advertised Content-Length is ${estimatedSize.amount} ${estimatedSize.unit} ` +
|
||||
`and only ${availableSize.amount} ${availableSize.unit} is available`,
|
||||
));
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
// 3. Track if quota is exceeded during writing
|
||||
const tracking: Guarded<PassThrough> = await this.strategy.createQuotaGuard(identifier);
|
||||
|
||||
// 4. Double check quota is not exceeded after write (concurrent writing possible)
|
||||
const afterWrite = new PassThrough({
|
||||
flush: async(done): Promise<void> => {
|
||||
const availableSpace = (await this.strategy.getAvailableSpace(identifier)).amount;
|
||||
done(availableSpace < 0 ? new PayloadHttpError('Quota exceeded after write completed') : undefined);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...representation,
|
||||
data: pipeSafely(pipeSafely(data, tracking), afterWrite),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,10 @@ export const FOAF = createUriAndTermNamespace('http://xmlns.com/foaf/0.1/',
|
||||
'Agent',
|
||||
);
|
||||
|
||||
export const HH = createUriAndTermNamespace('http://www.w3.org/2011/http-headers#',
|
||||
'content-length',
|
||||
);
|
||||
|
||||
export const HTTP = createUriAndTermNamespace('http://www.w3.org/2011/http#',
|
||||
'statusCodeNumber',
|
||||
);
|
||||
@@ -155,6 +159,7 @@ export const XSD = createUriAndTermNamespace('http://www.w3.org/2001/XMLSchema#'
|
||||
);
|
||||
|
||||
// Alias for commonly used types
|
||||
export const CONTENT_LENGTH_TERM = HH.terms['content-length'];
|
||||
export const CONTENT_TYPE = MA.format;
|
||||
export const CONTENT_TYPE_TERM = MA.terms.format;
|
||||
export const PREFERRED_PREFIX = VANN.preferredNamespacePrefix;
|
||||
|
||||
23
src/util/errors/PayloadHttpError.ts
Normal file
23
src/util/errors/PayloadHttpError.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { HttpErrorOptions } from './HttpError';
|
||||
import { HttpError } from './HttpError';
|
||||
|
||||
/**
|
||||
* An error thrown when data exceeded the pre configured quota
|
||||
*/
|
||||
export class PayloadHttpError extends HttpError {
|
||||
/**
|
||||
* Default message is 'Storage quota was exceeded.'.
|
||||
* @param message - Optional, more specific, message.
|
||||
* @param options - Optional error options.
|
||||
*/
|
||||
public constructor(message?: string, options?: HttpErrorOptions) {
|
||||
super(413,
|
||||
'PayloadHttpError',
|
||||
message ?? 'Storage quota was exceeded.',
|
||||
options);
|
||||
}
|
||||
|
||||
public static isInstance(error: any): error is PayloadHttpError {
|
||||
return HttpError.isInstance(error) && error.statusCode === 413;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user