fix: encode unicode filenames in Content-Disposition headers
Filenames with Romanian characters (Ș, Ț, etc.) caused ByteString errors. Also pass original filename through to extreme mode response. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -195,7 +195,7 @@ export async function POST(req: NextRequest) {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${upload.filename.replace(/\.pdf$/i, "-comprimat.pdf")}"`,
|
||||
"Content-Disposition": `attachment; filename="${encodeURIComponent(upload.filename.replace(/\.pdf$/i, "-comprimat.pdf"))}"`,
|
||||
"X-Original-Size": String(originalSize),
|
||||
"X-Compressed-Size": String(compressedSize),
|
||||
},
|
||||
|
||||
@@ -42,6 +42,7 @@ function streamFileResponse(
|
||||
filePath: string,
|
||||
originalSize: number,
|
||||
compressedSize: number,
|
||||
filename: string,
|
||||
): NextResponse {
|
||||
const nodeStream = createReadStream(filePath);
|
||||
const webStream = Readable.toWeb(nodeStream) as ReadableStream;
|
||||
@@ -51,7 +52,7 @@ function streamFileResponse(
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Length": String(compressedSize),
|
||||
"Content-Disposition": 'attachment; filename="optimized.pdf"',
|
||||
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
|
||||
"X-Original-Size": String(originalSize),
|
||||
"X-Compressed-Size": String(compressedSize),
|
||||
},
|
||||
@@ -128,13 +129,13 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
// Stream result from disk — if bigger, stream original
|
||||
if (compressedSize >= originalSize) {
|
||||
return streamFileResponse(inputPath, originalSize, originalSize);
|
||||
return streamFileResponse(inputPath, originalSize, originalSize, upload.filename);
|
||||
}
|
||||
|
||||
// NOTE: cleanup is deferred — we can't delete files while streaming.
|
||||
// The files will be cleaned up by the OS temp cleaner or on next request.
|
||||
// For immediate cleanup, we'd need to buffer, but that defeats the purpose.
|
||||
return streamFileResponse(outputPath, originalSize, compressedSize);
|
||||
return streamFileResponse(outputPath, originalSize, compressedSize, upload.filename);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Unknown error";
|
||||
console.error(`[compress-pdf] Error:`, message);
|
||||
|
||||
Reference in New Issue
Block a user