diff --git a/src/upload/upload.service.ts b/src/upload/upload.service.ts index 95d18eb..5626367 100644 --- a/src/upload/upload.service.ts +++ b/src/upload/upload.service.ts @@ -56,6 +56,20 @@ export class UploadService { return parts.length > 1 ? parts.pop()!.toLowerCase() : ''; } + /** + * Get Cache-Control header value based on folder type + * Since file IDs change on re-upload, we can use long cache times + */ + private getCacheControl(folder: UploadFolder): string { + const cacheDurations: Record = { + [UploadFolder.AVATARS]: 30 * 24 * 60 * 60, // 30 days + [UploadFolder.DOCUMENTS]: 7 * 24 * 60 * 60, // 7 days + [UploadFolder.PROPERTIES]: 30 * 24 * 60 * 60, // 30 days + [UploadFolder.VERIFICATION]: 1 * 24 * 60 * 60, // 1 day + }; + return `public, max-age=${cacheDurations[folder] || 86400}`; + } + private validateContentType(contentType: string, folder: UploadFolder): void { const allowedTypes: Record = { [UploadFolder.AVATARS]: ['image/jpeg', 'image/png', 'image/webp'], @@ -100,6 +114,7 @@ export class UploadService { Bucket: this.bucket, Key: key, ContentType: dto.contentType, + CacheControl: this.getCacheControl(dto.folder), }); const uploadUrl = await getSignedUrl(this.s3Client, command, { @@ -165,6 +180,7 @@ export class UploadService { Key: key, Body: buffer, ContentType: contentType, + CacheControl: this.getCacheControl(folder), }); await this.s3Client.send(command); @@ -198,6 +214,7 @@ export class UploadService { Bucket: this.bucket, Key: key, ContentType: contentType, + CacheControl: this.getCacheControl(UploadFolder.AVATARS), }); const uploadUrl = await getSignedUrl(this.s3Client, command, { @@ -234,6 +251,7 @@ export class UploadService { Bucket: this.bucket, Key: key, ContentType: contentType, + CacheControl: this.getCacheControl(UploadFolder.AVATARS), }); const uploadUrl = await getSignedUrl(this.s3Client, command, {