Slightly improve handling of file deletions

This commit is contained in:
Alice Gaudon 2020-07-09 14:15:46 +02:00
parent d15a4172ab
commit f202e01a5a
3 changed files with 20 additions and 13 deletions

View File

@ -9,6 +9,7 @@ import AuthToken from "../models/AuthToken";
import {IncomingForm} from "formidable";
import {FILE_UPLOAD_MIDDLEWARE} from "wms-core/components/ExpressAppComponent";
import generateSlug from "../SlugGenerator";
import Logger from "wms-core/Logger";
export default class FileController extends Controller {
@ -17,7 +18,7 @@ export default class FileController extends Controller {
this.get('/files/upload/script', this.downloadLinuxScript, 'file-linux-script');
this.post('/files/post', this.postFileFrontend, 'post-file-frontend', REQUIRE_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE);
this.get('/files/:page([0-9]+)?', this.getFileManager, 'file-manager', REQUIRE_AUTH_MIDDLEWARE);
this.post('/files/delete/:slug', FileController.deleteFile, 'delete-file-frontend', REQUIRE_AUTH_MIDDLEWARE);
this.post('/files/delete/:slug', FileController.deleteFileRoute, 'delete-file-frontend', REQUIRE_AUTH_MIDDLEWARE);
}
protected async getFileUploader(req: Request, res: Response): Promise<void> {
@ -86,7 +87,7 @@ export default class FileController extends Controller {
});
}
public static async deleteFile(req: Request, res: Response, next: NextFunction): Promise<void> {
public static async deleteFileRoute(req: Request, res: Response, next: NextFunction): Promise<void> {
const slug = req.params.slug;
if (!slug) throw new BadRequestError('Cannot delete nothing.', 'Please provide a slug.', req.url);
@ -96,8 +97,7 @@ export default class FileController extends Controller {
switch (file.storage_type) {
case 'local':
await file.delete();
fs.unlinkSync(file.storage_path);
await this.deleteFile(file);
break;
default:
throw new ServerError(`This file cannot be deleted. Deletion protocol for ${file.storage_type} storage type not implemented.`);
@ -114,6 +114,12 @@ export default class FileController extends Controller {
},
});
}
public static async deleteFile(file: FileModel): Promise<void> {
fs.unlinkSync(file.storage_path);
await file.delete();
Logger.info('Deleted', file.storage_path, `(${file.real_name})`);
}
}

View File

@ -6,15 +6,13 @@ import {REQUIRE_REQUEST_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent";
import URLRedirect from "../models/URLRedirect";
import URLRedirectController from "./URLRedirectController";
import FileModel from "../models/FileModel";
import fs from "fs";
import Logger from "wms-core/Logger";
import generateSlug from "../SlugGenerator";
import FileController, {FILE_UPLOAD_FORM_MIDDLEWARE} from "./FileController";
export default class LinkController extends Controller {
routes(): void {
this.post('/', this.postFile, 'post-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE);
this.delete('/:slug', FileController.deleteFile, 'delete-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE);
this.delete('/:slug', FileController.deleteFileRoute, 'delete-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE);
this.get('/:slug', this.downloadFile, 'get-file');
this.put('/:slug', this.putFile, 'put-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE);
@ -27,12 +25,11 @@ export default class LinkController extends Controller {
}
protected async downloadFile(req: Request, res: Response, next: NextFunction): Promise<void> {
console.log('get file', req.params.slug)
const file = await FileModel.getBySlug(req.params.slug);
if (!file) return next();
if (file.shouldBeDeleted()) {
await fs.unlinkSync(file.storage_path);
await file.delete();
Logger.info('Deleted', file.storage_path, `(${file.real_name})`);
await FileController.deleteFile(file);
return next();
}

View File

@ -39,9 +39,13 @@
{% set expires_at = file.getExpirationDate() %}
<td>{% if expires_at %}{{ expires_at.toISOString() }}{% else %}Never{% endif %}</td>
<td>
<form action="{{ route('delete-file-frontend', file.slug) }}" method="post">
<button class="button danger"><i data-feather="trash"></i> Delete</button>
</form>
{% if file.shouldBeDeleted() %}
Pending deletion
{% else %}
<form action="{{ route('delete-file-frontend', file.slug) }}" method="post">
<button class="button danger"><i data-feather="trash"></i> Delete</button>
</form>
{% endif %}
</td>
</tr>
{% endfor %}