mirror of
https://github.com/hanzoai/dataroom.git
synced 2026-10-30 18:41:07 +00:00
Use RFC 5987 filename* parameter to include the UTF-8 encoded original filename alongside the ASCII-safe slug, so downloads show the proper CJK name in modern browsers. Also adds missing Content-Disposition to put-file-server and stream-file-server uploads. Co-authored-by: Hanzo Dev <[email protected]>
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { Upload } from "@aws-sdk/lib-storage";
|
|
import { DocumentStorageType } from "@prisma/client";
|
|
import path from "node:path";
|
|
import { Readable } from "stream";
|
|
|
|
import { safeSlugify } from "@/lib/utils";
|
|
|
|
import { getTeamS3ClientAndConfig } from "./aws-client";
|
|
|
|
type StreamFile = {
|
|
name: string;
|
|
type: string;
|
|
stream: Readable;
|
|
};
|
|
|
|
export const streamFileServer = async ({
|
|
file,
|
|
teamId,
|
|
docId,
|
|
}: {
|
|
file: StreamFile;
|
|
teamId: string;
|
|
docId: string;
|
|
}) => {
|
|
const { client, config } = await getTeamS3ClientAndConfig(teamId);
|
|
|
|
// Get the basename and extension for the file
|
|
const { name, ext } = path.parse(file.name);
|
|
|
|
const slugifiedName = safeSlugify(name) + ext;
|
|
const originalFileName = `${name}${ext}`;
|
|
const key = `${teamId}/${docId}/${slugifiedName}`;
|
|
|
|
const params = {
|
|
client,
|
|
params: {
|
|
Bucket: config.bucket,
|
|
Key: key,
|
|
Body: file.stream,
|
|
ContentType: file.type,
|
|
ContentDisposition: `attachment; filename="${slugifiedName}"; filename*=UTF-8''${encodeURIComponent(originalFileName)}`,
|
|
},
|
|
};
|
|
|
|
// Use Upload for multipart upload support
|
|
const upload = new Upload(params);
|
|
|
|
// Send the upload to S3
|
|
await upload.done();
|
|
|
|
return {
|
|
type: DocumentStorageType.S3_PATH,
|
|
data: key,
|
|
};
|
|
};
|