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.

Add assets upload restriction 1 file per request (#710)

Ivan Starkovc8bd38d9fb6f
diff --git a/packages/asset-uploader/src/targets/s3/upload.ts b/packages/asset-uploader/src/targets/s3/upload.ts
index a46e091ad1..7989acfdc8 100644
--- a/packages/asset-uploader/src/targets/s3/upload.ts
+++ b/packages/asset-uploader/src/targets/s3/upload.ts
@@ -23,6 +23,7 @@ const AssetsUploadedSuccess = z.object({
});
const Ids = z.array(z.string().uuid());
+const MAX_FILES_PER_REQUEST = 1;
export const uploadToS3 = async ({
request,
@@ -33,6 +34,8 @@ export const uploadToS3 = async ({
projectId: string;
maxSize: number;
}): Promise<Array<Asset>> => {
+ const uploadHandler = createUploadHandler(MAX_FILES_PER_REQUEST);
+
const formData = await unstableCreateFileUploadHandler(
request,
unstableComposeUploadHandlers(
@@ -49,100 +52,112 @@ export const uploadToS3 = async ({
const fontsFormData = formData.getAll("font") as Array<string>;
const ids = Ids.parse(formData.getAll(idsFormDataFieldName));
- const assetsData = [...imagesFormData, ...fontsFormData].map(
- (dataString, i) => {
+ const assetsData = [...imagesFormData, ...fontsFormData]
+ .slice(0, MAX_FILES_PER_REQUEST)
+ .map((dataString, i) => {
// @todo validate with zod
return { ...JSON.parse(dataString), id: ids[i] };
- }
- );
+ });
return await createMany(projectId, assetsData);
};
-const uploadHandler = async ({
- file,
- maxSize,
-}: {
- file: UploadHandlerPart;
- maxSize: number;
-}): Promise<string | undefined> => {
- if (file.filename === undefined) {
- // Do not parse if it's not a file
- return;
- }
-
- if (!file.data) {
- throw new Error("Your asset seems to be empty");
- }
-
- // @todo this is going to put the entire file in memory
- // this has to be a stream that goes directly to s3
- // Size check has to happen as you stream and interrupted when size is too big
- // Also check if S3 client has an option to check the size limit
- const data = await toUint8Array(file.data);
-
- if (data.byteLength > maxSize) {
- throw new MaxPartSizeExceededError(file.name, maxSize);
- }
-
- const fileName = sanitizeS3Key(file.filename);
-
- const uniqueFilename = getUniqueFilename(fileName);
-
- const s3Env = S3Env.parse(process.env);
-
- // if there is no ACL passed we do not default since some providers do not support it
- const ACL = s3Env.S3_ACL ? { ACL: s3Env.S3_ACL } : {};
-
- const params: PutObjectCommandInput = {
- ...ACL,
- Bucket: s3Env.S3_BUCKET,
- Key: uniqueFilename,
- Body: data,
- ContentType: file.contentType,
- Metadata: {
- // encodeURIComponent is needed to support special characters like Cyrillic
- filename: encodeURIComponent(fileName) || "unnamed",
- },
- };
+const createUploadHandler = (maxFiles: number) => {
+ let count = 0;
+
+ return async ({
+ file,
+ maxSize,
+ }: {
+ file: UploadHandlerPart;
+ maxSize: number;
+ }): Promise<string | undefined> => {
+ if (file.filename === undefined) {
+ // Do not parse if it's not a file
+ return;
+ }
+
+ if (count >= maxFiles) {
+ // Do not throw, just ignore the file
+ // In case of throw we need to delete previously uploaded files
+ return;
+ }
- const upload = new Upload({ client: getS3Client(), params });
+ count++;
- AssetsUploadedSuccess.parse(await upload.done());
+ if (!file.data) {
+ throw new Error("Your asset seems to be empty");
+ }
- const type = file.contentType.startsWith("image")
- ? ("image" as const)
- : ("font" as const);
+ // @todo this is going to put the entire file in memory
+ // this has to be a stream that goes directly to s3
+ // Size check has to happen as you stream and interrupted when size is too big
+ // Also check if S3 client has an option to check the size limit
+ const data = await toUint8Array(file.data);
- const baseAssetOptions = {
- name: uniqueFilename,
- size: data.byteLength,
- data,
- location: Location.REMOTE,
- };
- let assetOptions;
-
- if (type === "image") {
- assetOptions = {
- // Id will be set later
- id: "",
- type,
- ...baseAssetOptions,
+ if (data.byteLength > maxSize) {
+ throw new MaxPartSizeExceededError(file.name, maxSize);
+ }
+
+ const fileName = sanitizeS3Key(file.filename);
+
+ const uniqueFilename = getUniqueFilename(fileName);
+
+ const s3Env = S3Env.parse(process.env);
+
+ // if there is no ACL passed we do not default since some providers do not support it
+ const ACL = s3Env.S3_ACL ? { ACL: s3Env.S3_ACL } : {};
+
+ const params: PutObjectCommandInput = {
+ ...ACL,
+ Bucket: s3Env.S3_BUCKET,
+ Key: uniqueFilename,
+ Body: data,
+ ContentType: file.contentType,
+ Metadata: {
+ // encodeURIComponent is needed to support special characters like Cyrillic
+ filename: encodeURIComponent(fileName) || "unnamed",
+ },
};
- } else if (type === "font") {
- assetOptions = {
- // Id will be set later
- id: "",
- type,
- ...baseAssetOptions,
+
+ const upload = new Upload({ client: getS3Client(), params });
+
+ AssetsUploadedSuccess.parse(await upload.done());
+
+ const type = file.contentType.startsWith("image")
+ ? ("image" as const)
+ : ("font" as const);
+
+ const baseAssetOptions = {
+ name: uniqueFilename,
+ size: data.byteLength,
+ data,
+ location: Location.REMOTE,
};
- }
+ let assetOptions;
+
+ if (type === "image") {
+ assetOptions = {
+ // Id will be set later
+ id: "",
+ type,
+ ...baseAssetOptions,
+ };
+ } else if (type === "font") {
+ assetOptions = {
+ // Id will be set later
+ id: "",
+ type,
+ ...baseAssetOptions,
+ };
+ }
- if (assetOptions === undefined) {
- throw new Error("Asset type not supported");
- }
+ if (assetOptions === undefined) {
+ throw new Error("Asset type not supported");
+ }
- const assetData = await getAssetData(assetOptions);
+ const assetData = await getAssetData(assetOptions);
- return JSON.stringify(assetData);
+ return JSON.stringify(assetData);
+ };
};