forked from nathydre21/nepa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStorageService.ts
More file actions
59 lines (50 loc) · 1.66 KB
/
FileStorageService.ts
File metadata and controls
59 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { PrismaClient } from '@prisma/client';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import zlib from 'zlib';
import { promisify } from 'util';
const gzip = promisify(zlib.gzip);
const prisma = new PrismaClient();
// Initialize S3 Client (Ensure AWS credentials are set in environment variables)
const s3Client = new S3Client({ region: process.env.AWS_REGION || 'us-east-1' });
interface UploadedFile {
originalname: string;
mimetype: string;
buffer: Buffer;
size: number;
}
export class FileStorageService {
async uploadFile(file: UploadedFile, userId: string, provider: 'S3' | 'IPFS' = 'S3') {
// 1. Compress file for optimization
const compressedBuffer = await gzip(file.buffer);
let path = '';
let key = '';
if (provider === 'S3') {
key = `documents/${userId}/${Date.now()}_${file.originalname}.gz`;
await s3Client.send(new PutObjectCommand({
Bucket: process.env.AWS_BUCKET_NAME,
Key: key,
Body: compressedBuffer,
ContentType: file.mimetype,
ContentEncoding: 'gzip',
}));
path = `https://${process.env.AWS_BUCKET_NAME}.s3.amazonaws.com/${key}`;
} else {
// Mock IPFS implementation
// In production, use 'ipfs-http-client' to add file to IPFS node
key = `ipfs_hash_${Date.now()}`;
path = `ipfs://${key}`;
}
// 2. Save metadata to database
return prisma.document.create({
data: {
filename: file.originalname,
originalName: file.originalname,
mimeType: file.mimetype,
size: file.size,
path: path,
provider: provider,
userId: userId
}
});
}
}