mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
feat(server): separate storage quota limit and blob size limit (#10957)
close CLOUD-171
This commit is contained in:
@@ -143,10 +143,12 @@ test('should reject blob exceeded limit', async t => {
|
||||
const buffer1 = Buffer.from(
|
||||
Array.from({ length: RESTRICTED_QUOTA.blobLimit + 1 }, () => 0)
|
||||
);
|
||||
await t.throwsAsync(setBlob(app, workspace1.id, buffer1));
|
||||
await t.throwsAsync(setBlob(app, workspace1.id, buffer1), {
|
||||
message: 'You have exceeded your blob size quota.',
|
||||
});
|
||||
});
|
||||
|
||||
test('should reject blob exceeded quota', async t => {
|
||||
test('should reject blob exceeded storage quota', async t => {
|
||||
await app.signupV1('u1@affine.pro');
|
||||
|
||||
const workspace = await createWorkspace(app);
|
||||
@@ -155,7 +157,9 @@ test('should reject blob exceeded quota', async t => {
|
||||
const buffer = Buffer.from(Array.from({ length: OneMB }, () => 0));
|
||||
|
||||
await t.notThrowsAsync(setBlob(app, workspace.id, buffer));
|
||||
await t.throwsAsync(setBlob(app, workspace.id, buffer));
|
||||
await t.throwsAsync(setBlob(app, workspace.id, buffer), {
|
||||
message: 'You have exceeded your storage quota.',
|
||||
});
|
||||
});
|
||||
|
||||
test('should accept blob even storage out of quota if workspace has unlimited feature', async t => {
|
||||
|
||||
@@ -695,7 +695,11 @@ export const USER_FRIENDLY_ERRORS = {
|
||||
// Quota & Limit errors
|
||||
blob_quota_exceeded: {
|
||||
type: 'quota_exceeded',
|
||||
message: 'You have exceeded your blob storage quota.',
|
||||
message: 'You have exceeded your blob size quota.',
|
||||
},
|
||||
storage_quota_exceeded: {
|
||||
type: 'quota_exceeded',
|
||||
message: 'You have exceeded your storage quota.',
|
||||
},
|
||||
member_quota_exceeded: {
|
||||
type: 'quota_exceeded',
|
||||
|
||||
@@ -759,6 +759,12 @@ export class BlobQuotaExceeded extends UserFriendlyError {
|
||||
}
|
||||
}
|
||||
|
||||
export class StorageQuotaExceeded extends UserFriendlyError {
|
||||
constructor(message?: string) {
|
||||
super('quota_exceeded', 'storage_quota_exceeded', message);
|
||||
}
|
||||
}
|
||||
|
||||
export class MemberQuotaExceeded extends UserFriendlyError {
|
||||
constructor(message?: string) {
|
||||
super('quota_exceeded', 'member_quota_exceeded', message);
|
||||
@@ -995,6 +1001,7 @@ export enum ErrorNames {
|
||||
COPILOT_FAILED_TO_MATCH_CONTEXT,
|
||||
COPILOT_EMBEDDING_UNAVAILABLE,
|
||||
BLOB_QUOTA_EXCEEDED,
|
||||
STORAGE_QUOTA_EXCEEDED,
|
||||
MEMBER_QUOTA_EXCEEDED,
|
||||
COPILOT_QUOTA_EXCEEDED,
|
||||
RUNTIME_CONFIG_NOT_FOUND,
|
||||
|
||||
@@ -258,14 +258,14 @@ export class QuotaService {
|
||||
this.logger.warn(
|
||||
`storage size limit exceeded: ${currentSize} > ${storageQuota}`
|
||||
);
|
||||
return true;
|
||||
return { storageQuotaExceeded: true, blobQuotaExceeded: false };
|
||||
} else if (recvSize > blobLimit) {
|
||||
this.logger.warn(
|
||||
`blob size limit exceeded: ${recvSize} > ${blobLimit}`
|
||||
);
|
||||
return true;
|
||||
return { storageQuotaExceeded: false, blobQuotaExceeded: true };
|
||||
} else {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
};
|
||||
return checkExceeded;
|
||||
|
||||
@@ -13,7 +13,11 @@ import {
|
||||
import GraphQLUpload from 'graphql-upload/GraphQLUpload.mjs';
|
||||
|
||||
import type { FileUpload } from '../../../base';
|
||||
import { BlobQuotaExceeded, CloudThrottlerGuard } from '../../../base';
|
||||
import {
|
||||
BlobQuotaExceeded,
|
||||
CloudThrottlerGuard,
|
||||
StorageQuotaExceeded,
|
||||
} from '../../../base';
|
||||
import { CurrentUser } from '../../auth';
|
||||
import { AccessController } from '../../permission';
|
||||
import { QuotaService } from '../../quota';
|
||||
@@ -92,9 +96,11 @@ export class WorkspaceBlobResolver {
|
||||
const checkExceeded =
|
||||
await this.quota.getWorkspaceQuotaCalculator(workspaceId);
|
||||
|
||||
// TODO(@darksky): need a proper way to separate `BlobQuotaExceeded` and `BlobSizeTooLarge`
|
||||
if (checkExceeded(0)) {
|
||||
let result = checkExceeded(0);
|
||||
if (result?.blobQuotaExceeded) {
|
||||
throw new BlobQuotaExceeded();
|
||||
} else if (result?.storageQuotaExceeded) {
|
||||
throw new StorageQuotaExceeded();
|
||||
}
|
||||
const buffer = await new Promise<Buffer>((resolve, reject) => {
|
||||
const stream = blob.createReadStream();
|
||||
@@ -104,16 +110,22 @@ export class WorkspaceBlobResolver {
|
||||
|
||||
// check size after receive each chunk to avoid unnecessary memory usage
|
||||
const bufferSize = chunks.reduce((acc, cur) => acc + cur.length, 0);
|
||||
if (checkExceeded(bufferSize)) {
|
||||
result = checkExceeded(bufferSize);
|
||||
if (result?.blobQuotaExceeded) {
|
||||
reject(new BlobQuotaExceeded());
|
||||
} else if (result?.storageQuotaExceeded) {
|
||||
reject(new StorageQuotaExceeded());
|
||||
}
|
||||
});
|
||||
stream.on('error', reject);
|
||||
stream.on('end', () => {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
|
||||
if (checkExceeded(buffer.length)) {
|
||||
result = checkExceeded(buffer.length);
|
||||
if (result?.blobQuotaExceeded) {
|
||||
reject(new BlobQuotaExceeded());
|
||||
} else if (result?.storageQuotaExceeded) {
|
||||
reject(new StorageQuotaExceeded());
|
||||
} else {
|
||||
resolve(buffer);
|
||||
}
|
||||
|
||||
@@ -471,6 +471,7 @@ enum ErrorNames {
|
||||
SPACE_NOT_FOUND
|
||||
SPACE_OWNER_NOT_FOUND
|
||||
SPACE_SHOULD_HAVE_ONLY_ONE_OWNER
|
||||
STORAGE_QUOTA_EXCEEDED
|
||||
SUBSCRIPTION_ALREADY_EXISTS
|
||||
SUBSCRIPTION_EXPIRED
|
||||
SUBSCRIPTION_HAS_BEEN_CANCELED
|
||||
|
||||
Reference in New Issue
Block a user