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.

remove file from aws

Sara Vieira066c83d83632
diff --git a/apps/designer/app/routes/designer/$id.tsx b/apps/designer/app/routes/designer/$id.tsx
index a765f6e8eb..6a3c60aad9 100644
--- a/apps/designer/app/routes/designer/$id.tsx
+++ b/apps/designer/app/routes/designer/$id.tsx
@@ -42,7 +42,7 @@ export const action: ActionFunction = async ({ request, params }) => {
const formData = await request.formData();
if (formData.get("assetId")) {
const id = formData.get("assetId") as string;
- const deletedAsset = await deleteAsset(id);
+ const deletedAsset = await deleteAsset({ id });
return { deletedAsset };
}
diff --git a/packages/asset-uploader/src/db/delete.ts b/packages/asset-uploader/src/db/delete.ts
index ae237a4edc..9898fbe420 100644
--- a/packages/asset-uploader/src/db/delete.ts
+++ b/packages/asset-uploader/src/db/delete.ts
@@ -1,7 +1,7 @@
import { prisma, Asset } from "@webstudio-is/prisma-client";
import { getAssetPath } from "../helpers/get-asset-path";
-export const deleteAsset = async (assetId: Asset["id"]): Promise<Asset> => {
+export const deleteAssetInDb = async (assetId: Asset["id"]): Promise<Asset> => {
if (typeof assetId !== "string") {
throw new Error("Asset ID required");
}
diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.ts
new file mode 100644
index 0000000000..72deede99c
--- /dev/null
+++ b/packages/asset-uploader/src/delete.ts
@@ -0,0 +1,25 @@
+import { DeleteObjectCommand } from "@aws-sdk/client-s3";
+import { prisma, Asset } from "@webstudio-is/prisma-client";
+import { deleteAssetInDb } from "./db";
+import { s3EnvVariables } from "./schema";
+import { s3Client } from "./targets/s3/client";
+
+export const deleteAsset = async ({ id }: { id: string }): Promise<Asset> => {
+ const s3Envs = s3EnvVariables.parse(process.env);
+ const currentAsset = await prisma?.asset.findUnique({
+ where: { id },
+ });
+ if (currentAsset && currentAsset.name) {
+ if (currentAsset.location === "REMOTE") {
+ await s3Client.send(
+ new DeleteObjectCommand({
+ Bucket: s3Envs.S3_BUCKET,
+ Key: currentAsset?.name,
+ })
+ );
+
+ return await deleteAssetInDb(id);
+ }
+ }
+ throw new Error("Asset does not exist");
+};
diff --git a/packages/asset-uploader/src/index.ts b/packages/asset-uploader/src/index.ts
index 22321d4d77..25948908ae 100644
--- a/packages/asset-uploader/src/index.ts
+++ b/packages/asset-uploader/src/index.ts
@@ -1,2 +1,3 @@
export * from "./upload";
+export * from "./delete";
export * from "./db";
diff --git a/packages/asset-uploader/src/targets/s3/client.ts b/packages/asset-uploader/src/targets/s3/client.ts
new file mode 100644
index 0000000000..db35152445
--- /dev/null
+++ b/packages/asset-uploader/src/targets/s3/client.ts
@@ -0,0 +1,12 @@
+import { S3Client } from "@aws-sdk/client-s3";
+import { s3EnvVariables } from "../../schema";
+
+const s3Envs = s3EnvVariables.parse(process.env);
+export const s3Client = new S3Client({
+ endpoint: s3Envs.S3_ENDPOINT,
+ region: s3Envs.S3_REGION,
+ credentials: {
+ accessKeyId: s3Envs.S3_ACCESS_KEY_ID,
+ secretAccessKey: s3Envs.S3_SECRET_ACCESS_KEY,
+ },
+});
diff --git a/packages/asset-uploader/src/targets/s3/handler.ts b/packages/asset-uploader/src/targets/s3/handler.ts
index a2573ff9c1..624463fe9c 100644
--- a/packages/asset-uploader/src/targets/s3/handler.ts
+++ b/packages/asset-uploader/src/targets/s3/handler.ts
@@ -1,5 +1,5 @@
import { UploadHandlerPart } from "@remix-run/node";
-import { PutObjectCommandInput, S3Client } from "@aws-sdk/client-s3";
+import { PutObjectCommandInput } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import ObjectID from "bson-objectid";
@@ -14,6 +14,7 @@ import {
getFilenameAndExtension,
} from "../../helpers/array-buffer-helpers";
import { Location } from "@webstudio-is/prisma-client";
+import { s3Client } from "./client";
type S3UploadHandler = ({
file,
@@ -57,16 +58,7 @@ export const s3UploadHandler: S3UploadHandler = async ({
},
};
- const client = new S3Client({
- endpoint: s3Envs.S3_ENDPOINT,
- region: s3Envs.S3_REGION,
- credentials: {
- accessKeyId: s3Envs.S3_ACCESS_KEY_ID,
- secretAccessKey: s3Envs.S3_SECRET_ACCESS_KEY,
- },
- });
-
- const upload = new Upload({ client, params });
+ const upload = new Upload({ client: s3Client, params });
ImagesUploadedSuccess.parse(await upload.done());
const image = sharp(uint8Array);