✨ Handling quota limit error (#437)
This commit is contained in:
@@ -60,7 +60,7 @@ import {
|
||||
RealtimeEntityActionType,
|
||||
ResourcePath,
|
||||
} from "../../../core/platform/services/realtime/types";
|
||||
|
||||
import config from "config";
|
||||
export class DocumentsService {
|
||||
version: "1";
|
||||
repository: Repository<DriveFile>;
|
||||
@@ -69,6 +69,12 @@ export class DocumentsService {
|
||||
driveTdriveTabRepository: Repository<DriveTdriveTabEntity>;
|
||||
ROOT: RootType = "root";
|
||||
TRASH: TrashType = "trash";
|
||||
quotaEnabled: boolean = config.has("drive.featureUserQuota")
|
||||
? config.get("drive.featureUserQuota")
|
||||
: false;
|
||||
defaultQuota: number = config.has("drive.defaultUserQuota")
|
||||
? config.get("drive.defaultUserQuota")
|
||||
: 0;
|
||||
logger: TdriveLogger = getLogger("Documents Service");
|
||||
|
||||
async init(): Promise<this> {
|
||||
@@ -328,6 +334,19 @@ export class DocumentsService {
|
||||
}
|
||||
|
||||
if (fileToProcess) {
|
||||
if (this.quotaEnabled) {
|
||||
const userQuota = await this.userQuota(context);
|
||||
const leftQuota = this.defaultQuota - userQuota;
|
||||
|
||||
if (fileToProcess.upload_data.size > leftQuota) {
|
||||
// clean up everything
|
||||
await globalResolver.services.files.delete(fileToProcess.id, context);
|
||||
throw new CrudException(
|
||||
`Not enough space: ${fileToProcess.upload_data.size}, ${leftQuota}.`,
|
||||
403,
|
||||
);
|
||||
}
|
||||
}
|
||||
driveItem.size = fileToProcess.upload_data.size;
|
||||
driveItem.is_directory = false;
|
||||
driveItem.extension = fileToProcess.metadata.name.split(".").pop();
|
||||
@@ -774,6 +793,17 @@ export class DocumentsService {
|
||||
const driveItemVersion = getDefaultDriveItemVersion(version, context);
|
||||
const metadata = await getFileMetadata(driveItemVersion.file_metadata.external_id, context);
|
||||
|
||||
if (this.quotaEnabled) {
|
||||
const userQuota = await this.userQuota(context);
|
||||
const leftQuota = this.defaultQuota - userQuota;
|
||||
|
||||
if (metadata.size > leftQuota) {
|
||||
// clean up everything
|
||||
await globalResolver.services.files.delete(metadata.external_id, context);
|
||||
throw new CrudException(`Not enough space: ${metadata.size}, ${leftQuota}.`, 403);
|
||||
}
|
||||
}
|
||||
|
||||
driveItemVersion.file_size = metadata.size;
|
||||
driveItemVersion.file_metadata.size = metadata.size;
|
||||
driveItemVersion.file_metadata.name = metadata.name;
|
||||
@@ -820,8 +850,12 @@ export class DocumentsService {
|
||||
|
||||
return driveItemVersion;
|
||||
} catch (error) {
|
||||
this.logger.error({ error: `${error}` }, "Failed to create Drive item version");
|
||||
throw new CrudException("Failed to create Drive item version", 500);
|
||||
logger.error({ error: `${error}` }, "Failed to create Drive item version");
|
||||
// if error code is 403, it means the user exceeded the quota limit
|
||||
if (error.code === 403) {
|
||||
CrudException.throwMe(error, new CrudException("Quota limit exceeded", 403));
|
||||
}
|
||||
CrudException.throwMe(error, new CrudException("Failed to create Drive item version", 500));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export class DocumentsController {
|
||||
version: Partial<FileVersion>;
|
||||
};
|
||||
}>,
|
||||
): Promise<DriveFile> => {
|
||||
): Promise<DriveFile | any> => {
|
||||
try {
|
||||
const context = getDriveExecutionContext(request);
|
||||
|
||||
@@ -73,6 +73,7 @@ export class DocumentsController {
|
||||
|
||||
const { item, version } = request.body;
|
||||
|
||||
//
|
||||
return await globalResolver.services.documents.documents.create(
|
||||
createdFile,
|
||||
item,
|
||||
@@ -305,14 +306,19 @@ export class DocumentsController {
|
||||
Body: Partial<FileVersion>;
|
||||
Querystring: { public_token?: string };
|
||||
}>,
|
||||
): Promise<FileVersion> => {
|
||||
const context = getDriveExecutionContext(request);
|
||||
const { id } = request.params;
|
||||
const version = request.body;
|
||||
): Promise<FileVersion | any> => {
|
||||
try {
|
||||
const context = getDriveExecutionContext(request);
|
||||
const { id } = request.params;
|
||||
const version = request.body;
|
||||
|
||||
if (!id) throw new CrudException("Missing id", 400);
|
||||
if (!id) throw new CrudException("Missing id", 400);
|
||||
|
||||
return await globalResolver.services.documents.documents.createVersion(id, version, context);
|
||||
return await globalResolver.services.documents.documents.createVersion(id, version, context);
|
||||
} catch (error) {
|
||||
logger.error({ error: `${error}` }, "Failed to create Drive item version");
|
||||
CrudException.throwMe(error, new CrudException("Failed to create Drive item version", 500));
|
||||
}
|
||||
};
|
||||
|
||||
downloadGetToken = async (
|
||||
|
||||
@@ -233,8 +233,14 @@ export default class UserApi {
|
||||
};
|
||||
|
||||
async createDocumentFromFilename(
|
||||
file_name: "sample.png",
|
||||
parent_id = "root"
|
||||
file_name:
|
||||
| "sample.png"
|
||||
| "sample.doc"
|
||||
| "sample.pdf"
|
||||
| "sample.zip"
|
||||
| "sample.mp4"
|
||||
| "sample.gif",
|
||||
parent_id = "root",
|
||||
) {
|
||||
const file = await this.uploadFile(file_name);
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
||||
import { init, TestPlatform } from "../setup";
|
||||
import UserApi from "../common/user-api";
|
||||
import config from "config";
|
||||
import { e2e_createDocumentFile, e2e_createVersion } from "./utils";
|
||||
import { deserialize } from "class-transformer";
|
||||
import { ResourceUpdateResponse } from "../../../src/utils/types";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
jest.mock("config");
|
||||
|
||||
describe("the Drive feature", () => {
|
||||
const filesUrl = "/internal/services/files/v1";
|
||||
let platform: TestPlatform;
|
||||
let currentUser: UserApi;
|
||||
let configHasSpy: jest.SpyInstance;
|
||||
let configGetSpy: jest.SpyInstance;
|
||||
|
||||
beforeEach(async () => {
|
||||
configHasSpy = jest.spyOn(config, "has");
|
||||
configGetSpy = jest.spyOn(config, "get");
|
||||
|
||||
configHasSpy.mockImplementation((setting: string) => {
|
||||
const value = jest.requireActual("config").has(setting);
|
||||
return value;
|
||||
});
|
||||
configGetSpy.mockImplementation((setting: string) => {
|
||||
if (setting === "drive.featureUserQuota") {
|
||||
return true;
|
||||
}
|
||||
if (setting === "drive.defaultUserQuota") {
|
||||
return 2000000;
|
||||
}
|
||||
return jest.requireActual("config").get(setting);
|
||||
});
|
||||
platform = await init({
|
||||
services: [
|
||||
"webserver",
|
||||
"database",
|
||||
"applications",
|
||||
"search",
|
||||
"storage",
|
||||
"message-queue",
|
||||
"user",
|
||||
"search",
|
||||
"files",
|
||||
"websocket",
|
||||
"messages",
|
||||
"auth",
|
||||
"realtime",
|
||||
"channels",
|
||||
"counter",
|
||||
"statistics",
|
||||
"platform-services",
|
||||
"documents",
|
||||
],
|
||||
});
|
||||
currentUser = await UserApi.getInstance(platform);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await platform?.tearDown();
|
||||
platform = null;
|
||||
configGetSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("did create the drive item with size under quota", async () => {
|
||||
const result = await currentUser.uploadFileAndCreateDocument("sample.doc");
|
||||
expect(result).toBeDefined();
|
||||
});
|
||||
|
||||
it("did not upload the drive item with size above quota", async () => {
|
||||
const item = await currentUser.uploadFile("sample.mp4");
|
||||
expect(item).toBeDefined();
|
||||
const result: any = await currentUser.createDocumentFromFile(item);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.statusCode).toBe(403);
|
||||
expect(result.error).toBe("Forbidden");
|
||||
expect(result.message).toContain("Not enough space");
|
||||
const fileDownloadResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${filesUrl}/companies/${platform.workspace.company_id}/files/${item.id}/download`,
|
||||
});
|
||||
// make sure the file was removed
|
||||
expect(fileDownloadResponse).toBeTruthy();
|
||||
expect(fileDownloadResponse.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
it("did create a version for a drive item", async () => {
|
||||
const item = await currentUser.createDefaultDocument();
|
||||
const fileUploadResponse = await e2e_createDocumentFile(
|
||||
platform,
|
||||
"../common/assets/sample.mp4",
|
||||
);
|
||||
const fileUploadResult: any = deserialize<ResourceUpdateResponse<File>>(
|
||||
ResourceUpdateResponse,
|
||||
fileUploadResponse.body,
|
||||
);
|
||||
const file_metadata = { external_id: fileUploadResult.resource.id };
|
||||
|
||||
const result: any = await e2e_createVersion(platform, item.id, {
|
||||
filename: "file2",
|
||||
file_metadata,
|
||||
});
|
||||
expect(result).toBeDefined();
|
||||
expect(result.statusCode).toBe(403);
|
||||
const fileDownloadResponse = await platform.app.inject({
|
||||
method: "GET",
|
||||
url: `${filesUrl}/companies/${platform.workspace.company_id}/files/${item.id}/download`,
|
||||
});
|
||||
// make sure the file was removed
|
||||
expect(fileDownloadResponse).toBeTruthy();
|
||||
expect(fileDownloadResponse.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
@@ -54,8 +54,9 @@ export const e2e_createVersion = async (
|
||||
});
|
||||
};
|
||||
|
||||
export const e2e_createDocumentFile = async (platform: TestPlatform) => {
|
||||
const filePath = `${__dirname}/assets/test.txt`;
|
||||
export const e2e_createDocumentFile = async (platform: TestPlatform, documentPath?: string) => {
|
||||
const subFilePath = documentPath ?? "assets/test.txt";
|
||||
const filePath = `${__dirname}/${subFilePath}`;
|
||||
const token = await platform.auth.getJWTToken();
|
||||
const form = formAutoContent({ file: fs.createReadStream(filePath) });
|
||||
form.headers["authorization"] = `Bearer ${token}`;
|
||||
|
||||
Reference in New Issue
Block a user