From 362a30f860cd08ef468b0a433b3c2f2077560833 Mon Sep 17 00:00:00 2001 From: Ahmad Mujahid Date: Mon, 8 Apr 2024 19:15:52 +0400 Subject: [PATCH 1/9] feature: Add PDF support --- apps/mobile/lib/upload.ts | 6 +- apps/web/app/api/assets/route.ts | 3 +- .../components/dashboard/UploadDropzone.tsx | 6 +- .../dashboard/bookmarks/AssetCard.tsx | 7 + .../dashboard/bookmarks/BookmarkPreview.tsx | 39 +- apps/workers/openaiWorker.ts | 67 +- apps/workers/package.json | 4 +- apps/workers/searchWorker.ts | 8 + apps/workers/utils.ts | 12 + packages/db/drizzle/0015_first_reavers.sql | 3 + packages/db/drizzle/meta/0015_snapshot.json | 959 + packages/db/drizzle/meta/_journal.json | 7 + packages/db/schema.ts | 5 +- packages/trpc/routers/bookmarks.ts | 3 + packages/trpc/types/bookmarks.ts | 2 +- pnpm-lock.yaml | 28330 +++++++--------- 16 files changed, 12452 insertions(+), 17009 deletions(-) create mode 100644 packages/db/drizzle/0015_first_reavers.sql create mode 100644 packages/db/drizzle/meta/0015_snapshot.json diff --git a/apps/mobile/lib/upload.ts b/apps/mobile/lib/upload.ts index 56b2c7a5..f9d05967 100644 --- a/apps/mobile/lib/upload.ts +++ b/apps/mobile/lib/upload.ts @@ -38,7 +38,7 @@ export function useUploadAsset( mutationFn: async (file: { type: string; name: string; uri: string }) => { const formData = new FormData(); // @ts-expect-error This is a valid api in react native - formData.append("image", { + formData.append("file", { uri: file.uri, name: file.name, type: file.type, @@ -57,7 +57,9 @@ export function useUploadAsset( }, onSuccess: (resp) => { const assetId = resp.assetId; - createBookmark({ type: "asset", assetId, assetType: "image" }); + const assetType = + resp.contentType === "application/pdf" ? "pdf" : "image"; + createBookmark({ type: "asset", assetId, assetType }); }, onError: (e) => { if (options.onError) { diff --git a/apps/web/app/api/assets/route.ts b/apps/web/app/api/assets/route.ts index 0bb2f778..aa8598b7 100644 --- a/apps/web/app/api/assets/route.ts +++ b/apps/web/app/api/assets/route.ts @@ -9,6 +9,7 @@ const SUPPORTED_ASSET_TYPES = new Set([ "image/jpeg", "image/png", "image/webp", + "application/pdf", ]); const MAX_UPLOAD_SIZE_BYTES = serverConfig.maxAssetSizeMb * 1024 * 1024; @@ -26,7 +27,7 @@ export async function POST(request: Request) { }); } const formData = await request.formData(); - const data = formData.get("image"); + const data = formData.get("file"); let buffer; let contentType; if (data instanceof File) { diff --git a/apps/web/components/dashboard/UploadDropzone.tsx b/apps/web/components/dashboard/UploadDropzone.tsx index 70e6483a..b7b453bb 100644 --- a/apps/web/components/dashboard/UploadDropzone.tsx +++ b/apps/web/components/dashboard/UploadDropzone.tsx @@ -34,7 +34,7 @@ function useUploadAsset({ onComplete }: { onComplete: () => void }) { const { mutateAsync: runUpload } = useMutation({ mutationFn: async (file: File) => { const formData = new FormData(); - formData.append("image", file); + formData.append("file", file); const resp = await fetch("/api/assets", { method: "POST", body: formData, @@ -46,7 +46,9 @@ function useUploadAsset({ onComplete }: { onComplete: () => void }) { }, onSuccess: async (resp) => { const assetId = resp.assetId; - return createBookmark({ type: "asset", assetId, assetType: "image" }); + const assetType = + resp.contentType === "application/pdf" ? "pdf" : "image"; + return createBookmark({ type: "asset", assetId, assetType }); }, onError: (error, req) => { const err = zUploadErrorSchema.parse(JSON.parse(error.message)); diff --git a/apps/web/components/dashboard/bookmarks/AssetCard.tsx b/apps/web/components/dashboard/bookmarks/AssetCard.tsx index 460dbe98..8997a7e2 100644 --- a/apps/web/components/dashboard/bookmarks/AssetCard.tsx +++ b/apps/web/components/dashboard/bookmarks/AssetCard.tsx @@ -59,6 +59,13 @@ export default function AssetCard({ /> )} + {bookmarkedAsset.assetType == "pdf" && ( +