Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: welcome upload using putfile now

Marc Seitzb03b1079440c
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 076138fd..c0bc9c15 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -23,7 +23,6 @@ import {
createDocument,
createNewDocumentVersion,
} from "@/lib/documents/create-document";
-import { set } from "ts-pattern/dist/patterns";
import { useAnalytics } from "@/lib/analytics";
export function AddDocumentModal({
diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsx
index 82d27c20..01bc2e70 100644
--- a/components/welcome/upload.tsx
+++ b/components/welcome/upload.tsx
@@ -1,20 +1,16 @@
import { useRouter } from "next/router";
import { motion } from "framer-motion";
import { useState } from "react";
-import { type PutBlobResult } from "@vercel/blob";
-import { upload } from "@vercel/blob/client";
import DocumentUpload from "@/components/document-upload";
import { toast } from "sonner";
import Skeleton from "../Skeleton";
import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";
-import { pdfjs } from "react-pdf";
import {
convertDataUrlToFile,
copyToClipboard,
- getExtension,
uploadImage,
} from "@/lib/utils";
-import { Button } from "../ui/button";
+import { Button } from "@/components/ui/button";
import { usePlausible } from "next-plausible";
import { useTeam } from "@/context/team-context";
import {
@@ -22,12 +18,12 @@ import {
AccordionContent,
AccordionItem,
AccordionTrigger,
-} from "../ui/accordion";
+} from "@/components/ui/accordion";
import { LinkOptions } from "../links/link-sheet/link-options";
import { DEFAULT_LINK_PROPS, DEFAULT_LINK_TYPE } from "../links/link-sheet";
import { useAnalytics } from "@/lib/analytics";
-
-pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
+import { putFile } from "@/lib/files/put-file";
+import { DocumentData, createDocument } from "@/lib/documents/create-document";
export default function Upload() {
const router = useRouter();
@@ -43,6 +39,8 @@ export default function Upload() {
useState<DEFAULT_LINK_TYPE>(DEFAULT_LINK_PROPS);
const teamInfo = useTeam();
+ const teamId = teamInfo?.currentTeam?.id as string;
+
const handleBrowserUpload = async (event: any) => {
event.preventDefault();
@@ -55,23 +53,21 @@ export default function Upload() {
try {
setUploading(true);
- const newBlob = await upload(currentFile.name, currentFile, {
- access: "public",
- handleUploadUrl: "/api/file/browser-upload",
+ const { type, data, numPages } = await putFile({
+ file: currentFile,
+ teamId,
});
setCurrentFile(null);
setCurrentBlob(true);
- let response: Response | undefined;
- let numPages: number | undefined;
- // create a document in the database if the document is a pdf
- if (getExtension(newBlob.pathname).includes("pdf")) {
- numPages = await getTotalPages(newBlob.url);
- response = await saveDocumentToDatabase(newBlob, numPages);
- } else {
- response = await saveDocumentToDatabase(newBlob);
- }
+ const documentData: DocumentData = {
+ name: currentFile.name,
+ key: data!,
+ storageType: type!,
+ };
+ // create a document in the database
+ const response = await createDocument({ documentData, teamId, numPages });
if (response) {
const document = await response.json();
@@ -107,50 +103,6 @@ export default function Upload() {
}
};
- const saveDocumentToDatabase = async (
- blob: PutBlobResult,
- numPages?: number,
- ) => {
- // create a document in the database with the blob url
- const response = await fetch(
- `/api/teams/${teamInfo?.currentTeam?.id}/documents`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- name: blob.pathname,
- url: blob.url,
- numPages: numPages,
- }),
- },
- );
-
- if (!response.ok) {
- throw new Error(`HTTP error! status: ${response.status}`);
- }
-
- return response;
- };
-
- // get the number of pages in the pdf
- async function getTotalPages(url: string): Promise<number> {
- const pdf = await pdfjs.getDocument(url).promise;
- return pdf.numPages;
- }
-
- const handleContinue = (id: string) => {
- copyToClipboard(
- `${process.env.NEXT_PUBLIC_BASE_URL}/view/${id}`,
- "Link copied to clipboard. Redirecting to document page...",
- );
- setTimeout(() => {
- router.push(`/documents/${currentDocId}`);
- setUploading(false);
- }, 2000);
- };
-
const handleSubmit = async (event: any) => {
event.preventDefault();