add delete feature
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user