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.

Fix asset errors

istarkovfc4719cde2c8
diff --git a/packages/asset-uploader/src/targets/s3/upload.ts b/packages/asset-uploader/src/targets/s3/upload.ts
index d4e4c8032e..bfcbc1fb01 100644
--- a/packages/asset-uploader/src/targets/s3/upload.ts
+++ b/packages/asset-uploader/src/targets/s3/upload.ts
@@ -86,7 +86,8 @@ const uploadHandler = async ({
Body: data,
ContentType: file.contentType,
Metadata: {
- filename: file.filename || "unnamed",
+ // encodeURIComponent is needed to support special characters like Cyrillic
+ filename: encodeURIComponent(file.filename) || "unnamed",
},
};
diff --git a/packages/asset-uploader/src/utils/format-asset.ts b/packages/asset-uploader/src/utils/format-asset.ts
index fe9abf7df5..1c54e372b2 100644
--- a/packages/asset-uploader/src/utils/format-asset.ts
+++ b/packages/asset-uploader/src/utils/format-asset.ts
@@ -1,8 +1,8 @@
import { z } from "zod";
-import { Asset as DbAsset } from "@webstudio-is/prisma-client";
+import type { Asset as DbAsset } from "@webstudio-is/prisma-client";
import { type FontFormat, FONT_FORMATS } from "@webstudio-is/fonts";
import { FontMeta } from "@webstudio-is/fonts/server";
-import { Asset } from "../types";
+import type { Asset } from "../types";
import { getAssetPath } from "./get-asset-path";
const ImageMeta = z.object({
diff --git a/packages/asset-uploader/src/utils/get-asset-path.ts b/packages/asset-uploader/src/utils/get-asset-path.ts
index 80238d5c0a..ad6e835a4f 100644
--- a/packages/asset-uploader/src/utils/get-asset-path.ts
+++ b/packages/asset-uploader/src/utils/get-asset-path.ts
@@ -8,18 +8,33 @@ const fsEnv = FsEnv.parse(process.env);
export const getAssetPath = (asset: DbAsset) => {
if (asset.location === Location.FS) {
const splitPath = fsEnv.FILE_UPLOAD_PATH.split("public");
- return path.join("/", splitPath[splitPath.length - 1], asset.name);
+ const locUrl = new URL(
+ path.join(
+ "/",
+ splitPath[splitPath.length - 1],
+ encodeURIComponent(asset.name)
+ ),
+ // Hostname here is not important
+ "http://localhost"
+ );
+
+ return locUrl.pathname;
}
if (asset.location === Location.REMOTE && s3Envs.success) {
if (s3Envs.data.ASSET_CDN_URL) {
const cndUrl = new URL(s3Envs.data.ASSET_CDN_URL);
- cndUrl.pathname = encodeURIComponent(asset.name);
+ // We encode file name during upload,
+ // see /packages/asset-uploader/src/targets/s3/upload.ts "Key: encodeURIComponent(uniqueFilename)"
+ // here we need to encode file name, i.e cndUrl.pathname = encodeURIComponent(fileName)
+ // but having that fileName = encodeURIComponent(asset.name) we are doing double encoding
+ cndUrl.pathname = encodeURIComponent(encodeURIComponent(asset.name));
return cndUrl.toString();
}
const s3Url = new URL(s3Envs.data.S3_ENDPOINT);
s3Url.hostname = `${s3Envs.data.S3_BUCKET}.${s3Url.hostname}`;
- s3Url.pathname = encodeURIComponent(asset.name);
+ // See above comment about double encoding
+ s3Url.pathname = encodeURIComponent(encodeURIComponent(asset.name));
return s3Url.toString();
}