| 192 | } |
| 193 | |
| 194 | export function encodeFilenameForHeader(storageKey: string): string { |
| 195 | const filename = storageKey.split('/').pop() || storageKey |
| 196 | |
| 197 | const hasNonAscii = /[^\x00-\x7F]/.test(filename) |
| 198 | |
| 199 | if (!hasNonAscii) { |
| 200 | return `filename="${filename}"` |
| 201 | } |
| 202 | |
| 203 | const encodedFilename = encodeURIComponent(filename) |
| 204 | const asciiSafe = filename.replace(/[^\x00-\x7F]/g, '_') |
| 205 | return `filename="${asciiSafe}"; filename*=UTF-8''${encodedFilename}` |
| 206 | } |
| 207 | |
| 208 | export function createFileResponse(file: FileResponse): NextResponse { |
| 209 | const { contentType, disposition } = getSecureFileHeaders(file.filename, file.contentType) |