Store all uploads in a special table in server

This commit is contained in:
Hakan Shehu
2025-02-20 08:55:25 +01:00
parent 88f6f2d2f2
commit 81dd328d4b
12 changed files with 300 additions and 132 deletions

View File

@@ -1,4 +1,14 @@
import {
PutObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
} from '@aws-sdk/client-s3';
import { FileAttributes } from '@colanode/core';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { configuration } from '@/lib/configuration';
import { fileS3 } from '@/data/storage';
export const buildFilePath = (
workspaceId: string,
@@ -7,3 +17,62 @@ export const buildFilePath = (
) => {
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);
};