Skip to content

Commit 8676455

Browse files
committed
fix: sanitize filename before upload to avoid invalid characters in uploaded filename
1 parent 0d470e3 commit 8676455

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export default class UploadPlugin extends AdminForthPlugin {
6060
)
6161
}
6262

63-
const filePath: string = this.options.filePath({ originalFilename, originalExtension, contentType, record });
63+
const sanitizeFileName = (name) => {
64+
return name
65+
.normalize("NFKD")
66+
.replace(/[^a-zA-Z0-9._-]/g, "_")
67+
}
68+
const fileName = sanitizeFileName(originalFilename);
69+
const filePath: string = this.options.filePath({ originalFilename: fileName, originalExtension, contentType, record });
6470
if (filePath.startsWith('/')) {
6571
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
6672
}

0 commit comments

Comments
 (0)