Skip to content

Commit 438f17e

Browse files
Merge pull request #311 from abhitrueprogrammer/prod
feat: download files in a zip
2 parents 5d3808c + 43b62c1 commit 438f17e

4 files changed

Lines changed: 103 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"google-auth-library": "^10.2.1",
4040
"googleapis": "^155.0.0",
4141
"jose": "^5.10.0",
42+
"jszip": "^3.10.1",
4243
"lodash": "^4.17.21",
4344
"lucide-react": "^0.395.0",
4445
"mongoose": "^8.13.2",

pnpm-lock.yaml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/CatalogueContent.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
downloadFile,
2727
} from "@/util/download_paper";
2828
import type { ICourses } from "@/interface";
29+
import JSZip from "jszip";
30+
import { toast } from "react-hot-toast";
2931

3032
const CatalogueContent = () => {
3133
const router = useRouter();
@@ -226,17 +228,36 @@ const CatalogueContent = () => {
226228
[],
227229
);
228230

229-
const handleDownloadAll = useCallback(async () => {
231+
const handleDownloadSelected = useCallback(async () => {
232+
const zip = new JSZip();
230233
const uniquePapers = Array.from(
231234
new Set(selectedPapers.map((paper) => paper._id)),
232235
).map((id) => selectedPapers.find((paper) => paper._id === id)) as IPaper[];
233-
236+
if(!uniquePapers){
237+
toast.error("No papers selected for download.");
238+
}
234239
for (const paper of uniquePapers) {
235-
await downloadFile(
236-
getSecureUrl(paper.final_url),
237-
generateFileName(paper),
238-
);
240+
try {
241+
const response = await fetch(getSecureUrl(paper.final_url));
242+
const blob = await response.blob();
243+
const filename = generateFileName(paper);
244+
zip.file(filename, blob);
245+
} catch (err) {
246+
// Optionally handle individual download errors
247+
console.error(`Failed to fetch ${paper.final_url}`, err);
248+
}
239249
}
250+
251+
const zipBlob = await zip.generateAsync({ type: "blob" });
252+
const url = URL.createObjectURL(zipBlob);
253+
const a = document.createElement("a");
254+
a.href = url;
255+
a.download = "papers.zip";
256+
document.body.appendChild(a);
257+
a.click();
258+
a.remove();
259+
URL.revokeObjectURL(url);
260+
toast.success("Download Initiated")
240261
}, [selectedPapers]);
241262

242263
const handleApplyFilters = useCallback(
@@ -338,7 +359,7 @@ const CatalogueContent = () => {
338359
selectedPapers={selectedPapers}
339360
subject={subject}
340361
filterOptions={filterOptions}
341-
handleDownloadAll={handleDownloadAll}
362+
handleDownloadSelected={handleDownloadSelected}
342363
closeFilters={closeFilters}
343364
/>
344365
</div>}
@@ -374,7 +395,7 @@ const CatalogueContent = () => {
374395
selectedPapers={selectedPapers}
375396
subject={subject}
376397
filterOptions={filterOptions}
377-
handleDownloadAll={handleDownloadAll}
398+
handleDownloadSelected={handleDownloadSelected}
378399
closeFilters={closeFilters}
379400
/>
380401
</SheetContent>

src/components/SideBar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function SideBar({
2222
handleApplyFilters,
2323
handleSelectAll,
2424
handleDeselectAll,
25-
handleDownloadAll,
25+
handleDownloadSelected: handleDownloadAll,
2626
}: {
2727
loading: boolean;
2828
selectedExams: string[];
@@ -47,7 +47,7 @@ function SideBar({
4747
) => void;
4848
handleSelectAll: () => void;
4949
handleDeselectAll: () => void;
50-
handleDownloadAll: () => void;
50+
handleDownloadSelected: () => void;
5151
}) {
5252
const exams =
5353
filterOptions?.unique_exams.map((exam) => ({
@@ -188,7 +188,7 @@ function SideBar({
188188
onClick={handleDownloadAll}
189189
className="cursor-pointer rounded-full border-2 border-black px-2 py-1 font-play text-xs font-semibold hover:bg-[#B2B8FF] hover:text-black dark:border-white dark:hover:border-[#434dba] dark:hover:bg-[#434dba] dark:hover:text-white"
190190
>
191-
Download All
191+
Download Selected
192192
</div>
193193
</div>
194194

0 commit comments

Comments
 (0)