Files
colanode/apps/server/src/lib/files.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

import {
PutObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
} from '@aws-sdk/client-s3';
2025-02-12 21:45:02 +01:00
import { FileAttributes } from '@colanode/core';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { configuration } from '@/lib/configuration';
import { fileS3 } from '@/data/storage';
2025-02-12 21:45:02 +01:00
export const buildFilePath = (
workspaceId: string,
fileId: string,
fileAttributes: FileAttributes
) => {
return `files/${workspaceId}/${fileId}_${fileAttributes.version}${fileAttributes.extension}`;
};
export const buildUploadUrl = async (
path: string,
size: number,
mimeType: string
) => {
const command = new PutObjectCommand({
Bucket: configuration.fileS3.bucketName,
Key: path,
ContentLength: size,
ContentType: mimeType,
});
const expiresIn = 60 * 60 * 4; // 4 hours
const presignedUrl = await getSignedUrl(fileS3, command, {
expiresIn,
});
return presignedUrl;
};
export const buildDownloadUrl = async (path: string) => {
const command = new GetObjectCommand({
Bucket: configuration.fileS3.bucketName,
Key: path,
});
const presignedUrl = await getSignedUrl(fileS3, command, {
expiresIn: 60 * 60 * 4, // 4 hours
});
return presignedUrl;
};
export const fetchFileMetadata = async (path: string) => {
const command = new HeadObjectCommand({
Bucket: configuration.fileS3.bucketName,
Key: path,
});
try {
const headObject = await fileS3.send(command);
return {
size: headObject.ContentLength,
mimeType: headObject.ContentType,
};
} catch {
return null;
}
};
export const deleteFile = async (path: string) => {
const command = new DeleteObjectCommand({
Bucket: configuration.fileS3.bucketName,
Key: path,
});
await fileS3.send(command);
};