diff --git a/apps/web/app/api/epub/route.ts b/apps/web/app/api/epub/route.ts index 591f955d..da81bc84 100644 --- a/apps/web/app/api/epub/route.ts +++ b/apps/web/app/api/epub/route.ts @@ -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"); @@ -20,7 +28,13 @@ 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("", { @@ -28,7 +42,7 @@ export async function GET(request: Request) { }); } - const chapters = bookmarkInformation.map((information) => { + const chapters: Chapter[] = bookmarkInformation.map((information) => { return { content: information.htmlContent ?? "", title: information.title ?? "", @@ -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, diff --git a/apps/web/package.json b/apps/web/package.json index f7f4f61d..ce6e7ada 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -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", @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a3234e50..d0202804 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -445,6 +445,9 @@ importers: '@emoji-mart/react': specifier: ^1.1.1 version: 1.1.1(emoji-mart@5.5.2)(react@18.2.0) + '@epubkit/epub-gen-memory': + specifier: 1.0.10-aplha.8 + version: 1.0.10-aplha.8 '@hoarder/db': specifier: workspace:^0.1.0 version: link:../../packages/db @@ -532,9 +535,6 @@ importers: drizzle-orm: specifier: ^0.29.4 version: 0.29.4(@types/react@18.2.58)(better-sqlite3@9.4.3)(react@18.2.0) - epub-gen-memory: - specifier: ^1.0.10 - version: 1.0.10 fastest-levenshtein: specifier: ^1.0.16 version: 1.0.16 @@ -2254,6 +2254,10 @@ packages: '@emotion/weak-memoize@0.3.1': resolution: {integrity: sha512-EsBwpc7hBUJWAsNPBmJy4hxWx12v6bshQsldrVmjxJoc3isbxhOrF2IcCpaXxfvq03NwkI7sbsOLXbYuqF/8Ww==} + '@epubkit/epub-gen-memory@1.0.10-aplha.8': + resolution: {integrity: sha512-YGHltjJnmSRceyQyY74vL5WTRpswQtdv+nR6oiHFLUIj5P1gfC4O0MnX7v5Wi5oX4VDzAyxouCz9OwWqmF0zag==} + engines: {node: '>=10.0.0'} + '@esbuild-kit/core-utils@3.3.2': resolution: {integrity: sha512-sPRAnw9CdSsRmEtnsl2WXWdyquogVpB3yZ3dgwJfe8zrOzTsV7cJvmwrKVa+0ma5BoiGJ+BoqkMvawbayKUsqQ==} @@ -6254,10 +6258,6 @@ packages: eol@0.9.1: resolution: {integrity: sha512-Ds/TEoZjwggRoz/Q2O7SE3i4Jm66mqTDfmdHdq/7DKVk3bro9Q8h6WdXKdPqFLMoqxrDK5SVRzHVPOS6uuGtrg==} - epub-gen-memory@1.0.10: - resolution: {integrity: sha512-QS4rEGCO/AUaaofhvAzXAxz0bJ/u23ZGdsdfjVBsWE6LHqM1Mi4R0rs+f6BbGDrHWBvASZkadvrfMc73TFpC6Q==} - engines: {node: '>=10.0.0'} - err-code@2.0.3: resolution: {integrity: sha512-2bmlRpNKBxT/CRmPOlyISQpNj+qSeYvcym/uT0Jx2bMOlKLtSy1ZmLuVxSEKKyor/N5yhvp/ZiG1oE3DEYMSFA==} @@ -15429,6 +15429,25 @@ snapshots: '@emotion/weak-memoize@0.3.1': dev: false + '@epubkit/epub-gen-memory@1.0.10-aplha.8': + dependencies: + abort-controller: 3.0.0 + css-select: 4.3.0 + diacritics: 1.3.0 + dom-serializer: 1.4.1 + domhandler: 4.3.1 + domutils: 2.8.0 + ejs: 3.1.9 + htmlparser2: 7.2.0 + jszip: 3.10.1 + mime: 2.6.0 + node-fetch: 2.7.0 + ow: 0.28.2 + slugify: 1.6.6 + transitivePeerDependencies: + - encoding + dev: false + '@esbuild-kit/core-utils@3.3.2': dependencies: esbuild: 0.18.20 @@ -20888,25 +20907,6 @@ snapshots: eol@0.9.1: dev: false - epub-gen-memory@1.0.10: - dependencies: - abort-controller: 3.0.0 - css-select: 4.3.0 - diacritics: 1.3.0 - dom-serializer: 1.4.1 - domhandler: 4.3.1 - domutils: 2.8.0 - ejs: 3.1.9 - htmlparser2: 7.2.0 - jszip: 3.10.1 - mime: 2.6.0 - node-fetch: 2.7.0 - ow: 0.28.2 - slugify: 1.6.6 - transitivePeerDependencies: - - encoding - dev: false - err-code@2.0.3: dev: false