diff --git a/src/controller/FileController.ts b/src/controller/FileController.ts index a74d68f..0dde5cc 100644 --- a/src/controller/FileController.ts +++ b/src/controller/FileController.ts @@ -1,6 +1,7 @@ import multer from 'multer'; import path from 'path'; import {v4 as uuidv4} from 'uuid'; +import fs from 'fs'; const storage = multer.diskStorage({ destination: path.join(__dirname, '../../public/uploads'), @@ -10,3 +11,12 @@ const storage = multer.diskStorage({ }); export const upload = multer({ storage }); + +export const deleteFile = (filename: string) => { + const filePath = path.join(__dirname, '../../public/uploads', filename); + fs.unlink(filePath, (err) => { + if (err) { + console.log(err); + } + }); +} \ No newline at end of file diff --git a/src/controller/PostController.ts b/src/controller/PostController.ts index 28b12af..3962ad7 100644 --- a/src/controller/PostController.ts +++ b/src/controller/PostController.ts @@ -2,6 +2,7 @@ import { PrismaClient, Post as PrismaPost, Like } from '@prisma/client'; import { Post } from '@/models/PostModel'; import { exclude } from '@/lib/utils'; import { getUserById } from './UserController'; +import { deleteFile } from './FileController'; const prisma = new PrismaClient(); @@ -95,6 +96,9 @@ const deletePost = async (id: number, userId: number): Promise