aboutsummaryrefslogtreecommitdiffstats
path: root/packages/api/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/api/utils')
-rw-r--r--packages/api/utils/upload.ts17
1 files changed, 16 insertions, 1 deletions
diff --git a/packages/api/utils/upload.ts b/packages/api/utils/upload.ts
index daff1fb9..7e322c76 100644
--- a/packages/api/utils/upload.ts
+++ b/packages/api/utils/upload.ts
@@ -12,6 +12,10 @@ import {
} from "@karakeep/shared/assetdb";
import serverConfig from "@karakeep/shared/config";
import { AuthedContext } from "@karakeep/trpc";
+import {
+ checkStorageQuota,
+ StorageQuotaError,
+} from "@karakeep/trpc/lib/storageQuota";
const MAX_UPLOAD_SIZE_BYTES = serverConfig.maxAssetSizeMb * 1024 * 1024;
@@ -42,7 +46,7 @@ export async function uploadAsset(
db: AuthedContext["db"],
formData: { file: File } | { image: File },
): Promise<
- | { error: string; status: 400 | 413 }
+ | { error: string; status: 400 | 413 | 403 }
| {
assetId: string;
contentType: string;
@@ -66,6 +70,16 @@ export async function uploadAsset(
return { error: "Asset is too big", status: 413 };
}
+ let quotaApproved;
+ try {
+ quotaApproved = await checkStorageQuota(db, user.id, data.size);
+ } catch (error) {
+ if (error instanceof StorageQuotaError) {
+ return { error: error.message, status: 403 };
+ }
+ throw error;
+ }
+
let tempFilePath: string | undefined;
try {
@@ -94,6 +108,7 @@ export async function uploadAsset(
assetId: assetDb.id,
assetPath: tempFilePath,
metadata: { contentType, fileName },
+ quotaApproved,
});
return {