Skip to content

Commit

Permalink
✨ Refactor quota check service implementation (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy authored Mar 26, 2024
1 parent cb5a2ad commit 90ad26e
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,13 @@ 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,
);
}
}
// if quota is enabled, check if the user has enough space
await this.checkQuotaAndCleanUp(
fileToProcess.upload_data.size,
fileToProcess.id,
context,
);

driveItem.size = fileToProcess.upload_data.size;
driveItem.is_directory = false;
driveItem.extension = fileToProcess.metadata.name.split(".").pop();
Expand Down Expand Up @@ -793,16 +787,8 @@ 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);
}
}
// if quota is enabled, check if the user has enough space
await this.checkQuotaAndCleanUp(metadata.size, metadata.external_id, context);

driveItemVersion.file_size = metadata.size;
driveItemVersion.file_metadata.size = metadata.size;
Expand Down Expand Up @@ -851,10 +837,6 @@ export class DocumentsService {
return driveItemVersion;
} catch (error) {
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));
}
};
Expand Down Expand Up @@ -1152,4 +1134,16 @@ export class DocumentsService {

return await this.getTab(tabId, context);
};

checkQuotaAndCleanUp = async (size: number, fileId: string, context: DriveExecutionContext) => {
if (!this.quotaEnabled) return;

const userQuota = await this.userQuota(context);
const leftQuota = this.defaultQuota - userQuota;

if (size > leftQuota) {
await globalResolver.services.files.delete(fileId, context);
throw new CrudException(`Not enough space: ${size}, ${leftQuota}.`, 403);
}
};
}

0 comments on commit 90ad26e

Please sign in to comment.