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.

Reuse sanitize

istarkovc75f3e4f1628
diff --git a/apps/designer/app/designer/shared/assets/sanitize-s3-key.ts b/apps/designer/app/designer/shared/assets/sanitize-s3-key.ts
deleted file mode 100644
index d5435fa28b..0000000000
--- a/apps/designer/app/designer/shared/assets/sanitize-s3-key.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-/**
- * TEMPORARY: This function is a temporary fix for https://github.com/remix-run/remix/issues/4443
- *
- * Based on this: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
- * Characters that might require special handling
- * ASCII character ranges 00–1F hex (0–31 decimal) and 7F (127 decimal)
- * "&","$", "@", "=", ";" "/", ":", "+", Significant sequences of spaces might be lost in some uses (especially multiple spaces)
- * ",", "?"
- * Characters to avoid
- * Non-printable ASCII characters (128–255 decimal characters)
- * "\", "{", "^", "}", "%", "`", "]", ', ", “, ”, ">", "[", "~", "<", "#", "|"
- **/
-
-// eslint-disable-next-line no-control-regex
-const specialRegexps = [/[\x00-\x1F\x7F]+/g, /[&$@=;/:+\s,?]+/g];
-
-// eslint-disable-next-line no-control-regex
-const avoidRegexps = [/[\x80-\xFF]+/g, /[\\{^}%`\]'"“”>[~<#|]+/g];
-
-const allRegexps = [...specialRegexps, ...avoidRegexps];
-
-const REPLACE_CHAR = "_";
-
-export const sanitizeS3Key = (str: string): string => {
- return allRegexps.reduce((r, reg) => r.replace(reg, REPLACE_CHAR), str);
-};
diff --git a/apps/designer/app/designer/shared/assets/use-assets.ts b/apps/designer/app/designer/shared/assets/use-assets.ts
index 933b63ddd7..3c7bf3ac82 100644
--- a/apps/designer/app/designer/shared/assets/use-assets.ts
+++ b/apps/designer/app/designer/shared/assets/use-assets.ts
@@ -10,7 +10,7 @@ import ObjectID from "bson-objectid";
import { useMemo } from "react";
import { restAssetsPath } from "~/shared/router-utils";
import { useAssets as useAssetsState, useProject } from "../nano-states";
-import { sanitizeS3Key } from "./sanitize-s3-key";
+import { sanitizeS3Key } from "@webstudio-is/asset-uploader";
import { PreviewAsset } from "./types";
const toPreviewAssets = (formData: FormData): Promise<PreviewAsset[]> => {
@@ -57,6 +57,7 @@ const toFormData = (type: AssetType, input: HTMLInputElement) => {
}
// sanitizeS3Key here is just because of https://github.com/remix-run/remix/issues/4443
+ // should be removed after fix
formData.append(type, file, sanitizeS3Key(file.name));
}
return formData;
diff --git a/packages/asset-uploader/src/index.ts b/packages/asset-uploader/src/index.ts
index 83cf8c43dd..5772be5362 100644
--- a/packages/asset-uploader/src/index.ts
+++ b/packages/asset-uploader/src/index.ts
@@ -2,3 +2,4 @@ export * from "./types";
export * from "./constants";
export * from "./utils/filter-by-type";
export * from "./utils/to-bytes";
+export * from "./utils/sanitize-s3-key";
diff --git a/packages/asset-uploader/src/utils/sanitize-s3-key.ts b/packages/asset-uploader/src/utils/sanitize-s3-key.ts
index 5102c3032d..68c330ae4f 100644
--- a/packages/asset-uploader/src/utils/sanitize-s3-key.ts
+++ b/packages/asset-uploader/src/utils/sanitize-s3-key.ts
@@ -19,6 +19,10 @@ const allRegexps = [...specialRegexps, ...avoidRegexps];
const REPLACE_CHAR = "_";
+/**
+ * Sanitize S3 key based on https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html
+ * plz do not use this function for other purposes
+ **/
export const sanitizeS3Key = (str: string): string => {
return allRegexps.reduce((r, reg) => r.replace(reg, REPLACE_CHAR), str);
};