Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: handle model upload limits globally

Sidwebworks5b42b61e1dbf
diff --git a/apps/erp/app/components/CadModel.tsx b/apps/erp/app/components/CadModel.tsx
index 22fda91ff8..ed44776d18 100644
--- a/apps/erp/app/components/CadModel.tsx
+++ b/apps/erp/app/components/CadModel.tsx
@@ -9,7 +9,11 @@ import {
toast
} from "@carbon/react";
import { useMode } from "@carbon/remix";
-import { convertKbToString, supportedModelTypes } from "@carbon/utils";
+import {
+ convertKbToString,
+ getFileSizeLimit,
+ supportedModelTypes
+} from "@carbon/utils";
import { nanoid } from "nanoid";
import { useState } from "react";
import { useDropzone } from "react-dropzone";
@@ -18,7 +22,7 @@ import { useFetcher } from "react-router";
import { useUser } from "~/hooks";
import { getPrivateUrl, path } from "~/utils/path";
-const fileSizeLimitMb = 120;
+const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");
type CadModelProps = {
modelPath: string | null;
@@ -159,10 +163,9 @@ const CadModelUpload = ({
const { getRootProps, getInputProps } = useDropzone({
disabled: hasFile,
multiple: false,
- maxSize: fileSizeLimitMb * 1024 * 1024, // 50 MB
+ maxSize: SIZE_LIMIT.bytes,
onDropAccepted: (acceptedFiles) => {
const file = acceptedFiles[0];
- const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
const fileExtension = file.name.split(".").pop()?.toLowerCase();
if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
@@ -171,8 +174,8 @@ const CadModelUpload = ({
return;
}
- if (file.size > fileSizeLimit) {
- toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ if (file.size > SIZE_LIMIT.bytes) {
+ toast.error(`File size too big (max. ${SIZE_LIMIT.format()})`);
return;
}
@@ -182,7 +185,7 @@ const CadModelUpload = ({
const { errors } = fileRejections[0];
let message;
if (errors[0].code === "file-too-large") {
- message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ message = `File size too big (max. ${SIZE_LIMIT.format()})`;
} else if (errors[0].code === "file-invalid-type") {
message = "File type not supported";
} else {
diff --git a/apps/erp/app/modules/items/ui/Parts/PartForm.tsx b/apps/erp/app/modules/items/ui/Parts/PartForm.tsx
index 72c8be11e3..ab32bcacb6 100644
--- a/apps/erp/app/modules/items/ui/Parts/PartForm.tsx
+++ b/apps/erp/app/modules/items/ui/Parts/PartForm.tsx
@@ -15,7 +15,11 @@ import {
toast,
VStack
} from "@carbon/react";
-import { convertKbToString, supportedModelTypes } from "@carbon/utils";
+import {
+ convertKbToString,
+ getFileSizeLimit,
+ supportedModelTypes
+} from "@carbon/utils";
import type { PostgrestResponse } from "@supabase/supabase-js";
import { nanoid } from "nanoid";
import { useEffect, useState } from "react";
@@ -53,7 +57,7 @@ type PartFormProps = {
onClose?: () => void;
};
-const fileSizeLimitMb = 120;
+const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");
function startsWithLetter(value: string) {
return /^[A-Za-z]/.test(value);
@@ -113,10 +117,9 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
const { getRootProps, getInputProps, isDragActive } = useDropzone({
multiple: false,
- maxSize: fileSizeLimitMb * 1024 * 1024, // 50 MB
+ maxSize: SIZE_LIMIT.bytes,
onDropAccepted: async (acceptedFiles) => {
const file = acceptedFiles[0];
- const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
const fileExtension = file.name.split(".").pop()?.toLowerCase();
if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
@@ -125,8 +128,8 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
return;
}
- if (file.size > fileSizeLimit) {
- toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ if (file.size > SIZE_LIMIT.bytes) {
+ toast.error(`File size too big (max. ${SIZE_LIMIT.format()})`);
return;
}
@@ -136,7 +139,7 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
const { errors } = fileRejections[0];
let message;
if (errors[0].code === "file-too-large") {
- message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ message = `File size too big (max. ${SIZE_LIMIT.format()})`;
} else if (errors[0].code === "file-invalid-type") {
message = "File type not supported";
} else {
diff --git a/apps/erp/app/modules/items/ui/Tools/ToolForm.tsx b/apps/erp/app/modules/items/ui/Tools/ToolForm.tsx
index 21f4580e75..9a8b98973b 100644
--- a/apps/erp/app/modules/items/ui/Tools/ToolForm.tsx
+++ b/apps/erp/app/modules/items/ui/Tools/ToolForm.tsx
@@ -15,7 +15,11 @@ import {
toast,
VStack
} from "@carbon/react";
-import { convertKbToString, supportedModelTypes } from "@carbon/utils";
+import {
+ convertKbToString,
+ getFileSizeLimit,
+ supportedModelTypes
+} from "@carbon/utils";
import type { PostgrestResponse } from "@supabase/supabase-js";
import { nanoid } from "nanoid";
import { useEffect, useState } from "react";
@@ -53,7 +57,7 @@ type ToolFormProps = {
onClose?: () => void;
};
-const fileSizeLimitMb = 50;
+const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");
function startsWithLetter(value: string) {
return /^[A-Za-z]/.test(value);
@@ -113,10 +117,9 @@ const ToolForm = ({ initialValues, type = "card", onClose }: ToolFormProps) => {
const { getRootProps, getInputProps, isDragActive } = useDropzone({
multiple: false,
- maxSize: fileSizeLimitMb * 1024 * 1024,
+ maxSize: SIZE_LIMIT.bytes,
onDropAccepted: async (acceptedFiles) => {
const file = acceptedFiles[0];
- const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
const fileExtension = file.name.split(".").pop()?.toLowerCase();
if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
@@ -124,8 +127,8 @@ const ToolForm = ({ initialValues, type = "card", onClose }: ToolFormProps) => {
return;
}
- if (file.size > fileSizeLimit) {
- toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ if (file.size > SIZE_LIMIT.bytes) {
+ toast.error(`File size too big (max. ${SIZE_LIMIT.format()})`);
return;
}
@@ -135,7 +138,7 @@ const ToolForm = ({ initialValues, type = "card", onClose }: ToolFormProps) => {
const { errors } = fileRejections[0];
let message;
if (errors[0].code === "file-too-large") {
- message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ message = `File size too big (max. ${SIZE_LIMIT.format()})`;
} else if (errors[0].code === "file-invalid-type") {
message = "File type not supported";
} else {
diff --git a/examples/quote-configurator/app/components/ModelUpload.tsx b/examples/quote-configurator/app/components/ModelUpload.tsx
index d2b9fbcbe1..24cc407e8f 100644
--- a/examples/quote-configurator/app/components/ModelUpload.tsx
+++ b/examples/quote-configurator/app/components/ModelUpload.tsx
@@ -1,5 +1,5 @@
import { cn, Spinner, toast } from "@carbon/react";
-import { convertKbToString } from "@carbon/utils";
+import { convertKbToString, getFileSizeLimit } from "@carbon/utils";
import { useDropzone } from "react-dropzone";
import { LuCloudUpload } from "react-icons/lu";
@@ -10,7 +10,7 @@ type ModelUploadProps = {
onFileChange: (file: File | null) => void;
};
-const fileSizeLimitMb = 120;
+const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");
const supportedModelTypes = ["stp", "step"];
export const ModelUpload = ({
@@ -27,10 +27,9 @@ export const ModelUpload = ({
},
disabled: hasFile,
multiple: false,
- maxSize: fileSizeLimitMb * 1024 * 1024, // 50 MB
+ maxSize: SIZE_LIMIT.bytes,
onDropAccepted: (acceptedFiles) => {
const file = acceptedFiles[0];
- const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
const fileExtension = file.name.split(".").pop()?.toLowerCase();
if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
@@ -39,8 +38,8 @@ export const ModelUpload = ({
return;
}
- if (file.size > fileSizeLimit) {
- toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ if (file.size > SIZE_LIMIT.bytes) {
+ toast.error(`File size too big (max. ${SIZE_LIMIT.format()})`);
return;
}
@@ -50,7 +49,7 @@ export const ModelUpload = ({
const { errors } = fileRejections[0];
let message;
if (errors[0].code === "file-too-large") {
- message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ message = `File size too big (max. ${SIZE_LIMIT.format()})`;
} else if (errors[0].code === "file-invalid-type") {
message = "File type not supported";
} else {
diff --git a/examples/upload-3d-model/app/components/ModelUpload.tsx b/examples/upload-3d-model/app/components/ModelUpload.tsx
index d2b9fbcbe1..24cc407e8f 100644
--- a/examples/upload-3d-model/app/components/ModelUpload.tsx
+++ b/examples/upload-3d-model/app/components/ModelUpload.tsx
@@ -1,5 +1,5 @@
import { cn, Spinner, toast } from "@carbon/react";
-import { convertKbToString } from "@carbon/utils";
+import { convertKbToString, getFileSizeLimit } from "@carbon/utils";
import { useDropzone } from "react-dropzone";
import { LuCloudUpload } from "react-icons/lu";
@@ -10,7 +10,7 @@ type ModelUploadProps = {
onFileChange: (file: File | null) => void;
};
-const fileSizeLimitMb = 120;
+const SIZE_LIMIT = getFileSizeLimit("CAD_MODEL_UPLOAD");
const supportedModelTypes = ["stp", "step"];
export const ModelUpload = ({
@@ -27,10 +27,9 @@ export const ModelUpload = ({
},
disabled: hasFile,
multiple: false,
- maxSize: fileSizeLimitMb * 1024 * 1024, // 50 MB
+ maxSize: SIZE_LIMIT.bytes,
onDropAccepted: (acceptedFiles) => {
const file = acceptedFiles[0];
- const fileSizeLimit = fileSizeLimitMb * 1024 * 1024;
const fileExtension = file.name.split(".").pop()?.toLowerCase();
if (!fileExtension || !supportedModelTypes.includes(fileExtension)) {
@@ -39,8 +38,8 @@ export const ModelUpload = ({
return;
}
- if (file.size > fileSizeLimit) {
- toast.error(`File size too big (max. ${fileSizeLimitMb} MB)`);
+ if (file.size > SIZE_LIMIT.bytes) {
+ toast.error(`File size too big (max. ${SIZE_LIMIT.format()})`);
return;
}
@@ -50,7 +49,7 @@ export const ModelUpload = ({
const { errors } = fileRejections[0];
let message;
if (errors[0].code === "file-too-large") {
- message = `File size too big (max. ${fileSizeLimitMb} MB)`;
+ message = `File size too big (max. ${SIZE_LIMIT.format()})`;
} else if (errors[0].code === "file-invalid-type") {
message = "File type not supported";
} else {
diff --git a/packages/utils/src/const.ts b/packages/utils/src/const.ts
index f884b3112b..6011cd8751 100644
--- a/packages/utils/src/const.ts
+++ b/packages/utils/src/const.ts
@@ -1 +1,23 @@
export const SUPPORT_EMAIL = "[email protected]";
+
+export const FILE_SIZE_LIMIT_MB = {
+ CAD_MODEL_UPLOAD: 120,
+ DOCUMENT_UPLOAD: 50
+} as const;
+
+export const getFileSizeLimit = (type: keyof typeof FILE_SIZE_LIMIT_MB) => {
+ const valueMegaBytes = FILE_SIZE_LIMIT_MB[type];
+ const valueBytes = valueMegaBytes * 1024 * 1024;
+
+ return {
+ get megabytes() {
+ return valueMegaBytes;
+ },
+ format() {
+ return `${valueMegaBytes} ${valueMegaBytes > 1 ? "MBs" : "MB"}`;
+ },
+ get bytes() {
+ return valueBytes;
+ }
+ } as const;
+};