Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/paper/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fetchPaperID } from "@/app/actions/get-papers-by-id";
import RelatedPapers from "@/components/RelatedPaper";
import Loader from "@/components/ui/loader";
import { type ErrorResponse, type PaperResponse } from "@/interface";
import { extractBracketContent } from "@/lib/utils/string";
import { courseCodeOrName } from "@/lib/utils/string";
import axios, { type AxiosResponse } from "axios";
import { type Metadata } from "next";
import { redirect } from "next/navigation";
Expand Down Expand Up @@ -177,7 +177,7 @@ const PaperPage = async ({ params }: { params: { id: string } }) => {

<PDFViewer
url={paper.file_url}
name={`${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}`}/>
name={`${courseCodeOrName(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}`}/>

{/* <PdfViewer
url={paper.file_url}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IPaper } from "../../interface";
import { extractBracketContent } from "./string";
import { courseCodeOrName } from "./string";
import axios from "axios";
import toast from "react-hot-toast";

Expand All @@ -8,7 +8,7 @@ export const getSecureUrl = (url: string): string =>

export const generateFileName = (paper: IPaper): string => {
const extension = paper.file_url.split(".").pop();
return `${extractBracketContent(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
return `${courseCodeOrName(paper.subject)}-${paper.exam}-${paper.slot}-${paper.year}.${extension}`;
};

export const downloadFile = async (
Expand Down
6 changes: 5 additions & 1 deletion src/lib/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export function toSentenceCase(input: string): string {
export function extractBracketContent(subject: string): string | null {
const regex = /\[(.*?)\]/;
const match = regex.exec(subject);
return match?.[1] ?? "BMAT102L";
return match?.[1] ?? null;
}

export function extractWithoutBracketContent(subject: string): string {
return subject.replace(/\s*\[.*?\]\s*/g, "").trim();
}

export function courseCodeOrName(subject: string): string {
return extractBracketContent(subject) ?? extractWithoutBracketContent(subject);
}