Skip to content

Commit

Permalink
Allow Epub generation from multiple bookmarks hoarder-app#295
Browse files Browse the repository at this point in the history
Changed dependency
added proper authentication check
added a flag to ignore errors while downloading
  • Loading branch information
kamtschatka committed Jul 21, 2024
1 parent e36132c commit 4c4e5a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
29 changes: 23 additions & 6 deletions apps/web/app/api/epub/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { inArray } from "drizzle-orm";
import epub, { Chapter } from "epub-gen-memory";
import { getServerAuthSession } from "@/server/auth";
import epub, { Chapter } from "@epubkit/epub-gen-memory";
import { and, eq, inArray } from "drizzle-orm";

import { db } from "@hoarder/db";
import { bookmarkLinks } from "@hoarder/db/schema";
import { bookmarkLinks, bookmarks } from "@hoarder/db/schema";

export async function GET(request: Request) {
const session = await getServerAuthSession();
if (!session || !session.user) {
return new Response("", {
status: 401,
});
}

const { searchParams } = new URL(request.url);
const assetIds = searchParams.getAll("assetId");

Expand All @@ -20,15 +28,21 @@ export async function GET(request: Request) {
htmlContent: bookmarkLinks.htmlContent,
})
.from(bookmarkLinks)
.where(inArray(bookmarkLinks.id, assetIds));
.leftJoin(bookmarks, eq(bookmarks.id, bookmarkLinks.id))
.where(
and(
eq(bookmarks.userId, session.user.id),
inArray(bookmarkLinks.id, assetIds),
),
);

if (!bookmarkInformation || bookmarkInformation.length === 0) {
return new Response("", {
status: 404,
});
}

const chapters = bookmarkInformation.map((information) => {
const chapters: Chapter[] = bookmarkInformation.map((information) => {
return {
content: information.htmlContent ?? "",
title: information.title ?? "",
Expand All @@ -37,7 +51,10 @@ export async function GET(request: Request) {

const title = getTitle(chapters);

const generatedEpub = await epub(title, chapters);
const generatedEpub = await epub(
{ title, ignoreFailedDownloads: true },
chapters,
);

return new Response(generatedEpub, {
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@auth/drizzle-adapter": "^0.8.0",
"@emoji-mart/data": "^1.1.2",
"@emoji-mart/react": "^1.1.1",
"@epubkit/epub-gen-memory": "1.0.10-aplha.8",
"@hoarder/db": "workspace:^0.1.0",
"@hoarder/shared": "workspace:^0.1.0",
"@hoarder/shared-react": "workspace:^0.1.0",
Expand Down Expand Up @@ -47,7 +48,6 @@
"clsx": "^2.1.0",
"dayjs": "^1.11.10",
"drizzle-orm": "^0.29.4",
"epub-gen-memory": "^1.0.10",
"fastest-levenshtein": "^1.0.16",
"lucide-react": "^0.330.0",
"next": "14.1.4",
Expand Down
52 changes: 26 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c4e5a8

Please sign in to comment.