-
Notifications
You must be signed in to change notification settings - Fork 0
feat(pyqs): add diagram attachments with AI context support #64
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
app/api/content/attachments/[branch]/[semester]/[subject]/[...path]/route.ts
This file contains hidden or 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,73 @@ | ||
| import fs from "fs/promises"; | ||
| import path from "path"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { resolveAttachmentFilesystemPath } from "@/lib/content/attachment-path"; | ||
|
|
||
| const MIME_TYPES: Record<string, string> = { | ||
| ".webp": "image/webp", | ||
| ".png": "image/png", | ||
| ".jpg": "image/jpeg", | ||
| ".jpeg": "image/jpeg", | ||
| ".gif": "image/gif", | ||
| ".svg": "image/svg+xml", | ||
| ".pdf": "application/pdf", | ||
| }; | ||
|
|
||
| interface RouteContext { | ||
| params: Promise<{ | ||
| branch: string; | ||
| semester: string; | ||
| subject: string; | ||
| path: string[]; | ||
| }>; | ||
| } | ||
|
|
||
| export async function GET(_request: NextRequest, context: RouteContext) { | ||
| const { | ||
| branch, | ||
| semester, | ||
| subject, | ||
| path: pathSegments, | ||
| } = await context.params; | ||
| const attachmentPath = pathSegments.map(decodeURIComponent).join("/"); | ||
|
|
||
| const filePath = resolveAttachmentFilesystemPath({ | ||
| branch, | ||
| semester, | ||
| subject, | ||
| attachmentPath, | ||
| }); | ||
|
|
||
| if (!filePath) { | ||
| console.warn( | ||
| `[attachments] Invalid attachment path requested: ${branch}/${semester}/${subject}/${attachmentPath}` | ||
| ); | ||
| return NextResponse.json( | ||
| { error: "Invalid attachment path" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| const fileBuffer = await fs.readFile(filePath); | ||
| const extension = path.extname(filePath).toLowerCase(); | ||
| const contentType = MIME_TYPES[extension] ?? "application/octet-stream"; | ||
|
|
||
| return new NextResponse(fileBuffer, { | ||
| status: 200, | ||
| headers: { | ||
| "Content-Type": contentType, | ||
| "Cache-Control": "public, max-age=31536000, immutable", | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.warn( | ||
| `[attachments] File not found: ${branch}/${semester}/${subject}/${attachmentPath}`, | ||
| error | ||
| ); | ||
| return NextResponse.json( | ||
| { error: "Attachment not found" }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| "use client"; | ||
|
|
||
| import { memo, useCallback, useState } from "react"; | ||
| import Image from "next/image"; | ||
| import { ImageOff } from "lucide-react"; | ||
|
|
||
| interface AttachmentImageProps { | ||
| src: string; | ||
| alt: string; | ||
| title?: string; | ||
| caption?: string; | ||
| invalidPath?: boolean; | ||
| } | ||
|
|
||
| function DiagramUnavailableCard() { | ||
| return ( | ||
| <div | ||
| className="flex items-center gap-3 rounded-xl border border-border bg-card p-5 shadow-sm" | ||
| role="img" | ||
| aria-label="Diagram unavailable" | ||
| > | ||
| <div | ||
| aria-hidden="true" | ||
| className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-muted" | ||
| > | ||
| <ImageOff className="h-5 w-5 text-muted-foreground" /> | ||
| </div> | ||
| <p className="text-sm font-medium text-muted-foreground"> | ||
| Diagram unavailable | ||
| </p> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function AttachmentImageComponent({ | ||
| src, | ||
| alt, | ||
| title, | ||
| caption, | ||
| invalidPath = false, | ||
| }: AttachmentImageProps) { | ||
| const [hasLoadError, setHasLoadError] = useState(invalidPath); | ||
|
|
||
| const handleError = useCallback(() => { | ||
| console.warn(`[attachments] Failed to load image: ${src}`); | ||
| setHasLoadError(true); | ||
| }, [src]); | ||
|
|
||
| if (!src || hasLoadError) { | ||
| return <DiagramUnavailableCard />; | ||
| } | ||
|
|
||
| return ( | ||
| <figure className="w-full max-w-2xl"> | ||
| {title ? ( | ||
| <figcaption className="mb-2 text-sm font-semibold text-foreground"> | ||
| {title} | ||
| </figcaption> | ||
| ) : null} | ||
|
|
||
| <div | ||
| aria-hidden="true" | ||
| className="overflow-hidden rounded-xl border border-border bg-white p-3 shadow-sm" | ||
| > | ||
| <div className="relative mx-auto w-full"> | ||
| <Image | ||
| src={src} | ||
| alt={alt} | ||
| width={960} | ||
| height={720} | ||
| loading="lazy" | ||
| sizes="(max-width: 640px) 100vw, (max-width: 1024px) 80vw, 672px" | ||
| className="mx-auto h-auto w-full max-w-full object-contain" | ||
| onError={handleError} | ||
| /> | ||
| </div> | ||
| </div> | ||
|
imuniqueshiv marked this conversation as resolved.
|
||
|
|
||
| {caption ? ( | ||
| <figcaption className="mt-2 text-xs text-muted-foreground sm:text-sm"> | ||
| {caption} | ||
| </figcaption> | ||
| ) : null} | ||
| </figure> | ||
| ); | ||
| } | ||
|
|
||
| const AttachmentImage = memo(AttachmentImageComponent); | ||
| AttachmentImage.displayName = "AttachmentImage"; | ||
|
|
||
| export default AttachmentImage; | ||
This file contains hidden or 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,130 @@ | ||
| import AttachmentImage from "@/components/pyqs/attachment-image"; | ||
| import { | ||
| attachmentFileExists, | ||
| getAttachmentPath, | ||
| } from "@/lib/content/attachment-path"; | ||
| import { | ||
| ATTACHMENT_TYPE_IMAGE, | ||
| getAttachmentAlt, | ||
| getAttachmentCaption, | ||
| getAttachmentTitle, | ||
| isQuestionAttachment, | ||
| isSupportedAttachmentType, | ||
| } from "@/lib/content/attachment-utils"; | ||
| import type { QuestionAttachment } from "@/types/pyq"; | ||
|
|
||
| interface QuestionAttachmentsProps { | ||
| attachments?: QuestionAttachment[]; | ||
| branch: string; | ||
| semester: string; | ||
| subject: string; | ||
| } | ||
|
|
||
| function warnInDevelopment(message: string) { | ||
| if (process.env.NODE_ENV === "development") { | ||
| console.warn(message); | ||
| } | ||
| } | ||
|
|
||
| async function renderAttachment( | ||
| attachment: QuestionAttachment, | ||
| branch: string, | ||
| semester: string, | ||
| subject: string | ||
| ) { | ||
| if (!isQuestionAttachment(attachment)) { | ||
| warnInDevelopment( | ||
| `[attachments] Skipping malformed attachment in ${subject}: ${JSON.stringify(attachment)}` | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| if (!isSupportedAttachmentType(attachment.type)) { | ||
| warnInDevelopment( | ||
| `[attachments] Unsupported attachment type "${attachment.type}" for ${attachment.id}` | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const pathInput = { | ||
| branch, | ||
| semester, | ||
| subject, | ||
| attachmentPath: attachment.path, | ||
| }; | ||
|
|
||
| const src = getAttachmentPath(pathInput); | ||
|
|
||
| if (!src) { | ||
| console.warn( | ||
| `[attachments] Invalid attachment path for ${attachment.id}: ${attachment.path}` | ||
| ); | ||
|
|
||
| return ( | ||
| <AttachmentImage | ||
| key={attachment.id} | ||
| src="" | ||
| alt={getAttachmentAlt(attachment)} | ||
| title={getAttachmentTitle(attachment)} | ||
| caption={getAttachmentCaption(attachment)} | ||
| invalidPath | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const fileExists = await attachmentFileExists(pathInput); | ||
|
|
||
| if (!fileExists) { | ||
| console.warn( | ||
| `[attachments] Attachment file missing for ${attachment.id}: ${attachment.path}` | ||
| ); | ||
| } | ||
|
|
||
| switch (attachment.type) { | ||
| case ATTACHMENT_TYPE_IMAGE: | ||
| return ( | ||
| <AttachmentImage | ||
| key={attachment.id} | ||
| src={src} | ||
| alt={getAttachmentAlt(attachment)} | ||
| title={getAttachmentTitle(attachment)} | ||
| caption={getAttachmentCaption(attachment)} | ||
| invalidPath={!fileExists} | ||
| /> | ||
| ); | ||
| default: | ||
| warnInDevelopment( | ||
| `[attachments] No renderer for attachment type "${attachment.type}"` | ||
| ); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export default async function QuestionAttachments({ | ||
| attachments, | ||
| branch, | ||
| semester, | ||
| subject, | ||
| }: QuestionAttachmentsProps) { | ||
| if (!attachments?.length) { | ||
| return null; | ||
| } | ||
|
|
||
| const renderedAttachments = ( | ||
| await Promise.all( | ||
| attachments.map((attachment) => | ||
| renderAttachment(attachment, branch, semester, subject) | ||
| ) | ||
| ) | ||
| ).filter(Boolean); | ||
|
|
||
| if (!renderedAttachments.length) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="space-y-4" aria-label="Question attachments"> | ||
| {renderedAttachments} | ||
| </div> | ||
| ); | ||
| } |
Binary file added
BIN
+46.5 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2023/Q.1-a-dec-2023.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.9 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2023/Q.1-b-dec-2023.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.3 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2024/Q.1-b-dec-2024.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+18.4 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2024/Q.2-a-dec-2024.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.73 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2025/Q.1-b-dec-2025.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+29.5 KB
content/rgpv/common/semester-2/bt-104/diagrams/december-2025/Q.6-a-dec-2025.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+46.1 KB
content/rgpv/common/semester-2/bt-104/diagrams/june-2023/Q.1-a-june-2023.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+39.1 KB
content/rgpv/common/semester-2/bt-104/diagrams/june-2023/Q.1-b-june-2023.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+43.6 KB
content/rgpv/common/semester-2/bt-104/diagrams/june-2024/Q.2-a-june-2024.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.22 KB
content/rgpv/common/semester-2/bt-104/diagrams/june-2025/Q.1-b-june-2025.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.6 KB
content/rgpv/common/semester-2/bt-104/diagrams/june-2025/Q.2-b-june-2025.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24 KB
content/rgpv/common/semester-2/bt-104/diagrams/november-2022/Q.1-a-dec-2022.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+34.3 KB
content/rgpv/common/semester-2/bt-104/diagrams/november-2022/Q.1-b-dec-2022.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.