-
Notifications
You must be signed in to change notification settings - Fork 191
feat: display local pdf files #85
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
Changes from all commits
45d66af
021bd3f
56ed2f8
ec9a97f
8a2585c
df77dd5
e29cfba
b4b049c
2cab2d8
9f9a7ea
d54fe0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "create-llama": patch | ||
| --- | ||
|
|
||
| Display PDF files in source nodes |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| from app.api.routers.chat import chat_router | ||
| from app.settings import init_settings | ||
| from app.observability import init_observability | ||
| from fastapi.staticfiles import StaticFiles | ||
|
|
||
|
|
||
| app = FastAPI() | ||
|
|
@@ -20,7 +21,6 @@ | |
|
|
||
| environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set | ||
|
|
||
|
|
||
| if environment == "dev": | ||
| logger = logging.getLogger("uvicorn") | ||
| logger.warning("Running in development mode - allowing CORS for all origins") | ||
|
|
@@ -38,6 +38,8 @@ async def redirect_to_docs(): | |
| return RedirectResponse(url="/docs") | ||
|
|
||
|
|
||
| if os.path.exists("data"): | ||
| app.mount("/api/data", StaticFiles(directory="data"), name="static") | ||
|
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The
Analysis chainEnsure the Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify the existence and permissions of the `data` directory.
# Test: Check if the `data` directory exists and has read permissions.
if [ -d "data" ] && [ -r "data" ]; then
echo "The 'data' directory exists and is readable."
else
echo "The 'data' directory is missing or not readable."
exit 1
fi
Length of output: 139 |
||
| app.include_router(chat_router, prefix="/api/chat") | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { readFile } from "fs/promises"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import path from "path"; | ||
|
|
||
| /** | ||
| * This API is to get file data from ./data folder | ||
| * It receives path slug and response file data like serve static file | ||
| */ | ||
| export async function GET( | ||
| _request: NextRequest, | ||
| { params }: { params: { path: string } }, | ||
| ) { | ||
| const slug = params.path; | ||
|
|
||
| if (!slug) { | ||
| return NextResponse.json({ detail: "Missing file slug" }, { status: 400 }); | ||
| } | ||
|
|
||
| if (slug.includes("..") || path.isAbsolute(slug)) { | ||
| return NextResponse.json({ detail: "Invalid file path" }, { status: 400 }); | ||
| } | ||
|
|
||
| try { | ||
| const filePath = path.join(process.cwd(), "data", slug); | ||
| const blob = await readFile(filePath); | ||
|
|
||
| return new NextResponse(blob, { | ||
| status: 200, | ||
| statusText: "OK", | ||
| headers: { | ||
| "Content-Length": blob.byteLength.toString(), | ||
| }, | ||
| }); | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| console.error(error); | ||
| return NextResponse.json({ detail: "File not found" }, { status: 404 }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { PDFViewer, PdfFocusProvider } from "@llamaindex/pdf-viewer"; | ||
| import { Button } from "../../button"; | ||
| import { | ||
| Drawer, | ||
| DrawerClose, | ||
| DrawerContent, | ||
| DrawerDescription, | ||
| DrawerHeader, | ||
| DrawerTitle, | ||
| DrawerTrigger, | ||
| } from "../../drawer"; | ||
|
|
||
| export interface PdfDialogProps { | ||
| documentId: string; | ||
| path: string; | ||
| url: string; | ||
| trigger: React.ReactNode; | ||
| } | ||
|
|
||
| export default function PdfDialog(props: PdfDialogProps) { | ||
| return ( | ||
| <Drawer direction="left"> | ||
| <DrawerTrigger>{props.trigger}</DrawerTrigger> | ||
| <DrawerContent className="w-3/5 mt-24 h-full max-h-[96%] "> | ||
| <DrawerHeader className="flex justify-between"> | ||
| <div className="space-y-2"> | ||
| <DrawerTitle>PDF Content</DrawerTitle> | ||
| <DrawerDescription> | ||
| File path:{" "} | ||
| <a | ||
| className="hover:text-blue-900" | ||
| href={props.url} | ||
| target="_blank" | ||
| > | ||
| {props.path} | ||
| </a> | ||
| </DrawerDescription> | ||
| </div> | ||
| <DrawerClose asChild> | ||
| <Button variant="outline">Close</Button> | ||
| </DrawerClose> | ||
| </DrawerHeader> | ||
| <div className="m-4"> | ||
| <PdfFocusProvider> | ||
| <PDFViewer | ||
| file={{ | ||
| id: props.documentId, | ||
| url: props.url, | ||
| }} | ||
| /> | ||
| </PdfFocusProvider> | ||
| </div> | ||
| </DrawerContent> | ||
| </Drawer> | ||
| ); | ||
| } |
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.
Move the import statement to the top of the file to follow best practices.
Committable suggestion