From 4402e6f04170cbb0613d35fe94471162253e91b2 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 19 Apr 2024 20:01:51 +0100 Subject: feature: Download images and screenshots --- apps/mobile/components/bookmarks/BookmarkCard.tsx | 69 +- apps/web/app/api/assets/route.ts | 4 +- .../components/dashboard/bookmarks/AssetCard.tsx | 7 +- .../bookmarks/BookmarkLayoutAdaptingCard.tsx | 2 +- .../components/dashboard/bookmarks/LinkCard.tsx | 11 +- .../components/dashboard/bookmarks/TextCard.tsx | 2 +- .../dashboard/preview/BookmarkPreview.tsx | 14 +- .../dashboard/preview/LinkContentSection.tsx | 77 ++ .../dashboard/preview/TextContentSection.tsx | 40 +- apps/web/lib/bookmarkUtils.tsx | 22 - apps/workers/crawlerWorker.ts | 158 +++- docker/docker-compose.yml | 1 + packages/db/drizzle/0020_sudden_dagger.sql | 2 + packages/db/drizzle/meta/0020_snapshot.json | 1000 ++++++++++++++++++++ packages/db/drizzle/meta/_journal.json | 7 + packages/db/schema.ts | 2 + packages/shared-react/utils/assetUtils.ts | 3 + packages/shared-react/utils/bookmarkUtils.ts | 39 + packages/shared/assetdb.ts | 4 + packages/shared/types/bookmarks.ts | 2 + packages/trpc/routers/bookmarks.ts | 41 +- tooling/eslint/base.js | 1 + 22 files changed, 1373 insertions(+), 135 deletions(-) create mode 100644 apps/web/components/dashboard/preview/LinkContentSection.tsx delete mode 100644 apps/web/lib/bookmarkUtils.tsx create mode 100644 packages/db/drizzle/0020_sudden_dagger.sql create mode 100644 packages/db/drizzle/meta/0020_snapshot.json create mode 100644 packages/shared-react/utils/assetUtils.ts create mode 100644 packages/shared-react/utils/bookmarkUtils.ts diff --git a/apps/mobile/components/bookmarks/BookmarkCard.tsx b/apps/mobile/components/bookmarks/BookmarkCard.tsx index 6662e76a..c995d593 100644 --- a/apps/mobile/components/bookmarks/BookmarkCard.tsx +++ b/apps/mobile/components/bookmarks/BookmarkCard.tsx @@ -21,33 +21,17 @@ import { useDeleteBookmark, useUpdateBookmark, } from "@hoarder/shared-react/hooks/bookmarks"; +import { + getBookmarkLinkImageUrl, + isBookmarkStillLoading, + isBookmarkStillTagging, +} from "@hoarder/shared-react/utils/bookmarkUtils"; import { TailwindResolver } from "../TailwindResolver"; import { Divider } from "../ui/Divider"; import { Skeleton } from "../ui/Skeleton"; import { useToast } from "../ui/Toast"; -const MAX_LOADING_MSEC = 30 * 1000; - -export function isBookmarkStillCrawling(bookmark: ZBookmark) { - return ( - bookmark.content.type === "link" && - !bookmark.content.crawledAt && - Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC - ); -} - -export function isBookmarkStillTagging(bookmark: ZBookmark) { - return ( - bookmark.taggingStatus === "pending" && - Date.now().valueOf() - bookmark.createdAt.valueOf() < MAX_LOADING_MSEC - ); -} - -export function isBookmarkStillLoading(bookmark: ZBookmark) { - return isBookmarkStillTagging(bookmark) || isBookmarkStillCrawling(bookmark); -} - function ActionBar({ bookmark }: { bookmark: ZBookmark }) { const { toast } = useToast(); @@ -176,6 +160,7 @@ function TagList({ bookmark }: { bookmark: ZBookmark }) { } function LinkCard({ bookmark }: { bookmark: ZBookmark }) { + const { settings } = useAppSettings(); if (bookmark.content.type !== "link") { throw new Error("Wrong content type rendered"); } @@ -183,18 +168,36 @@ function LinkCard({ bookmark }: { bookmark: ZBookmark }) { const url = bookmark.content.url; const parsedUrl = new URL(url); - const imageComp = bookmark.content.imageUrl ? ( - - ) : ( - - ); + const imageUrl = getBookmarkLinkImageUrl(bookmark.content); + + let imageComp; + if (imageUrl) { + imageComp = ( + + ); + } else { + imageComp = ( + + ); + } return ( diff --git a/apps/web/app/api/assets/route.ts b/apps/web/app/api/assets/route.ts index c77751d3..a1ebea0f 100644 --- a/apps/web/app/api/assets/route.ts +++ b/apps/web/app/api/assets/route.ts @@ -2,7 +2,7 @@ import { createContextFromRequest } from "@/server/api/client"; import { TRPCError } from "@trpc/server"; import type { ZUploadResponse } from "@hoarder/shared/types/uploads"; -import { saveAsset } from "@hoarder/shared/assetdb"; +import { newAssetId, saveAsset } from "@hoarder/shared/assetdb"; import serverConfig from "@hoarder/shared/config"; const SUPPORTED_ASSET_TYPES = new Set([ @@ -46,7 +46,7 @@ export async function POST(request: Request) { return Response.json({ error: "Bad request" }, { status: 400 }); } - const assetId = crypto.randomUUID(); + const assetId = newAssetId(); const fileName = data.name; await saveAsset({ diff --git a/apps/web/components/dashboard/bookmarks/AssetCard.tsx b/apps/web/components/dashboard/bookmarks/AssetCard.tsx index c9a43575..40f435de 100644 --- a/apps/web/components/dashboard/bookmarks/AssetCard.tsx +++ b/apps/web/components/dashboard/bookmarks/AssetCard.tsx @@ -1,13 +1,14 @@ "use client"; import Image from "next/image"; -import { isBookmarkStillTagging } from "@/lib/bookmarkUtils"; import { api } from "@/lib/trpc"; import type { ZBookmark, ZBookmarkTypeAsset, } from "@hoarder/shared/types/bookmarks"; +import { getAssetUrl } from "@hoarder/shared-react/utils/assetUtils"; +import { isBookmarkStillTagging } from "@hoarder/shared-react/utils/bookmarkUtils"; import { BookmarkLayoutAdaptingCard } from "./BookmarkLayoutAdaptingCard"; @@ -24,7 +25,7 @@ function AssetImage({ return ( asset @@ -35,7 +36,7 @@ function AssetImage({