mirror of
https://github.com/mfts/papermark.git
synced 2025-12-20 01:03:24 +08:00
Introduces team-specific S3 storage configuration and multi-region support, allowing teams to use either EU or US S3 buckets based on feature flags. Refactors file operations (upload, copy, delete, presigned URLs, Lambda invocations) to resolve storage region and credentials per team. Adds new storage config and multi-region S3Store, updates API routes and utility functions to use team-aware clients, and extends feature flags with 'usStorage'.
52 lines
1.0 KiB
TypeScript
52 lines
1.0 KiB
TypeScript
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 { 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 key = `${teamId}/${docId}/${slugify(name)}${ext}`;
|
|
|
|
const params = {
|
|
client,
|
|
params: {
|
|
Bucket: config.bucket,
|
|
Key: key,
|
|
Body: file.stream,
|
|
ContentType: file.type,
|
|
},
|
|
};
|
|
|
|
// 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,
|
|
};
|
|
};
|