fix: Protect against exports larger than memory/max

This commit is contained in:
Tom Moor
2023-09-02 22:11:53 -04:00
parent 0054b7152e
commit 093ee74a90
3 changed files with 65 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import path from "path";
import { QueryTypes } from "sequelize";
import {
BeforeDestroy,
BelongsTo,
@@ -116,6 +117,30 @@ class Attachment extends IdModel {
await FileStorage.deleteFile(model.key);
}
// static methods
/**
* Get the total size of all attachments for a given team.
*
* @param teamId - The ID of the team to get the total size for.
* @returns A promise resolving to the total size of all attachments for the given team in bytes.
*/
static async getTotalSizeForTeam(teamId: string): Promise<number> {
const result = await this.sequelize!.query<{ total: string }>(
`
SELECT SUM(size) as total
FROM attachments
WHERE "teamId" = :teamId
`,
{
replacements: { teamId },
type: QueryTypes.SELECT,
}
);
return parseInt(result?.[0]?.total ?? "0", 10);
}
// associations
@BelongsTo(() => Team, "teamId")