-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fix optimizer and API for user study (#196)
* chore: fix optimizer and API for user study * chore: fix optimizer and API for user study
- Loading branch information
1 parent
4328a8e
commit e75a9ed
Showing
11 changed files
with
151 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,42 @@ | ||
// app/api/documents/[...path]/route.ts | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { readFile } from "fs/promises"; | ||
import path from "path"; | ||
import { lookup } from "mime-types"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: { path: string[] } } | ||
) { | ||
try { | ||
// Join the path segments and decode any URL encoding | ||
const filePath = decodeURIComponent(params.path.join("/")); | ||
|
||
// Basic security check to prevent directory traversal | ||
const normalizedPath = path.normalize(filePath); | ||
if (normalizedPath.includes("..")) { | ||
return NextResponse.json({ error: "Invalid file path" }, { status: 400 }); | ||
} | ||
|
||
const fileBuffer = await readFile(normalizedPath); | ||
const mimeType = lookup(normalizedPath) || "application/octet-stream"; | ||
|
||
return new NextResponse(fileBuffer, { | ||
headers: { | ||
"Content-Type": mimeType, | ||
"Content-Disposition": `inline; filename="${path.basename( | ||
normalizedPath | ||
)}"`, | ||
"Cache-Control": "public, max-age=3600", | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error("Error serving file:", error); | ||
return NextResponse.json( | ||
{ error: "Failed to serve file" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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
Oops, something went wrong.