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.

Clone assets (#969)

Roman Pominov6e5e440e26e2
diff --git a/packages/asset-uploader/src/db/clone.ts b/packages/asset-uploader/src/db/clone.ts
new file mode 100644
index 0000000000..1505c92811
--- /dev/null
+++ b/packages/asset-uploader/src/db/clone.ts
@@ -0,0 +1,53 @@
+import { prisma, type Prisma, type Project } from "@webstudio-is/prisma-client";
+import {
+ authorizeProject,
+ type AppContext,
+} from "@webstudio-is/trpc-interface/server";
+
+export const cloneAssets = async (
+ {
+ fromProjectId,
+ toProjectId,
+ dontCheckEditPermission = false,
+ }: {
+ fromProjectId: Project["id"];
+ toProjectId: Project["id"];
+
+ /**
+ * Don't use unless absolutely have to (e.g. because of transactions)
+ * and unless it's obvious on the call site that permission is checked
+ */
+ dontCheckEditPermission?: boolean;
+ },
+ context: AppContext,
+ client: Prisma.TransactionClient = prisma
+) => {
+ const canView = await authorizeProject.hasProjectPermit(
+ { projectId: fromProjectId, permit: "view" },
+ context
+ );
+
+ if (canView === false) {
+ throw new Error("You don't have access to this project assets");
+ }
+
+ if (dontCheckEditPermission === false) {
+ const canEdit = await authorizeProject.hasProjectPermit(
+ { projectId: toProjectId, permit: "edit" },
+ context
+ );
+
+ if (canEdit === false) {
+ throw new Error("You don't have access to create this project assets");
+ }
+ }
+
+ const assets = await client.asset.findMany({
+ where: { projectId: fromProjectId, status: "UPLOADED" },
+ });
+
+ await client.asset.createMany({
+ // we intentionally keep old ids in order for all references in styles/props to work
+ data: assets.map((asset) => ({ ...asset, projectId: toProjectId })),
+ });
+};
diff --git a/packages/asset-uploader/src/db/index.ts b/packages/asset-uploader/src/db/index.ts
index 7a4923ced3..3595c75e02 100644
--- a/packages/asset-uploader/src/db/index.ts
+++ b/packages/asset-uploader/src/db/index.ts
@@ -1,3 +1,4 @@
export * from "./create";
export * from "./load";
export * from "./delete";
+export * from "./clone";
diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.ts
index 830c7d92a1..158809e22b 100644
--- a/packages/asset-uploader/src/delete.ts
+++ b/packages/asset-uploader/src/delete.ts
@@ -35,7 +35,16 @@ export const deleteAssets = async (
await deleteFromDb(props, context);
+ const assetsByName = await prisma.asset.findMany({
+ where: { name: { in: assets.map((asset) => asset.name) } },
+ });
+ const stillUsedNames = new Set(assetsByName.map((asset) => asset.name));
+
for (const asset of assets) {
+ if (stillUsedNames.has(asset.name)) {
+ continue;
+ }
+
if (asset.location === "REMOTE") {
await deleteFromS3(asset.name);
} else {
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index e7b9088ea0..0e47c09e92 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -28,7 +28,7 @@ enum UploadStatus {
}
model Asset {
- id String @default(uuid())
+ id String @default(uuid()) // not unique!
projectId String
format String
size Int
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index a2edc730c0..8dbcd53793 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -23,7 +23,7 @@ export const parseBuild = async (build: DbBuild): Promise<Build> => {
createdAt: build.createdAt.toISOString(),
pages,
breakpoints: Breakpoints.parse(JSON.parse(build.breakpoints)),
- styles: await parseStyles(build.styles),
+ styles: await parseStyles(build.projectId, build.styles),
styleSources: StyleSources.parse(JSON.parse(build.styleSources)),
};
};
diff --git a/packages/project/src/db/project.ts b/packages/project/src/db/project.ts
index 335d8be741..5eca0243e0 100644
--- a/packages/project/src/db/project.ts
+++ b/packages/project/src/db/project.ts
@@ -1,6 +1,7 @@
import slugify from "slugify";
import { customAlphabet } from "nanoid";
import { prisma, Prisma } from "@webstudio-is/prisma-client";
+import { cloneAssets } from "@webstudio-is/asset-uploader/server";
import * as db from "./index";
import {
authorizeProject,
@@ -204,13 +205,16 @@ const clone = async (
? await db.build.loadByProjectId(project.id, "dev")
: await db.build.loadByProjectId(project.id, "prod");
- const projectId = uuid();
- authorizeProject.registerProjectOwner({ projectId }, context);
+ const newProjectId = uuid();
+ await authorizeProject.registerProjectOwner(
+ { projectId: newProjectId },
+ context
+ );
const clonedProject = await prisma.$transaction(async (client) => {
const clonedProject = await client.project.create({
data: {
- id: projectId,
+ id: newProjectId,
userId: userId,
title: title ?? project.title,
domain: generateDomain(project.title),
@@ -218,11 +222,23 @@ const clone = async (
});
await db.build.create(
- { projectId: clonedProject.id, env: "dev", sourceBuild: build },
+ { projectId: newProjectId, env: "dev", sourceBuild: build },
context,
client
);
+ await cloneAssets(
+ {
+ fromProjectId: project.id,
+ toProjectId: newProjectId,
+
+ // Permission check on newProjectId will fail until this transaction is committed.
+ // We have to skip it, but it's ok because registerProjectOwner is right above
+ dontCheckEditPermission: true,
+ },
+ context
+ );
+
return clonedProject;
});
diff --git a/packages/project/src/db/styles.ts b/packages/project/src/db/styles.ts
index 1c350377fd..36e45a0c2e 100644
--- a/packages/project/src/db/styles.ts
+++ b/packages/project/src/db/styles.ts
@@ -38,7 +38,10 @@ const parseValue = (
return styleValue;
};
-export const parseStyles = async (stylesString: string) => {
+export const parseStyles = async (
+ projectId: Asset["projectId"],
+ stylesString: string
+) => {
const storedStyles = StoredStyles.parse(JSON.parse(stylesString));
const assetIds: string[] = [];
@@ -55,9 +58,8 @@ export const parseStyles = async (stylesString: string) => {
// Load all assets
const assets = await prisma.asset.findMany({
where: {
- id: {
- in: assetIds,
- },
+ id: { in: assetIds },
+ projectId,
},
});
const assetsMap = new Map<string, Asset>();
@@ -130,7 +132,7 @@ export const patch = async (
}
// these styles are filtered by treeId
- const styles = await parseStyles(build.styles);
+ const styles = await parseStyles(build.projectId, build.styles);
const patchedStyles = Styles.parse(applyPatches(styles, patches));