Skip to content

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

perf(pptx): stream large media to storage to avoid OOM

Marc Seitz1f810ab832b5
diff --git a/lib/files/put-file-server.ts b/lib/files/put-file-server.ts
index 8b285fdf..86bc3d4f 100644
--- a/lib/files/put-file-server.ts
+++ b/lib/files/put-file-server.ts
@@ -1,6 +1,8 @@
import { PutObjectCommand } from "@aws-sdk/client-s3";
+import { Upload } from "@aws-sdk/lib-storage";
import { DocumentStorageType } from "@prisma/client";
import { put } from "@vercel/blob";
+import { createReadStream } from "node:fs";
import path from "node:path";
import { match } from "ts-pattern";
@@ -124,3 +126,118 @@ const putFileInS3Server = async ({
data: key,
};
};
+
+// `File` variant that streams from a path on disk instead of holding the whole
+// payload in a Buffer. Used for large media (videos) so memory stays flat.
+type StreamFile = {
+ name: string;
+ type: string;
+ path: string;
+};
+
+/**
+ * Streaming counterpart to {@link putFileServer}. Reads the file from disk as a
+ * stream and uploads it without buffering the whole payload in memory (S3 uses
+ * multipart via lib-storage; Vercel Blob uses multipart streaming).
+ */
+export const putFileServerStream = async ({
+ file,
+ teamId,
+ docId,
+ restricted = true,
+ subfolder,
+}: {
+ file: StreamFile;
+ teamId: string;
+ docId?: string;
+ restricted?: boolean;
+ subfolder?: string;
+}) => {
+ const NEXT_PUBLIC_UPLOAD_TRANSPORT = process.env.NEXT_PUBLIC_UPLOAD_TRANSPORT;
+
+ const { type, data } = await match(NEXT_PUBLIC_UPLOAD_TRANSPORT)
+ .with("s3", async () =>
+ putFileStreamInS3Server({ file, teamId, docId, restricted, subfolder }),
+ )
+ .with("vercel", async () => putFileStreamInVercelServer(file))
+ .otherwise(() => {
+ return { type: null, data: null };
+ });
+
+ return { type, data };
+};
+
+const putFileStreamInVercelServer = async (file: StreamFile) => {
+ const blob = await put(file.name, createReadStream(file.path), {
+ access: "public",
+ addRandomSuffix: true,
+ multipart: true,
+ contentType: file.type,
+ });
+
+ return {
+ type: DocumentStorageType.VERCEL_BLOB,
+ data: blob.url,
+ };
+};
+
+const putFileStreamInS3Server = async ({
+ file,
+ teamId,
+ docId,
+ restricted = true,
+ subfolder,
+}: {
+ file: StreamFile;
+ teamId: string;
+ docId?: string;
+ restricted?: boolean;
+ subfolder?: string;
+}) => {
+ if (!docId) {
+ docId = newId("doc");
+ }
+
+ if (
+ restricted &&
+ file.type !== "image/png" &&
+ file.type !== "image/jpeg" &&
+ file.type !== "application/pdf"
+ ) {
+ throw new Error("Only PNG, JPEG, PDF or MP4 files are supported");
+ }
+
+ if (!restricted && !SUPPORTED_DOCUMENT_MIME_TYPES.includes(file.type)) {
+ throw new Error("Unsupported file type");
+ }
+
+ const { client, config } = await getTeamS3ClientAndConfig(teamId);
+
+ const { name, ext } = path.parse(file.name);
+
+ const slugifiedName = safeSlugify(name) + ext;
+ const originalFileName = `${name}${ext}`;
+ const folderPrefix = subfolder ? `${safeSlugify(subfolder)}/` : "";
+ const key = `${teamId}/${docId}/${folderPrefix}${slugifiedName}`;
+
+ const upload = new Upload({
+ client,
+ params: {
+ Bucket: config.bucket,
+ Key: key,
+ Body: createReadStream(file.path),
+ ContentType: file.type,
+ ContentDisposition: buildContentDisposition(
+ originalFileName,
+ slugifiedName,
+ ),
+ },
+ });
+
+ await upload.done();
+
+ return {
+ type: DocumentStorageType.S3_PATH,
+ data: key,
+ };
+};