Skip to content

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

feat: add support for keynote files

Marc Seitz932edec36922
diff --git a/lib/api/documents/process-document.ts b/lib/api/documents/process-document.ts
index 2a555936..44672b8b 100644
--- a/lib/api/documents/process-document.ts
+++ b/lib/api/documents/process-document.ts
@@ -5,8 +5,11 @@ import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";
import notion from "@/lib/notion";
import { getNotionPageIdFromSlug } from "@/lib/notion/utils";
import prisma from "@/lib/prisma";
-import { convertCadToPdfTask } from "@/lib/trigger/convert-files";
-import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";
+import {
+ convertCadToPdfTask,
+ convertFilesToPdfTask,
+ convertKeynoteToPdfTask,
+} from "@/lib/trigger/convert-files";
import { processVideo } from "@/lib/trigger/optimize-video-files";
import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";
import { getExtension } from "@/lib/utils";
@@ -135,7 +138,30 @@ export const processDocument = async ({
});
// Trigger appropriate conversion tasks based on document type
- if (type === "docs" || type === "slides") {
+ // Check if it's a Keynote file (slides type with Keynote content type)
+ if (
+ type === "slides" &&
+ (contentType === "application/vnd.apple.keynote" ||
+ contentType === "application/x-iwork-keynote-sffkey")
+ ) {
+ await convertKeynoteToPdfTask.trigger(
+ {
+ documentId: document.id,
+ documentVersionId: document.versions[0].id,
+ teamId,
+ },
+ {
+ idempotencyKey: `${teamId}-${document.versions[0].id}-keynote`,
+ tags: [
+ `team_${teamId}`,
+ `document_${document.id}`,
+ `version:${document.versions[0].id}`,
+ ],
+ queue: conversionQueue(teamPlan),
+ concurrencyKey: teamId,
+ },
+ );
+ } else if (type === "docs" || type === "slides") {
await convertFilesToPdfTask.trigger(
{
documentId: document.id,
diff --git a/lib/constants.ts b/lib/constants.ts
index 84a56bee..3e61fc22 100644
--- a/lib/constants.ts
+++ b/lib/constants.ts
@@ -75,6 +75,8 @@ export const SUPPORTED_DOCUMENT_MIME_TYPES = [
"application/vnd.ms-powerpoint", // .ppt
"application/vnd.openxmlformats-officedocument.presentationml.presentation", // .pptx
"application/vnd.oasis.opendocument.presentation", // .odp
+ "application/vnd.apple.keynote", // .key
+ "application/x-iwork-keynote-sffkey", // .key (older format)
"application/msword", // .doc
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .docx
"application/vnd.oasis.opendocument.text", // .odt
@@ -126,6 +128,8 @@ export const FULL_PLAN_ACCEPTED_FILE_TYPES = {
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
[], // ".pptx"
"application/vnd.oasis.opendocument.presentation": [], // ".odp"
+ "application/vnd.apple.keynote": [".key"], // ".key"
+ "application/x-iwork-keynote-sffkey": [".key"], // ".key"
"application/msword": [], // ".doc"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": [], // ".docx"
"application/vnd.oasis.opendocument.text": [], // ".odt"
diff --git a/lib/trigger/convert-files.ts b/lib/trigger/convert-files.ts
index 0c25209c..c73c2c57 100644
--- a/lib/trigger/convert-files.ts
+++ b/lib/trigger/convert-files.ts
@@ -37,6 +37,7 @@ export const convertFilesToPdfTask = task({
const document = await prisma.document.findUnique({
where: {
id: payload.documentId,
+ teamId: payload.teamId,
},
select: {
name: true,
@@ -224,6 +225,7 @@ export const convertCadToPdfTask = task({
const document = await prisma.document.findUnique({
where: {
id: payload.documentId,
+ teamId: payload.teamId,
},
select: {
name: true,
@@ -387,3 +389,188 @@ export const convertCadToPdfTask = task({
return;
},
});
+
+// convert keynote file to pdf
+export const convertKeynoteToPdfTask = task({
+ id: "convert-keynote-to-pdf",
+ retry: { maxAttempts: 3 },
+ queue: {
+ concurrencyLimit: 2,
+ },
+ run: async (payload: ConvertPayload) => {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: payload.teamId,
+ },
+ });
+
+ if (!team) {
+ logger.error("Team not found", { teamId: payload.teamId });
+ return;
+ }
+
+ const document = await prisma.document.findUnique({
+ where: {
+ id: payload.documentId,
+ teamId: payload.teamId,
+ },
+ select: {
+ name: true,
+ versions: {
+ where: {
+ id: payload.documentVersionId,
+ },
+ select: {
+ file: true,
+ originalFile: true,
+ contentType: true,
+ storageType: true,
+ },
+ },
+ },
+ });
+
+ if (
+ !document ||
+ !document.versions[0] ||
+ !document.versions[0].originalFile ||
+ !document.versions[0].contentType
+ ) {
+ logger.error("Document not found", {
+ documentId: payload.documentId,
+ documentVersionId: payload.documentVersionId,
+ teamId: payload.teamId,
+ });
+ return;
+ }
+
+ const fileUrl = await getFile({
+ data: document.versions[0].originalFile,
+ type: document.versions[0].storageType,
+ });
+
+ // create payload for keynote to pdf conversion
+ const tasksPayload = {
+ tasks: {
+ "import-file-v1": {
+ operation: "import/url",
+ url: fileUrl,
+ filename: document.name,
+ },
+ "convert-file-v1": {
+ operation: "convert",
+ input: ["import-file-v1"],
+ input_format: getExtensionFromContentType(
+ document.versions[0].contentType,
+ ),
+ output_format: "pdf",
+ engine: "iwork",
+ },
+ "export-file-v1": {
+ operation: "export/url",
+ input: ["convert-file-v1"],
+ inline: false,
+ archive_multiple_files: false,
+ },
+ },
+ redirect: true,
+ };
+
+ // Make the conversion request
+ const conversionResponse = await retry.fetch(
+ `${process.env.NEXT_PRIVATE_CONVERT_API_URL}`,
+ {
+ method: "POST",
+ body: JSON.stringify(tasksPayload),
+ headers: {
+ Authorization: `Bearer ${process.env.NEXT_PRIVATE_CONVERT_API_KEY}`,
+ "Content-Type": "application/json",
+ },
+ retry: {
+ byStatus: {
+ "500-599": {
+ strategy: "backoff",
+ maxAttempts: 3,
+ factor: 2,
+ minTimeoutInMs: 1_000,
+ maxTimeoutInMs: 30_000,
+ randomize: false,
+ },
+ },
+ },
+ },
+ );
+
+ if (!conversionResponse.ok) {
+ const body = await conversionResponse.json();
+ throw new Error(
+ `Conversion failed: ${body.message} ${conversionResponse.status}`,
+ );
+ }
+
+ const conversionBuffer = Buffer.from(
+ await conversionResponse.arrayBuffer(),
+ );
+
+ // get docId from url with starts with "doc_" with regex
+ const match = document.versions[0].originalFile.match(/(doc_[^\/]+)\//);
+ const docId = match ? match[1] : undefined;
+
+ // Save the converted file to the database
+ const { type: storageType, data } = await putFileServer({
+ file: {
+ name: `${document.name}.pdf`,
+ type: "application/pdf",
+ buffer: conversionBuffer,
+ },
+ teamId: payload.teamId,
+ docId: docId,
+ });
+
+ if (!data || !storageType) {
+ logger.error("Failed to save converted file to database", {
+ documentId: payload.documentId,
+ documentVersionId: payload.documentVersionId,
+ teamId: payload.teamId,
+ docId: docId,
+ });
+ return;
+ }
+
+ console.log("data from conversion", data);
+ console.log("storageType from conversion", storageType);
+
+ await prisma.documentVersion.update({
+ where: { id: payload.documentVersionId },
+ data: {
+ file: data,
+ type: "pdf",
+ storageType: storageType,
+ },
+ });
+
+ await convertPdfToImageRoute.trigger(
+ {
+ documentId: payload.documentId,
+ documentVersionId: payload.documentVersionId,
+ teamId: payload.teamId,
+ },
+ {
+ idempotencyKey: `${payload.teamId}-${payload.documentVersionId}`,
+ tags: [
+ `team_${payload.teamId}`,
+ `document_${payload.documentId}`,
+ `version:${payload.documentVersionId}`,
+ ],
+ },
+ );
+
+ logger.info("Keynote document converted", {
+ documentId: payload.documentId,
+ documentVersionId: payload.documentVersionId,
+ teamId: payload.teamId,
+ docId: docId,
+ });
+ return;
+ },
+});
diff --git a/lib/utils/get-content-type.ts b/lib/utils/get-content-type.ts
index fe2e9fb0..7b8e9af0 100644
--- a/lib/utils/get-content-type.ts
+++ b/lib/utils/get-content-type.ts
@@ -19,6 +19,8 @@ export function getSupportedContentType(contentType: string): string | null {
case "application/vnd.ms-powerpoint":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
case "application/vnd.oasis.opendocument.presentation":
+ case "application/vnd.apple.keynote":
+ case "application/x-iwork-keynote-sffkey":
return "slides";
case "image/vnd.dwg":
case "image/vnd.dxf":
@@ -78,6 +80,9 @@ export function getExtensionFromContentType(
return "odp";
case "application/vnd.ms-powerpoint":
return "ppt";
+ case "application/vnd.apple.keynote":
+ case "application/x-iwork-keynote-sffkey":
+ return "key";
case "application/msword":
return "doc";
case "application/rtf":
diff --git a/lib/utils/get-file-icon.tsx b/lib/utils/get-file-icon.tsx
index 13935e5a..1f5d9506 100644
--- a/lib/utils/get-file-icon.tsx
+++ b/lib/utils/get-file-icon.tsx
@@ -36,6 +36,8 @@ export function fileIcon({
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
case "application/vnd.ms-powerpoint":
case "application/vnd.oasis.opendocument.presentation":
+ case "application/vnd.apple.keynote":
+ case "application/x-iwork-keynote-sffkey":
case "slides":
return <SlidesIcon className={className} isLight={isLight} />;
case "application/vnd.ms-excel":