add delete feature

This commit is contained in:
Rutra
2026-05-04 15:16:07 +02:00
parent ac81ac530f
commit 9c51891828
6 changed files with 37 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
import app from '@adonisjs/core/services/app';
import { readdirSync, existsSync, statSync } from 'node:fs';
import { readdirSync, existsSync, statSync, unlinkSync } from 'node:fs';
import { extname, join } from 'node:path';
const IMAGE_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp']);
const VIDEO_EXTENSIONS = new Set(['.mp4', '.mov', '.avi', '.webm']);
@@ -46,5 +46,17 @@ export default class MediaController {
}
return response.download(filePath);
}
async destroy({ params, response }) {
const { filename } = params;
if (!/^[\w\-. ]+$/.test(filename)) {
return response.status(400).json({ success: false, message: 'Invalid filename.' });
}
const filePath = join(app.publicPath('uploads'), filename);
if (!existsSync(filePath)) {
return response.status(404).json({ success: false, message: 'File not found.' });
}
unlinkSync(filePath);
return response.ok({ success: true });
}
}
//# sourceMappingURL=media_controller.js.map