✨ Refactor quota check service implementation (#472)
This commit is contained in:
@@ -334,19 +334,13 @@ export class DocumentsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fileToProcess) {
|
if (fileToProcess) {
|
||||||
if (this.quotaEnabled) {
|
// if quota is enabled, check if the user has enough space
|
||||||
const userQuota = await this.userQuota(context);
|
await this.checkQuotaAndCleanUp(
|
||||||
const leftQuota = this.defaultQuota - userQuota;
|
fileToProcess.upload_data.size,
|
||||||
|
fileToProcess.id,
|
||||||
|
context,
|
||||||
|
);
|
||||||
|
|
||||||
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.size = fileToProcess.upload_data.size;
|
||||||
driveItem.is_directory = false;
|
driveItem.is_directory = false;
|
||||||
driveItem.extension = fileToProcess.metadata.name.split(".").pop();
|
driveItem.extension = fileToProcess.metadata.name.split(".").pop();
|
||||||
@@ -793,16 +787,8 @@ export class DocumentsService {
|
|||||||
const driveItemVersion = getDefaultDriveItemVersion(version, context);
|
const driveItemVersion = getDefaultDriveItemVersion(version, context);
|
||||||
const metadata = await getFileMetadata(driveItemVersion.file_metadata.external_id, context);
|
const metadata = await getFileMetadata(driveItemVersion.file_metadata.external_id, context);
|
||||||
|
|
||||||
if (this.quotaEnabled) {
|
// if quota is enabled, check if the user has enough space
|
||||||
const userQuota = await this.userQuota(context);
|
await this.checkQuotaAndCleanUp(metadata.size, metadata.external_id, 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_size = metadata.size;
|
||||||
driveItemVersion.file_metadata.size = metadata.size;
|
driveItemVersion.file_metadata.size = metadata.size;
|
||||||
@@ -851,10 +837,6 @@ export class DocumentsService {
|
|||||||
return driveItemVersion;
|
return driveItemVersion;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error({ error: `${error}` }, "Failed to create Drive item version");
|
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));
|
CrudException.throwMe(error, new CrudException("Failed to create Drive item version", 500));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1152,4 +1134,16 @@ export class DocumentsService {
|
|||||||
|
|
||||||
return await this.getTab(tabId, context);
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user