-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathattachments.service.ts
More file actions
308 lines (280 loc) · 8.42 KB
/
attachments.service.ts
File metadata and controls
308 lines (280 loc) · 8.42 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {
DeleteObjectCommand,
GetObjectCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { AttachmentEntityType, AttachmentType, db } from '@db';
import {
BadRequestException,
Injectable,
InternalServerErrorException,
} from '@nestjs/common';
import { randomBytes } from 'crypto';
import { AttachmentResponseDto } from '../tasks/dto/task-responses.dto';
import { UploadAttachmentDto } from './upload-attachment.dto';
@Injectable()
export class AttachmentsService {
private s3Client: S3Client;
private bucketName: string;
private readonly MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024; // 10MB
private readonly SIGNED_URL_EXPIRY = 900; // 15 minutes
constructor() {
// AWS configuration is validated at startup via ConfigModule
// Safe to access environment variables directly since they're validated
this.bucketName = process.env.APP_AWS_BUCKET_NAME!;
this.s3Client = new S3Client({
region: process.env.APP_AWS_REGION || 'us-east-1',
credentials: {
accessKeyId: process.env.APP_AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.APP_AWS_SECRET_ACCESS_KEY!,
},
});
}
/**
* Upload attachment to S3 and create database record
*/
async uploadAttachment(
organizationId: string,
entityId: string,
entityType: AttachmentEntityType,
uploadDto: UploadAttachmentDto,
userId?: string,
): Promise<AttachmentResponseDto> {
try {
// Validate file size
const fileBuffer = Buffer.from(uploadDto.fileData, 'base64');
if (fileBuffer.length > this.MAX_FILE_SIZE_BYTES) {
throw new BadRequestException(
`File size exceeds maximum allowed size of ${this.MAX_FILE_SIZE_BYTES / (1024 * 1024)}MB`,
);
}
// Generate unique file key
const fileId = randomBytes(16).toString('hex');
const sanitizedFileName = this.sanitizeFileName(uploadDto.fileName);
const timestamp = Date.now();
const s3Key = `${organizationId}/attachments/${entityType}/${entityId}/${timestamp}-${fileId}-${sanitizedFileName}`;
// Upload to S3
const putCommand = new PutObjectCommand({
Bucket: this.bucketName,
Key: s3Key,
Body: fileBuffer,
ContentType: uploadDto.fileType,
Metadata: {
// S3 metadata becomes HTTP headers (x-amz-meta-*) and must be ASCII without control chars
originalFileName: this.sanitizeHeaderValue(uploadDto.fileName),
organizationId,
entityId,
entityType,
...(userId && { uploadedBy: userId }),
},
});
await this.s3Client.send(putCommand);
// Create database record
const attachment = await db.attachment.create({
data: {
name: uploadDto.fileName,
url: s3Key,
type: this.mapFileTypeToAttachmentType(uploadDto.fileType),
entityId,
entityType,
organizationId,
},
});
// Generate signed URL for immediate access
const downloadUrl = await this.generateSignedUrl(s3Key);
return {
id: attachment.id,
name: attachment.name,
type: attachment.type,
downloadUrl,
createdAt: attachment.createdAt,
size: fileBuffer.length,
};
} catch (error) {
console.error('Error uploading attachment:', error);
if (error instanceof BadRequestException) {
throw error;
}
throw new InternalServerErrorException('Failed to upload attachment');
}
}
/**
* Get all attachments for an entity WITH signed URLs (for backward compatibility)
*/
async getAttachments(
organizationId: string,
entityId: string,
entityType: AttachmentEntityType,
): Promise<AttachmentResponseDto[]> {
const attachments = await db.attachment.findMany({
where: {
organizationId,
entityId,
entityType,
},
orderBy: {
createdAt: 'asc',
},
});
// Generate signed URLs for all attachments
const attachmentsWithUrls = await Promise.all(
attachments.map(async (attachment) => {
const downloadUrl = await this.generateSignedUrl(attachment.url);
return {
id: attachment.id,
name: attachment.name,
type: attachment.type,
downloadUrl,
createdAt: attachment.createdAt,
};
}),
);
return attachmentsWithUrls;
}
/**
* Get attachment metadata WITHOUT signed URLs (for on-demand URL generation)
*/
async getAttachmentMetadata(
organizationId: string,
entityId: string,
entityType: AttachmentEntityType,
): Promise<{ id: string; name: string; type: string; createdAt: Date }[]> {
const attachments = await db.attachment.findMany({
where: {
organizationId,
entityId,
entityType,
},
orderBy: {
createdAt: 'asc',
},
});
return attachments.map((attachment) => ({
id: attachment.id,
name: attachment.name,
type: attachment.type,
createdAt: attachment.createdAt,
}));
}
/**
* Get download URL for an attachment
*/
async getAttachmentDownloadUrl(
organizationId: string,
attachmentId: string,
): Promise<{ downloadUrl: string; expiresIn: number }> {
try {
// Get attachment record
const attachment = await db.attachment.findFirst({
where: {
id: attachmentId,
organizationId,
},
});
if (!attachment) {
throw new BadRequestException('Attachment not found');
}
// Generate signed URL
const downloadUrl = await this.generateSignedUrl(attachment.url);
return {
downloadUrl,
expiresIn: this.SIGNED_URL_EXPIRY,
};
} catch (error) {
console.error('Error generating download URL:', error);
if (error instanceof BadRequestException) {
throw error;
}
throw new InternalServerErrorException('Failed to generate download URL');
}
}
/**
* Delete attachment from S3 and database
*/
async deleteAttachment(
organizationId: string,
attachmentId: string,
): Promise<void> {
try {
// Get attachment record
const attachment = await db.attachment.findFirst({
where: {
id: attachmentId,
organizationId,
},
});
if (!attachment) {
throw new BadRequestException('Attachment not found');
}
// Delete from S3
const deleteCommand = new DeleteObjectCommand({
Bucket: this.bucketName,
Key: attachment.url,
});
await this.s3Client.send(deleteCommand);
// Delete from database
await db.attachment.delete({
where: {
id: attachmentId,
organizationId,
},
});
} catch (error) {
console.error('Error deleting attachment:', error);
if (error instanceof BadRequestException) {
throw error;
}
throw new InternalServerErrorException('Failed to delete attachment');
}
}
/**
* Generate signed URL for file download
*/
private async generateSignedUrl(s3Key: string): Promise<string> {
const getCommand = new GetObjectCommand({
Bucket: this.bucketName,
Key: s3Key,
});
return getSignedUrl(this.s3Client, getCommand, {
expiresIn: this.SIGNED_URL_EXPIRY,
});
}
/**
* Sanitize filename for S3 storage
*/
private sanitizeFileName(fileName: string): string {
return fileName.replace(/[^a-zA-Z0-9.-]/g, '_');
}
/**
* Sanitize header value for S3 user metadata (x-amz-meta-*) to avoid invalid characters
* - Remove control characters (\x00-\x1F, \x7F)
* - Replace non-ASCII with '_'
* - Trim whitespace
*/
private sanitizeHeaderValue(value: string): string {
const withoutControls = value.replace(/[\x00-\x1F\x7F]/g, '');
const asciiOnly = withoutControls.replace(/[^\x20-\x7E]/g, '_');
return asciiOnly.trim();
}
/**
* Map MIME type to AttachmentType enum
*/
private mapFileTypeToAttachmentType(fileType: string): AttachmentType {
const type = fileType.split('/')[0];
switch (type) {
case 'image':
return AttachmentType.image;
case 'video':
return AttachmentType.video;
case 'audio':
return AttachmentType.audio;
case 'application':
case 'text':
return AttachmentType.document;
default:
return AttachmentType.other;
}
}
}