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.

avoid creating files with same name

Sara Vieira1cc9b136ac68
diff --git a/apps/designer/app/routes/designer/$id.tsx b/apps/designer/app/routes/designer/$id.tsx
index a133af2040..2471ad868d 100644
--- a/apps/designer/app/routes/designer/$id.tsx
+++ b/apps/designer/app/routes/designer/$id.tsx
@@ -11,6 +11,7 @@ import * as db from "~/shared/db";
import config from "~/config";
import env from "~/env.server";
import { z } from "zod";
+import ObjectID from "bson-objectid";
export { links };
export const loader: LoaderFunction = async ({ params }) => {
@@ -43,26 +44,38 @@ type ImageUpload = z.infer<typeof ImageUpload>;
export const action: ActionFunction = async ({ request, params }) => {
if (params.id === undefined) throw new Error("Project id undefined");
- const formData = await unstable_parseMultipartFormData(
- request,
- unstable_createFileUploadHandler({
- maxPartSize: 10_000_000,
- directory,
- file: ({ filename }) => filename,
- })
- );
- const imageInfo = ImageUpload.parse(formData.get("image"));
+ try {
+ const randomId = ObjectID.toString();
+ const formData = await unstable_parseMultipartFormData(
+ request,
+ unstable_createFileUploadHandler({
+ maxPartSize: 10_000_000,
+ directory,
+ file: ({ filename }) => `${randomId}_${filename}`,
+ })
+ );
+
+ const imageInfo = ImageUpload.parse(formData.get("image"));
+
const info = imageInfo as ImageUpload;
+ const fileName = `${randomId}_${info.name}`;
const data = {
type: info.type,
- name: info.name,
- path: `/uploads/${info.name}`,
+ name: fileName,
+ path: `/uploads/${fileName}`,
};
db.assets.create(params.id, data);
+
+ return {
+ ok: true,
+ };
+ } catch (error) {
+ if (error instanceof Error) {
+ return {
+ errors: error.message,
+ };
+ }
}
- return {
- ok: true,
- };
};
const DesignerRoute = () => {