* Normalizes a user-provided file path by handling escape sequences.
(filePath: string)
| 71 | * Normalizes a user-provided file path by handling escape sequences. |
| 72 | */ |
| 73 | function normalizeUserProvidedPath(filePath: string): string { |
| 74 | let normalized = filePath |
| 75 | |
| 76 | // Handle unicode escape sequences (e.g., from terminal copy/paste) |
| 77 | normalized = normalized.replace(/\\u\{([0-9a-fA-F]+)\}|\\u([0-9a-fA-F]{4})/g, (_, bracedCode, shortCode) => { |
| 78 | const code = bracedCode || shortCode |
| 79 | const value = Number.parseInt(code, 16) |
| 80 | return Number.isNaN(value) ? _ : String.fromCodePoint(value) |
| 81 | }) |
| 82 | |
| 83 | // Handle shell-escaped special characters (e.g., spaces in paths) |
| 84 | normalized = normalized.replace(/\\([ \t"'(){}\[\]])/g, '$1') |
| 85 | |
| 86 | return normalized |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Validates if a file path is a supported image |