Skip to content

Commit

Permalink
Merge pull request #8 from dorik/fix/key-slugify
Browse files Browse the repository at this point in the history
fix: update slugify logic to remove special character (BE-997)
  • Loading branch information
getanwar authored Aug 13, 2024
2 parents 7826c65 + 9b4068c commit 26db55b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/many-candles-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@s3-presigner/server": patch
---

patch bump
15 changes: 14 additions & 1 deletion packages/server/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import path from "path";
import slugify from "slugify";
import { nanoid } from "nanoid";

const sanitizeSpecialChar = (fileKey: string) => {
const sanitizedString = fileKey.replace(/[^a-zA-Z0-9]/g, "");

if (!sanitizedString) {
return nanoid(10);
}
return sanitizedString;
};

type TgetS3PublicUrl = {
bucket: string;
region: string;
Expand All @@ -20,7 +29,11 @@ export const createUniqueFileKey = ({
}) => {
const nameParts = name.split(".");
const ext = nameParts.pop();
const fileKey = slugify(nameParts.join("."));

const sanitizedFileKey = sanitizeSpecialChar(nameParts.join("."));

const fileKey = slugify(sanitizedFileKey);

const key = path.join(prefix, `${fileKey}-${nanoid(5)}.${ext}`);
return key;
};

0 comments on commit 26db55b

Please sign in to comment.