forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.service.ts
More file actions
192 lines (157 loc) · 7.14 KB
/
Copy pathupload.service.ts
File metadata and controls
192 lines (157 loc) · 7.14 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { Injectable, Logger, BadRequestException, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { v4 as uuidv4 } from 'uuid';
import { UploadPolicyValidator, DEFAULT_UPLOAD_POLICY, UploadPolicy } from './upload-policy';
@Injectable()
export class UploadService implements OnModuleInit {
private readonly logger = new Logger(UploadService.name);
private readonly s3Client: S3Client;
private readonly bucketName: string;
private readonly policyValidator: UploadPolicyValidator;
constructor(private readonly configService: ConfigService) {
this.validateS3Configuration();
const region = this.configService.get<string>('AWS_REGION')!;
const accessKeyId = this.configService.get<string>('AWS_ACCESS_KEY_ID')!;
const secretAccessKey = this.configService.get<string>('AWS_SECRET_ACCESS_KEY')!;
this.s3Client = new S3Client({
region,
credentials: {
accessKeyId,
secretAccessKey,
},
endpoint: this.configService.get<string>('S3_ENDPOINT'),
forcePathStyle: true,
});
this.bucketName = this.configService.get<string>('S3_BUCKET_NAME')!;
// Initialize policy validator
const uploadPolicy = this.getUploadPolicy();
this.policyValidator = new UploadPolicyValidator(uploadPolicy);
}
onModuleInit() {
this.logger.log('UploadService initialized with S3 configuration');
}
private validateS3Configuration(): void {
const requiredVars = ['AWS_REGION', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'S3_BUCKET_NAME'];
const missing = requiredVars.filter(varName => !this.configService.get<string>(varName));
if (missing.length > 0) {
throw new Error(`Missing required S3 configuration: ${missing.join(', ')}`);
}
}
private getUploadPolicy(): UploadPolicy {
// Allow policy customization via environment variables if needed
const customPolicy: Partial<UploadPolicy> = {};
const maxFileSize = this.configService.get<string>('UPLOAD_MAX_FILE_SIZE');
if (maxFileSize) {
customPolicy.maxFileSize = this.parseFileSize(maxFileSize);
}
const allowedMimes = this.configService.get<string>('UPLOAD_ALLOWED_MIME_TYPES');
if (allowedMimes) {
customPolicy.allowedMimeTypes = allowedMimes.split(',').map(mime => mime.trim());
}
const keyPrefix = this.configService.get<string>('UPLOAD_KEY_PREFIX');
if (keyPrefix) {
customPolicy.keyPrefix = keyPrefix;
}
return { ...DEFAULT_UPLOAD_POLICY, ...customPolicy };
}
private parseFileSize(sizeStr: string): number {
const units: Record<string, number> = {
'B': 1,
'KB': 1024,
'MB': 1024 * 1024,
'GB': 1024 * 1024 * 1024
};
const match = sizeStr.match(/^([0-9]+)\s*(B|KB|MB|GB)$/i);
if (!match) {
throw new Error(`Invalid file size format: ${sizeStr}. Use format like '10MB', '1GB', etc.`);
}
const [, size, unit] = match;
return parseInt(size) * units[unit.toUpperCase()];
}
async getPresignedUploadUrl(fileName: string, contentType: string, fileSize?: number): Promise<{ url: string; key: string }> {
// Validate inputs
if (!fileName || typeof fileName !== 'string') {
throw new BadRequestException('Filename is required and must be a string');
}
if (!contentType || typeof contentType !== 'string') {
throw new BadRequestException('Content type is required and must be a string');
}
// Validate file extension
this.policyValidator.validateFileExtension(fileName);
// Validate MIME type
this.policyValidator.validateMimeType(contentType);
// Validate file size if provided
if (fileSize !== undefined) {
if (typeof fileSize !== 'number' || fileSize < 0) {
throw new BadRequestException('File size must be a positive number');
}
this.policyValidator.validateFileSize(fileSize);
}
// Sanitize filename and generate object key
const sanitizedFileName = this.policyValidator.sanitizeFilename(fileName);
const uuid = uuidv4();
const key = this.policyValidator.generateObjectKey(sanitizedFileName, uuid);
const command = new PutObjectCommand({
Bucket: this.bucketName,
Key: key,
ContentType: contentType,
ContentLength: fileSize,
Metadata: {
originalFilename: fileName,
uploadId: uuid,
uploadedAt: new Date().toISOString()
}
});
const url = await getSignedUrl(this.s3Client, command, {
expiresIn: this.policyValidator.getUploadExpiration()
});
this.logger.log(`Generated presigned upload URL for key: ${key}`);
return { url, key };
}
async getPresignedDownloadUrl(key: string): Promise<string> {
if (!key || typeof key !== 'string') {
throw new BadRequestException('Object key is required and must be a string');
}
// Validate key format to prevent path traversal
if (key.includes('..') || key.startsWith('/') || key.includes('\\')) {
throw new BadRequestException('Invalid object key format');
}
const headers = this.policyValidator.getDownloadHeaders();
const command = new GetObjectCommand({
Bucket: this.bucketName,
Key: key,
ResponseCacheControl: headers['Cache-Control'],
ResponseContentDisposition: headers['Content-Disposition'],
});
const url = await getSignedUrl(this.s3Client, command, {
expiresIn: this.policyValidator.getDownloadExpiration()
});
this.logger.log(`Generated presigned download URL for key: ${key}`);
return url;
}
async deleteFile(key: string): Promise<void> {
if (!key || typeof key !== 'string') {
throw new BadRequestException('Object key is required and must be a string');
}
// Validate key format to prevent path traversal
if (key.includes('..') || key.startsWith('/') || key.includes('\\')) {
throw new BadRequestException('Invalid object key format');
}
const command = new DeleteObjectCommand({
Bucket: this.bucketName,
Key: key,
});
try {
await this.s3Client.send(command);
this.logger.log(`Successfully deleted file: ${key}`);
} catch (err) {
this.logger.error(`Failed to delete file ${key} from S3:`, err);
throw err;
}
}
getPolicy(): UploadPolicy {
return this.policyValidator['policy'];
}
}