Skip to content
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

Allow Epub generation from multiple bookmarks #295 #312

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
98 changes: 98 additions & 0 deletions apps/web/app/api/epub/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { getServerAuthSession } from "@/server/auth";
import epub, { Chapter } from "@kamtschatka/epub-gen-memory";
import { and, eq, inArray } from "drizzle-orm";

import { db } from "@hoarder/db";
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");

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

const bookmarkInformation = await db
.select({
kamtschatka marked this conversation as resolved.
Show resolved Hide resolved
title: bookmarkLinks.title,
htmlContent: bookmarkLinks.htmlContent,
})
.from(bookmarkLinks)
.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: Chapter[] = bookmarkInformation.map((information) => {
return {
content: information.htmlContent ?? "",
title: information.title ?? "",
};
});

const title = getTitle(chapters);
// If there is only 1 bookmark, we can skip the table of contents
const tocInTOC = chapters.length > 1;

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

return new Response(generatedEpub, {
status: 200,
headers: {
"Content-Type": "application/epub+zip",
"Content-Disposition": `attachment; filename=${createFilename()}`,
},
});
}

function urlValidator(url: string): boolean {
const urlParsed = new URL(url);
if (urlParsed.protocol != "http:" && urlParsed.protocol != "https:") {
return true;
}
return ["localhost", "127.0.0.1", "0.0.0.0"].includes(urlParsed.hostname);
}

function createFilename(): string {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");

return `${year}-${month}-${day}-hoarder-bookmarks.epub`;
}

function getTitle(chapters: Chapter[]): string {
if (chapters.length === 1 && chapters[0].title) {
return chapters[0].title;
}
return "Hoarder EPub export";
}
20 changes: 20 additions & 0 deletions apps/web/components/dashboard/BulkBookmarksAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useBulkActionsStore from "@/lib/bulkActions";
import { useTranslation } from "@/lib/i18n/client";
import {
CheckCheck,
Download,
FileDown,
Hash,
Link,
Expand Down Expand Up @@ -223,6 +224,25 @@ export default function BulkBookmarksAction() {
isPending: recrawlBookmarkMutator.isPending,
hidden: !isBulkEditEnabled,
},
{
name: t("actions.download_epub"),
icon: (
<a
href={`/api/epub?${selectedBookmarks
.filter((item) => item.content.type === BookmarkTypes.LINK)
.map((bookmark) => "assetId=" + bookmark.id)
.join("&")}`}
>
<Download size={18} />
</a>
),
isPending: false,
hidden:
!isBulkEditEnabled ||
selectedBookmarks.filter(
(item) => item.content.type === BookmarkTypes.LINK,
).length === 0,
},
{
name: t("actions.refresh"),
icon: <RotateCw size={18} />,
Expand Down
36 changes: 23 additions & 13 deletions apps/web/components/dashboard/bookmarks/BookmarkOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useToast } from "@/components/ui/use-toast";
import { useClientConfig } from "@/lib/clientConfig";
import { useTranslation } from "@/lib/i18n/client";
import {
Download,
FileDown,
Link,
List,
Expand Down Expand Up @@ -187,19 +188,28 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) {
)}

{bookmark.content.type === BookmarkTypes.LINK && (
<DropdownMenuItem
onClick={() => {
navigator.clipboard.writeText(
(bookmark.content as ZBookmarkedLink).url,
);
toast({
description: t("toasts.bookmarks.clipboard_copied"),
});
}}
>
<Link className="mr-2 size-4" />
<span>{t("actions.copy_link")}</span>
</DropdownMenuItem>
<>
<DropdownMenuItem
onClick={() => {
navigator.clipboard.writeText(
(bookmark.content as ZBookmarkedLink).url,
);
toast({
description: t("toasts.bookmarks.clipboard_copied"),
});
}}
>
<Link className="mr-2 size-4" />
<span>{t("actions.copy_link")}</span>
</DropdownMenuItem>

<DropdownMenuItem>
<Download className="mr-2 size-4"></Download>
<a href={`/api/epub?assetId=${bookmark.id}`}>
{t("actions.download_epub")}
</a>
</DropdownMenuItem>
</>
)}
<DropdownMenuItem onClick={() => setTagModalIsOpen(true)}>
<Tags className="mr-2 size-4" />
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Löschen",
"refresh": "Aktualisieren",
"download_full_page_archive": "Vollständiges Seitenarchiv herunterladen",
"download_epub": "EPUB herunterladen",
"edit_tags": "Tags bearbeiten",
"add_to_list": "Zur Liste hinzufügen",
"select_all": "Alle auswählen",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Delete",
"refresh": "Refresh",
"download_full_page_archive": "Download Full Page Archive",
"download_epub": "Download EPUB",
"edit_tags": "Edit Tags",
"add_to_list": "Add to List",
"select_all": "Select All",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "Supprimer",
"refresh": "Rafraîchir",
"download_full_page_archive": "Télécharger l'archive de la page complète",
"download_epub": "Télécharger EPUB",
"edit_tags": "Modifier les tags",
"add_to_list": "Ajouter à la liste",
"select_all": "Tout sélectionner",
Expand Down
1 change: 1 addition & 0 deletions apps/web/lib/i18n/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"delete": "删除",
"refresh": "刷新",
"download_full_page_archive": "下载完整页面归档",
"download_epub": "下载EPUB",
"edit_tags": "编辑标签",
"add_to_list": "添加到列表",
"select_all": "全选",
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@hoarder/shared-react": "workspace:^0.1.0",
"@hoarder/trpc": "workspace:^0.1.0",
"@hookform/resolvers": "^3.3.4",
"@kamtschatka/epub-gen-memory": "^1.0.0",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand Down
Loading
Loading