Feat: Implement S3 object deletion when deleting an asset record

This commit is contained in:
kenilkb 2026-07-16 10:00:34 +05:30
parent 6d694efeb0
commit 4da31c52c0

View File

@ -1,6 +1,8 @@
import prisma from '../utils/db';
import crypto from 'crypto';
import { MailService } from './mail.service';
import { s3Client, BUCKET_NAME } from '../utils/s3';
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
export class AssetService {
private mailService = new MailService();
@ -377,6 +379,19 @@ export class AssetService {
}
public async deleteAsset(id: string) {
const asset = await prisma.asset.findUnique({ where: { id } });
if (asset && asset.url.startsWith('/uploads/')) {
const filename = asset.url.replace('/uploads/', '');
try {
await s3Client.send(new DeleteObjectCommand({
Bucket: BUCKET_NAME,
Key: filename,
}));
console.log(`[S3] Deleted file: ${filename}`);
} catch (err) {
console.error(`[S3] Failed to delete file ${filename}:`, err);
}
}
return await prisma.asset.delete({ where: { id } });
}