generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 371
🐛 fix(duplicate-ja-doc-bug): centralize filename sanitization logic #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tbrand
merged 1 commit into
aws-samples:main
from
ShotaroKataoka:fix/duplicate-ja-doc-bug
Oct 16, 2025
+85
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import crypto from 'crypto'; | ||
|
|
||
| /** | ||
| * Convert filename to safe format for AWS Bedrock API | ||
| * AWS Bedrock DocumentBlock.name only allows: alphanumeric, whitespace, hyphens, parentheses, square brackets | ||
| * Replaces non-allowed characters with '_' and adds hash suffix only when replacements occur | ||
| * @param filename Original filename | ||
| * @returns Safe filename with hash suffix (only if non-allowed characters were replaced) | ||
| */ | ||
| export const convertToSafeFilename = (filename: string): string => { | ||
| const lastDotIndex = filename.lastIndexOf('.'); | ||
| const nameWithoutExt = | ||
| lastDotIndex > 0 ? filename.substring(0, lastDotIndex) : filename; | ||
| const safeName = nameWithoutExt.replace(/[^a-zA-Z0-9\s\-()[\]]/g, '_'); | ||
|
|
||
| // Add hash only if non-ASCII characters were replaced | ||
| if (safeName !== nameWithoutExt) { | ||
| const hash = crypto | ||
| .createHash('md5') | ||
| .update(filename) | ||
| .digest('hex') | ||
| .substring(0, 8); | ||
| return `${safeName}_${hash}`; | ||
| } | ||
|
|
||
| return safeName; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* eslint-disable i18nhelper/no-jp-string */ | ||
| import { convertToSafeFilename } from '../../../lambda/utils/fileNameUtils'; | ||
|
|
||
| describe('convertToSafeFilename', () => { | ||
| it('should return filename without hash when only ASCII characters', () => { | ||
| const result = convertToSafeFilename('document.pdf'); | ||
| expect(result).toBe('document'); | ||
| }); | ||
|
|
||
| it('should return filename without hash for ASCII with allowed special chars', () => { | ||
| const result = convertToSafeFilename('report-2024 (final)[v1].pdf'); | ||
| expect(result).toBe('report-2024 (final)[v1]'); | ||
| }); | ||
|
|
||
| it('should add hash when Japanese characters are present', () => { | ||
| const result = convertToSafeFilename('資料.pdf'); | ||
| expect(result).toBe('___46a890b2'); | ||
| }); | ||
|
|
||
| it('should add hash when mixed Japanese and ASCII characters', () => { | ||
| const result = convertToSafeFilename('report資料2024.pdf'); | ||
| expect(result).toBe('report__2024_f3805637'); | ||
| }); | ||
|
|
||
| it('should generate different hashes for different Japanese filenames with same length', () => { | ||
| const result1 = convertToSafeFilename('資料.pdf'); | ||
| const result2 = convertToSafeFilename('書類.pdf'); | ||
| expect(result1).toBe('___46a890b2'); | ||
| expect(result2).toBe('___5c4aa342'); | ||
| expect(result1).not.toBe(result2); | ||
| }); | ||
|
|
||
| it('should generate consistent hash for same filename', () => { | ||
| const result1 = convertToSafeFilename('資料.pdf'); | ||
| const result2 = convertToSafeFilename('資料.pdf'); | ||
| expect(result1).toBe('___46a890b2'); | ||
| expect(result2).toBe('___46a890b2'); | ||
| }); | ||
|
|
||
| it('should handle filename without extension', () => { | ||
| const result = convertToSafeFilename('document'); | ||
| expect(result).toBe('document'); | ||
| }); | ||
|
|
||
| it('should handle filename with multiple dots', () => { | ||
| const result = convertToSafeFilename('report.final.pdf'); | ||
| expect(result).toBe('report_final_8d101382'); | ||
| }); | ||
|
|
||
| it('should replace special characters with underscore and add hash', () => { | ||
| const result = convertToSafeFilename('file@#$.pdf'); | ||
| expect(result).toBe('file____cf25ced4'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test