Skip to content

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

fix: preserve original CJK filename in Content-Disposition header

Marc Seitz3070b01e60a7
diff --git a/lib/files/put-file-server.ts b/lib/files/put-file-server.ts
index f118b683..44c085fc 100644
--- a/lib/files/put-file-server.ts
+++ b/lib/files/put-file-server.ts
@@ -1,11 +1,11 @@
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { DocumentStorageType } from "@prisma/client";
-import slugify from "@sindresorhus/slugify";
import { put } from "@vercel/blob";
import path from "node:path";
import { match } from "ts-pattern";
import { newId } from "@/lib/id-helper";
+import { safeSlugify } from "@/lib/utils";
import { SUPPORTED_DOCUMENT_MIME_TYPES } from "../constants";
import { getTeamS3ClientAndConfig } from "./aws-client";
@@ -93,13 +93,16 @@ const putFileInS3Server = async ({
// Get the basename and extension for the file
const { name, ext } = path.parse(file.name);
- const key = `${teamId}/${docId}/${slugify(name)}${ext}`;
+ const slugifiedName = safeSlugify(name) + ext;
+ const originalFileName = `${name}${ext}`;
+ const key = `${teamId}/${docId}/${slugifiedName}`;
const params = {
Bucket: config.bucket,
Key: key,
Body: file.buffer,
ContentType: file.type,
+ ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
};
// Create a new instance of the PutObjectCommand with the parameters
diff --git a/lib/files/put-file.ts b/lib/files/put-file.ts
index eacef6b7..a36ccade 100644
--- a/lib/files/put-file.ts
+++ b/lib/files/put-file.ts
@@ -148,17 +148,19 @@ const putFileSingle = async ({
);
}
- const { url, key, fileName } = (await presignedResponse.json()) as {
- url: string;
- key: string;
- fileName: string;
- };
+ const { url, key, fileName, contentDisposition } =
+ (await presignedResponse.json()) as {
+ url: string;
+ key: string;
+ fileName: string;
+ contentDisposition: string;
+ };
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": file.type,
- "Content-Disposition": `attachment; filename="${fileName}"`,
+ "Content-Disposition": contentDisposition,
},
body: file,
});
diff --git a/lib/files/stream-file-server.ts b/lib/files/stream-file-server.ts
index a15cf802..27cd5bf0 100644
--- a/lib/files/stream-file-server.ts
+++ b/lib/files/stream-file-server.ts
@@ -1,9 +1,10 @@
import { Upload } from "@aws-sdk/lib-storage";
import { DocumentStorageType } from "@prisma/client";
-import slugify from "@sindresorhus/slugify";
import path from "node:path";
import { Readable } from "stream";
+import { safeSlugify } from "@/lib/utils";
+
import { getTeamS3ClientAndConfig } from "./aws-client";
type StreamFile = {
@@ -26,7 +27,9 @@ export const streamFileServer = async ({
// Get the basename and extension for the file
const { name, ext } = path.parse(file.name);
- const key = `${teamId}/${docId}/${slugify(name)}${ext}`;
+ const slugifiedName = safeSlugify(name) + ext;
+ const originalFileName = `${name}${ext}`;
+ const key = `${teamId}/${docId}/${slugifiedName}`;
const params = {
client,
@@ -35,6 +38,7 @@ export const streamFileServer = async ({
Key: key,
Body: file.stream,
ContentType: file.type,
+ ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
},
};
diff --git a/pages/api/file/s3/get-presigned-post-url.ts b/pages/api/file/s3/get-presigned-post-url.ts
index c19038ee..33fa7fa6 100644
--- a/pages/api/file/s3/get-presigned-post-url.ts
+++ b/pages/api/file/s3/get-presigned-post-url.ts
@@ -2,12 +2,12 @@ import { NextApiRequest, NextApiResponse } from "next";
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
-import slugify from "@sindresorhus/slugify";
import { getServerSession } from "next-auth";
import path from "node:path";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
+import { safeSlugify } from "@/lib/utils";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
@@ -53,8 +53,10 @@ export default async function handler(
// Get the basename and extension for the file
const { name, ext } = path.parse(fileName);
- const slugifiedName = slugify(name) + ext;
+ const slugifiedName = safeSlugify(name) + ext;
+ const originalFileName = `${name}${ext}`;
const key = `${team.id}/${docId}/${slugifiedName}`;
+ const contentDisposition = `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
const { client, config } = await getTeamS3ClientAndConfig(team.id);
@@ -62,14 +64,16 @@ export default async function handler(
Bucket: config.bucket,
Key: key,
ContentType: contentType,
- ContentDisposition: `attachment; filename="${slugifiedName}"`,
+ ContentDisposition: contentDisposition,
});
const url = await getSignedUrl(client, putObjectCommand, {
expiresIn: ONE_HOUR / ONE_SECOND,
});
- return res.status(200).json({ url, key, docId, fileName: slugifiedName });
+ return res
+ .status(200)
+ .json({ url, key, docId, fileName: slugifiedName, contentDisposition });
} catch (error) {
return res.status(500).json({ error: "Internal server error" });
}
diff --git a/pages/api/file/s3/multipart.ts b/pages/api/file/s3/multipart.ts
index fbff1cd2..d4b41b82 100644
--- a/pages/api/file/s3/multipart.ts
+++ b/pages/api/file/s3/multipart.ts
@@ -7,12 +7,12 @@ import {
UploadPartCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
-import slugify from "@sindresorhus/slugify";
import { getServerSession } from "next-auth";
import path from "node:path";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
+import { safeSlugify } from "@/lib/utils";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
import { MultipartUploadSchema } from "@/lib/zod/schemas/multipart";
@@ -68,7 +68,8 @@ export default async function handler(
// Get the basename and extension for the file
const { name, ext } = path.parse(fileName);
- const slugifiedName = slugify(name) + ext;
+ const slugifiedName = safeSlugify(name) + ext;
+ const originalFileName = `${name}${ext}`;
const key = `${team.id}/${docId}/${slugifiedName}`;
const { client, config } = await getTeamS3ClientAndConfig(team.id);
@@ -80,7 +81,7 @@ export default async function handler(
Bucket: config.bucket,
Key: key,
ContentType: contentType,
- ContentDisposition: `attachment; filename="${slugifiedName}"`,
+ ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
});
const createResponse = await client.send(createCommand);
diff --git a/pages/api/file/tus-viewer/[[...file]].ts b/pages/api/file/tus-viewer/[[...file]].ts
index f5a8d6ea..7fc90d77 100644
--- a/pages/api/file/tus-viewer/[[...file]].ts
+++ b/pages/api/file/tus-viewer/[[...file]].ts
@@ -2,12 +2,12 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { MultiRegionS3Store } from "@/ee/features/storage/s3-store";
import { CopyObjectCommand } from "@aws-sdk/client-s3";
-import slugify from "@sindresorhus/slugify";
import { Server } from "@tus/server";
import path from "node:path";
import { verifyDataroomSessionInPagesRouter } from "@/lib/auth/dataroom-auth";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
+import { safeSlugify } from "@/lib/utils";
import { RedisLocker } from "@/lib/files/tus-redis-locker";
import { newId } from "@/lib/id-helper";
import prisma from "@/lib/prisma";
@@ -78,7 +78,7 @@ const tusServer = new Server({
const docId = newId("doc");
const { name, ext } = path.parse(fileName);
- const newName = `${teamIdToUse}/${docId}/${slugify(name)}${ext}`;
+ const newName = `${teamIdToUse}/${docId}/${safeSlugify(name)}${ext}`;
return newName;
},
generateUrl(req, { proto, host, path, id }) {
@@ -147,7 +147,8 @@ const tusServer = new Server({
const metadata = upload.metadata || {};
const contentType = metadata.contentType || "application/octet-stream";
const { name, ext } = path.parse(metadata.fileName!);
- const contentDisposition = `attachment; filename="${slugify(name)}${ext}"`;
+ const originalFileName = `${name}${ext}`;
+ const contentDisposition = `attachment; filename="${safeSlugify(name)}${ext}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
// The Key (object path) where the file was uploaded
const objectKey = upload.id;
diff --git a/pages/api/file/tus/[[...file]].ts b/pages/api/file/tus/[[...file]].ts
index ef4e1933..0ba678a7 100644
--- a/pages/api/file/tus/[[...file]].ts
+++ b/pages/api/file/tus/[[...file]].ts
@@ -2,12 +2,12 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { MultiRegionS3Store } from "@/ee/features/storage/s3-store";
import { CopyObjectCommand } from "@aws-sdk/client-s3";
-import slugify from "@sindresorhus/slugify";
import { Server } from "@tus/server";
import { getServerSession } from "next-auth/next";
import path from "node:path";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
+import { safeSlugify } from "@/lib/utils";
import { RedisLocker } from "@/lib/files/tus-redis-locker";
import { newId } from "@/lib/id-helper";
import { lockerRedisClient } from "@/lib/redis";
@@ -40,7 +40,7 @@ const tusServer = new Server({
};
const docId = newId("doc");
const { name, ext } = path.parse(fileName);
- const newName = `${teamId}/${docId}/${slugify(name)}${ext}`;
+ const newName = `${teamId}/${docId}/${safeSlugify(name)}${ext}`;
return newName;
},
generateUrl(req, { proto, host, path, id }) {
@@ -65,7 +65,8 @@ const tusServer = new Server({
const metadata = upload.metadata || {};
const contentType = metadata.contentType || "application/octet-stream";
const { name, ext } = path.parse(metadata.fileName!);
- const contentDisposition = `attachment; filename="${slugify(name)}${ext}"`;
+ const originalFileName = `${name}${ext}`;
+ const contentDisposition = `attachment; filename="${safeSlugify(name)}${ext}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`;
// The Key (object path) where the file was uploaded
const objectKey = upload.id;