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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Mark files as deleted (#1638)
Bogdan Chadkin543934343776
diff --git a/apps/builder/app/routes/rest.patch.ts b/apps/builder/app/routes/rest.patch.tsindex c30cd781ce..c722868fff 100644--- a/apps/builder/app/routes/rest.patch.ts+++ b/apps/builder/app/routes/rest.patch.ts@@ -30,7 +30,6 @@ import {import { patchAssets } from "@webstudio-is/asset-uploader/index.server";import type { Project } from "@webstudio-is/project";import { createContext } from "~/shared/context.server";-import { createAssetClient } from "~/shared/asset-client";import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";import { applyPatches } from "immer";@@ -157,7 +156,7 @@ export const action = async ({ request }: ActionArgs) => {// @todo parallelize the updates// currently not possible because we fetch the entire tree// and parallelized updates will cause unpredictable side effects- await patchAssets({ projectId }, patches, context, createAssetClient());+ await patchAssets({ projectId }, patches, context);continue;}diff --git a/packages/asset-uploader/src/client.ts b/packages/asset-uploader/src/client.tsindex 67fa5a4dd3..035eb9b0b2 100644--- a/packages/asset-uploader/src/client.ts+++ b/packages/asset-uploader/src/client.ts@@ -6,5 +6,4 @@ export type AssetClient = {type: string,data: AsyncIterable<Uint8Array>) => Promise<AssetData>;- deleteFile: (name: string) => Promise<void>;};diff --git a/packages/asset-uploader/src/clients/fs/delete.ts b/packages/asset-uploader/src/clients/fs/delete.tsdeleted file mode 100644index 6241eed9a2..0000000000--- a/packages/asset-uploader/src/clients/fs/delete.ts+++ /dev/null@@ -1,23 +0,0 @@-import { unlink } from "fs/promises";-import path from "path";--// @todo we need a better place for this to reuse it-const isNodeError = (error: unknown): error is NodeJS.ErrnoException =>- error instanceof Error && "code" in error;--export const deleteFromFs = async ({- name,- fileDirectory,-}: {- name: string;- fileDirectory: string;-}) => {- try {- await unlink(path.join(fileDirectory, name));- } catch (error) {- // When file was deleted from local file system, we don't need to throw an error.- if (isNodeError(error) && error.code !== "ENOENT") {- throw error;- }- }-};diff --git a/packages/asset-uploader/src/clients/fs/fs.ts b/packages/asset-uploader/src/clients/fs/fs.tsindex e663b3864e..63ddccc78c 100644--- a/packages/asset-uploader/src/clients/fs/fs.ts+++ b/packages/asset-uploader/src/clients/fs/fs.ts@@ -1,5 +1,4 @@import type { AssetClient } from "../../client";-import { deleteFromFs } from "./delete";import { uploadToFs } from "./upload";type FsClientOptions = {@@ -17,7 +16,5 @@ export const createFsClient = (options: FsClientOptions): AssetClient => {maxSize: options.maxUploadSize,fileDirectory: options.fileDirectory,}),- deleteFile: (name) =>- deleteFromFs({ name, fileDirectory: options.fileDirectory }),};};diff --git a/packages/asset-uploader/src/clients/s3/s3.ts b/packages/asset-uploader/src/clients/s3/s3.tsindex 6790045850..127ca5de01 100644--- a/packages/asset-uploader/src/clients/s3/s3.ts+++ b/packages/asset-uploader/src/clients/s3/s3.ts@@ -1,4 +1,4 @@-import { DeleteObjectCommand, S3Client } from "@aws-sdk/client-s3";+import { S3Client } from "@aws-sdk/client-s3";import type { AssetClient } from "../../client";import { uploadToS3 } from "./upload";@@ -35,17 +35,7 @@ export const createS3Client = (options: S3ClientOptions): AssetClient => {});};- const deleteFile: AssetClient["deleteFile"] = async (name) => {- await client.send(- new DeleteObjectCommand({- Bucket: options.bucket,- Key: name,- })- );- };-return {uploadFile,- deleteFile,};};diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.tsindex 5dd7060fa6..dd4de8e872 100644--- a/packages/asset-uploader/src/delete.ts+++ b/packages/asset-uploader/src/delete.ts@@ -4,7 +4,6 @@ import {type AppContext,AuthorizationError,} from "@webstudio-is/trpc-interface/index.server";-import type { AssetClient } from "./client";import type { Asset } from "./schema";export const deleteAssets = async (@@ -12,8 +11,7 @@ export const deleteAssets = async (ids: Array<Asset["id"]>;projectId: Project["id"];},- context: AppContext,- client: AssetClient+ context: AppContext) => {const canDelete = await authorizeProject.hasProjectPermit({ projectId: props.projectId, permit: "edit" },@@ -55,10 +53,10 @@ export const deleteAssets = async (}// delete unused files- await prisma.file.deleteMany({+ await prisma.file.updateMany({where: { name: { in: Array.from(unusedFileNames) } },+ data: {+ isDeleted: true,+ },});- for (const name of unusedFileNames) {- await client.deleteFile(name);- }};diff --git a/packages/asset-uploader/src/patch.ts b/packages/asset-uploader/src/patch.tsindex c93f135655..2b4daed664 100644--- a/packages/asset-uploader/src/patch.ts+++ b/packages/asset-uploader/src/patch.ts@@ -7,7 +7,6 @@ import {import { type Asset, Assets } from "./schema";import { deleteAssets } from "./delete";import { loadAssetsByProject } from "./db/load";-import type { AssetClient } from "./client";/*** patchAssets can only delete or add assets@@ -16,8 +15,7 @@ import type { AssetClient } from "./client";export const patchAssets = async ({ projectId }: { projectId: Project["id"] },patches: Array<Patch>,- context: AppContext,- assetClient: AssetClient+ context: AppContext) => {const canEdit = await authorizeProject.hasProjectPermit({ projectId, permit: "edit" },@@ -42,7 +40,7 @@ export const patchAssets = async (}}if (deletedAssetIds.length !== 0) {- deleteAssets({ projectId, ids: deletedAssetIds }, context, assetClient);+ deleteAssets({ projectId, ids: deletedAssetIds }, context);}// add new assets found in patched version@@ -63,13 +61,21 @@ export const patchAssets = async (const fileNames = new Set(files.map((file) => file.name));+ // restore file when undo is triggered+ await prisma.file.updateMany({+ where: { name: { in: Array.from(fileNames) } },+ data: {+ isDeleted: false,+ },+ });+await prisma.asset.createMany({data: addedAssets// making sure corresponding file exist before creating an asset that references it.filter((asset) => fileNames.has(asset.name)).map((asset) => ({id: asset.id,- projectId: asset.projectId,+ projectId,name: asset.name,// @todo remove once legacy fields are removed from schemalocation: asset.location,diff --git a/packages/asset-uploader/src/upload.ts b/packages/asset-uploader/src/upload.tsindex a9658f8c47..74c2ecd807 100644--- a/packages/asset-uploader/src/upload.ts+++ b/packages/asset-uploader/src/upload.ts@@ -44,12 +44,14 @@ export const createUploadName = async (OR: [{status: "UPLOADED",+ isDeleted: false,uploaderProjectId: projectId,},{status: "UPLOADING",- createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },+ isDeleted: false,uploaderProjectId: projectId,+ createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },},],},diff --git a/packages/prisma-client/prisma/migrations/20230517133730_file_is_deleted/migration.sql b/packages/prisma-client/prisma/migrations/20230517133730_file_is_deleted/migration.sqlnew file mode 100644index 0000000000..7c9ebc3801--- /dev/null+++ b/packages/prisma-client/prisma/migrations/20230517133730_file_is_deleted/migration.sql@@ -0,0 +1,3 @@+-- AlterTable+ALTER TABLE "File" ADD COLUMN "isDeleted" BOOLEAN NOT NULL DEFAULT false,+ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prismaindex 1d2cba4f4c..6cbd3b8a2c 100644--- a/packages/prisma-client/prisma/schema.prisma+++ b/packages/prisma-client/prisma/schema.prisma@@ -36,8 +36,10 @@ model File {size Intdescription String?createdAt DateTime @default(now())+ updatedAt DateTime @default(now()) @updatedAtmeta String @default("{}")status UploadStatus @default(UPLOADING)+ isDeleted Boolean @default(false)uploaderProject Project? @relation(fields: [uploaderProjectId], references: [id])uploaderProjectId String?assets Asset[]