Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

submit uploads serially to

Oleg Isonen06a15e8ca0ca
diff --git a/apps/designer/app/designer/shared/assets/asset-upload.tsx b/apps/designer/app/designer/shared/assets/asset-upload.tsx
index 4b1a2dcf03..76c2e0a870 100644
--- a/apps/designer/app/designer/shared/assets/asset-upload.tsx
+++ b/apps/designer/app/designer/shared/assets/asset-upload.tsx
@@ -1,4 +1,4 @@
-import { Form, useActionData, useSubmit } from "@remix-run/react";
+import { Form, useActionData } from "@remix-run/react";
import ObjectID from "bson-objectid";
import { ChangeEvent, useEffect, useRef } from "react";
import { Button, Flex, Text } from "@webstudio-is/design-system";
@@ -6,8 +6,9 @@ import { UploadIcon } from "@webstudio-is/icons";
import { type AssetType } from "@webstudio-is/asset-uploader";
import type { ActionData, PreviewAsset } from "./types";
import { FONT_MIME_TYPES } from "@webstudio-is/fonts";
+import { useSerialSubmit } from "./use-serial-submit";
-const readPreviews = (fileList: FileList): Promise<PreviewAsset[]> => {
+const toPreviewAssets = (fileList: FileList): Promise<PreviewAsset[]> => {
const assets: Array<Promise<PreviewAsset>> = Array.from(fileList).map(
(file) =>
new Promise((resolve, reject) => {
@@ -40,15 +41,15 @@ const useUpload = ({
onActionData: (data: ActionData) => void;
onSubmit: (assets: Array<PreviewAsset>) => void;
}) => {
- const submit = useSubmit();
+ const submit = useSerialSubmit();
const inputRef = useRef<HTMLInputElement | null>(null);
const actionData: ActionData | undefined = useActionData();
- const onFormChange = (event: ChangeEvent<HTMLFormElement>) => {
+ const onChange = (event: ChangeEvent<HTMLFormElement>) => {
const files = inputRef?.current?.files;
- if (files) {
+ if (files && files.length !== 0) {
submit(event.currentTarget);
- readPreviews(files).then(onSubmit);
+ toPreviewAssets(files).then(onSubmit);
event.currentTarget.reset();
}
};
@@ -59,7 +60,7 @@ const useUpload = ({
}
}, [actionData, onActionData]);
- return { inputRef, onChange: onFormChange };
+ return { inputRef, onChange };
};
const acceptMap = {
diff --git a/apps/designer/app/designer/shared/assets/use-serial-submit.ts b/apps/designer/app/designer/shared/assets/use-serial-submit.ts
new file mode 100644
index 0000000000..be89611d9f
--- /dev/null
+++ b/apps/designer/app/designer/shared/assets/use-serial-submit.ts
@@ -0,0 +1,48 @@
+import type { FormEncType, FormMethod, SubmitOptions } from "@remix-run/react";
+import { useActionData, useSubmit } from "@remix-run/react";
+import { useCallback, useEffect, useRef, useState } from "react";
+
+const initialSubmission: {
+ data: Array<[string, FormDataEntryValue]>;
+ options: SubmitOptions;
+} = {
+ data: [],
+ options: {},
+};
+
+/**
+ * Wrapps useSubmit hook to submit form data serially.
+ * This is useful when you want to submit multiple files, but send them one after another.
+ * Specifically Vercel has a single upload limit of 4.5MB, so we need to send them one by one so that this limit is per file, not per total upload
+ */
+export const useSerialSubmit = () => {
+ const submit = useSubmit();
+ const actionData = useActionData();
+ const [submission, setSubmission] =
+ useState<typeof initialSubmission>(initialSubmission);
+ const submittedRef = useRef(new Map());
+
+ useEffect(() => {
+ const submitted = submittedRef.current;
+ for (const entry of submission.data) {
+ if (submitted.has(entry) === false) {
+ const data = new FormData();
+ data.append(entry[0], entry[1]);
+ submit(data, submission.options);
+ submitted.set(entry, true);
+ break;
+ }
+ }
+ }, [actionData, submission, submit]);
+
+ return useCallback((form: HTMLFormElement) => {
+ const formData = new FormData(form);
+ const data = Array.from(formData.entries());
+ const options = {
+ method: form.method as FormMethod,
+ action: form.action,
+ encType: form.enctype as FormEncType,
+ };
+ setSubmission({ data, options });
+ }, []);
+};
diff --git a/packages/asset-uploader/src/constants.ts b/packages/asset-uploader/src/constants.ts
index 6616fc8004..6228cc05c7 100644
--- a/packages/asset-uploader/src/constants.ts
+++ b/packages/asset-uploader/src/constants.ts
@@ -1 +1,3 @@
export const DEFAULT_UPLOAD_PATH = "public/uploads";
+// By default using Vercel's limit https://vercel.com/docs/concepts/limits/overview#serverless-function-payload-size-limit
+export const MAX_UPLOAD_SIZE = "4.5";
diff --git a/packages/asset-uploader/src/upload.ts b/packages/asset-uploader/src/upload.ts
index e181b51506..c73f1fd47d 100644
--- a/packages/asset-uploader/src/upload.ts
+++ b/packages/asset-uploader/src/upload.ts
@@ -1,17 +1,18 @@
import { z } from "zod";
+import { MAX_UPLOAD_SIZE } from "./constants";
import { S3EnvVariables } from "./schema";
import { uploadToFs } from "./targets/fs/upload";
import { uploadToS3 } from "./targets/s3/upload";
const AssetEnvVariables = z.object({
- MAX_UPLOAD_SIZE: z.string().optional().default("10"),
+ MAX_UPLOAD_SIZE: z.string().optional().default(MAX_UPLOAD_SIZE),
});
const isS3Upload = S3EnvVariables.safeParse(process.env).success;
const commonUploadVars = AssetEnvVariables.parse(process.env);
// user inputs the max value in mb and we transform it to bytes
-const MAX_UPLOAD_SIZE = parseFloat(commonUploadVars.MAX_UPLOAD_SIZE) * 1e6;
+const maxSize = parseFloat(commonUploadVars.MAX_UPLOAD_SIZE) * 1e6;
export const uploadAssets = async ({
request,
@@ -22,9 +23,9 @@ export const uploadAssets = async ({
}) => {
try {
if (isS3Upload) {
- return await uploadToS3({ request, projectId, maxSize: MAX_UPLOAD_SIZE });
+ return await uploadToS3({ request, projectId, maxSize });
}
- return await uploadToFs({ request, projectId, maxSize: MAX_UPLOAD_SIZE });
+ return await uploadToFs({ request, projectId, maxSize });
} catch (error) {
if (error instanceof Error && "maxBytes" in error) {
throw new Error(