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 tests

Sara Vieirafc20b05da807
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
index 667bb3d680..483c0f413e 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
@@ -51,13 +51,7 @@ export const TabContent = ({
</Flex>
<Grid columns={2} gap={2}>
{assets.map((asset) => (
- <AssetManagerImage
- key={asset.id}
- path={asset.path}
- alt={asset.alt || asset.name}
- status={asset.status}
- id={asset.id}
- />
+ <AssetManagerImage key={asset.id} {...asset} />
))}
</Grid>
</Flex>
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx b/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
index 159fd648b7..de7754426d 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
@@ -5,6 +5,7 @@ import placeholderImage from "~/shared/images/image-placeholder.svg";
import brokenImage from "~/shared/images/broken-image-placeholder.svg";
import { Asset } from "@webstudio-is/prisma-client";
import { useSubmit } from "@remix-run/react";
+import { UploadingAsset } from "../../types";
const useImageWithFallback = ({ path }: { path: string }) => {
const [src, setSrc] = useState(placeholderImage);
@@ -23,13 +24,9 @@ export const AssetManagerImage = ({
path,
alt,
status,
+ name,
id,
-}: {
- id: string;
- path: string;
- alt?: string;
- status?: Asset["status"];
-}) => {
+}: Asset | UploadingAsset) => {
const submit = useSubmit();
const isUploading = status === "uploading";
const src = useImageWithFallback({ path });
@@ -48,6 +45,7 @@ export const AssetManagerImage = ({
const deleteAsset = () => {
const formData = new FormData();
formData.append("assetId", id);
+ formData.append("assetName", name);
submit(formData, { method: "delete" });
};
return (
diff --git a/apps/designer/app/routes/designer/$id.tsx b/apps/designer/app/routes/designer/$id.tsx
index 6a3c60aad9..4f8b4cf215 100644
--- a/apps/designer/app/routes/designer/$id.tsx
+++ b/apps/designer/app/routes/designer/$id.tsx
@@ -42,7 +42,8 @@ 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 name = formData.get("assetName") as string;
+ const deletedAsset = await deleteAsset({ id, name, dirname: __dirname });
return { deletedAsset };
}
diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.ts
index 72deede99c..57ed93130f 100644
--- a/packages/asset-uploader/src/delete.ts
+++ b/packages/asset-uploader/src/delete.ts
@@ -1,23 +1,39 @@
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
import { prisma, Asset } from "@webstudio-is/prisma-client";
+import { unlink } from "fs/promises";
+import path from "path";
import { deleteAssetInDb } from "./db";
+import { getImageLocalDirectory } from "./helpers/get-image-local-path";
import { s3EnvVariables } from "./schema";
-import { s3Client } from "./targets/s3/client";
+import { getS3Client } from "./targets/s3/client";
-export const deleteAsset = async ({ id }: { id: string }): Promise<Asset> => {
- const s3Envs = s3EnvVariables.parse(process.env);
+export const deleteAsset = async ({
+ id,
+ name,
+ dirname,
+}: {
+ id: string;
+ dirname: string;
+ name: string;
+}): Promise<Asset> => {
const currentAsset = await prisma?.asset.findUnique({
where: { id },
});
if (currentAsset && currentAsset.name) {
if (currentAsset.location === "REMOTE") {
- await s3Client.send(
+ const s3Envs = s3EnvVariables.parse(process.env);
+ await getS3Client().send(
new DeleteObjectCommand({
Bucket: s3Envs.S3_BUCKET,
- Key: currentAsset?.name,
+ Key: name,
})
);
+ return await deleteAssetInDb(id);
+ } else {
+ const directory = await getImageLocalDirectory(dirname);
+ await unlink(path.join(directory, name));
+
return await deleteAssetInDb(id);
}
}
diff --git a/packages/asset-uploader/src/helpers/get-image-local-path.test.ts b/packages/asset-uploader/src/helpers/get-image-local-path.test.ts
new file mode 100644
index 0000000000..b1c712a6e1
--- /dev/null
+++ b/packages/asset-uploader/src/helpers/get-image-local-path.test.ts
@@ -0,0 +1,39 @@
+/* eslint-disable @typescript-eslint/no-var-requires */
+import { Decimal } from "@prisma/client/runtime";
+import { Location } from "@webstudio-is/prisma-client";
+import { getImageLocalDirectory } from "./get-image-local-path";
+
+const commonAsset = {
+ id: "sa-546",
+ width: new Decimal(200),
+ height: new Decimal(200),
+ projectId: "id",
+ size: 2135,
+ format: "png",
+ createdAt: new Date(),
+ alt: "",
+};
+
+describe("getAssetPath", () => {
+ const OLD_ENV = process.env;
+
+ beforeEach(() => {
+ jest.resetModules();
+ process.env = OLD_ENV;
+ });
+
+ afterAll(() => {
+ process.env = OLD_ENV;
+ });
+
+ describe("getImageLocalDirectory", () => {
+ test("creates path", () => {
+ expect(getImageLocalDirectory("uploads")).toBe("public/uploads");
+ });
+ test("changes path with env", () => {
+ process.env.FILE_UPLOAD_PATH = "test";
+ const { getImageLocalDirectory } = require("./get-image-local-path");
+ expect(getImageLocalDirectory("uploads")).toBe("public/test");
+ });
+ });
+});
diff --git a/packages/asset-uploader/src/helpers/get-image-local-path.ts b/packages/asset-uploader/src/helpers/get-image-local-path.ts
new file mode 100644
index 0000000000..71d68909f0
--- /dev/null
+++ b/packages/asset-uploader/src/helpers/get-image-local-path.ts
@@ -0,0 +1,10 @@
+import path from "path";
+import { fsEnvVariables } from "../schema";
+const fsUploadVars = fsEnvVariables.parse(process.env);
+
+export const getImageLocalDirectory = (dirname: string) => {
+ const uploads = path.join(dirname, "../public");
+ const directory = path.join(uploads, fsUploadVars.FILE_UPLOAD_PATH);
+
+ return directory;
+};
diff --git a/packages/asset-uploader/src/targets/s3/client.ts b/packages/asset-uploader/src/targets/s3/client.ts
index db35152445..0532597bcb 100644
--- a/packages/asset-uploader/src/targets/s3/client.ts
+++ b/packages/asset-uploader/src/targets/s3/client.ts
@@ -1,12 +1,14 @@
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,
- },
-});
+export const getS3Client = () => {
+ const s3Envs = s3EnvVariables.parse(process.env);
+ return 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 624463fe9c..24440b0050 100644
--- a/packages/asset-uploader/src/targets/s3/handler.ts
+++ b/packages/asset-uploader/src/targets/s3/handler.ts
@@ -14,7 +14,7 @@ import {
getFilenameAndExtension,
} from "../../helpers/array-buffer-helpers";
import { Location } from "@webstudio-is/prisma-client";
-import { s3Client } from "./client";
+import { getS3Client } from "./client";
type S3UploadHandler = ({
file,
@@ -58,7 +58,7 @@ export const s3UploadHandler: S3UploadHandler = async ({
},
};
- const upload = new Upload({ client: s3Client, params });
+ const upload = new Upload({ client: getS3Client(), params });
ImagesUploadedSuccess.parse(await upload.done());
const image = sharp(uint8Array);
diff --git a/packages/asset-uploader/src/upload.ts b/packages/asset-uploader/src/upload.ts
index 42afd7c58a..1b57199ccd 100644
--- a/packages/asset-uploader/src/upload.ts
+++ b/packages/asset-uploader/src/upload.ts
@@ -1,4 +1,3 @@
-import path from "path";
import {
unstable_createFileUploadHandler,
unstable_parseMultipartFormData,
@@ -6,11 +5,12 @@ import {
import { s3UploadHandler } from "./targets/s3/handler";
import { uploadToS3 } from "./targets/s3/uploader";
import { uploadToDisk } from "./targets/disk/upload";
-import { assetEnvVariables, fsEnvVariables, s3EnvVariables } from "./schema";
+import { assetEnvVariables, s3EnvVariables } from "./schema";
import { Asset } from "@webstudio-is/prisma-client";
+import { getImageLocalDirectory } from "./helpers/get-image-local-path";
const isS3Upload = s3EnvVariables.safeParse(process.env).success;
-const fsUploadVars = fsEnvVariables.parse(process.env);
+
const commonUploadVars = assetEnvVariables.parse(process.env);
// user inputs the max value in mb and we transform it to bytes
@@ -25,8 +25,7 @@ export const uploadAssets = async ({
projectId: string;
dirname: string;
}): Promise<Asset[]> => {
- const uploads = path.join(dirname, "../public");
- const directory = path.join(uploads, fsUploadVars.FILE_UPLOAD_PATH);
+ const directory = await getImageLocalDirectory(dirname);
const formData = await unstable_parseMultipartFormData(
request,
isS3Upload