Slightly improve handling of file deletions
This commit is contained in:
parent
d15a4172ab
commit
f202e01a5a
@ -9,6 +9,7 @@ import AuthToken from "../models/AuthToken";
|
|||||||
import {IncomingForm} from "formidable";
|
import {IncomingForm} from "formidable";
|
||||||
import {FILE_UPLOAD_MIDDLEWARE} from "wms-core/components/ExpressAppComponent";
|
import {FILE_UPLOAD_MIDDLEWARE} from "wms-core/components/ExpressAppComponent";
|
||||||
import generateSlug from "../SlugGenerator";
|
import generateSlug from "../SlugGenerator";
|
||||||
|
import Logger from "wms-core/Logger";
|
||||||
|
|
||||||
|
|
||||||
export default class FileController extends Controller {
|
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.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.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.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> {
|
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;
|
const slug = req.params.slug;
|
||||||
if (!slug) throw new BadRequestError('Cannot delete nothing.', 'Please provide a slug.', req.url);
|
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) {
|
switch (file.storage_type) {
|
||||||
case 'local':
|
case 'local':
|
||||||
await file.delete();
|
await this.deleteFile(file);
|
||||||
fs.unlinkSync(file.storage_path);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ServerError(`This file cannot be deleted. Deletion protocol for ${file.storage_type} storage type not implemented.`);
|
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})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,15 +6,13 @@ import {REQUIRE_REQUEST_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent";
|
|||||||
import URLRedirect from "../models/URLRedirect";
|
import URLRedirect from "../models/URLRedirect";
|
||||||
import URLRedirectController from "./URLRedirectController";
|
import URLRedirectController from "./URLRedirectController";
|
||||||
import FileModel from "../models/FileModel";
|
import FileModel from "../models/FileModel";
|
||||||
import fs from "fs";
|
|
||||||
import Logger from "wms-core/Logger";
|
|
||||||
import generateSlug from "../SlugGenerator";
|
import generateSlug from "../SlugGenerator";
|
||||||
import FileController, {FILE_UPLOAD_FORM_MIDDLEWARE} from "./FileController";
|
import FileController, {FILE_UPLOAD_FORM_MIDDLEWARE} from "./FileController";
|
||||||
|
|
||||||
export default class LinkController extends Controller {
|
export default class LinkController extends Controller {
|
||||||
routes(): void {
|
routes(): void {
|
||||||
this.post('/', this.postFile, 'post-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE);
|
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.get('/:slug', this.downloadFile, 'get-file');
|
||||||
this.put('/:slug', this.putFile, 'put-file', REQUIRE_REQUEST_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE);
|
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> {
|
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);
|
const file = await FileModel.getBySlug(req.params.slug);
|
||||||
if (!file) return next();
|
if (!file) return next();
|
||||||
if (file.shouldBeDeleted()) {
|
if (file.shouldBeDeleted()) {
|
||||||
await fs.unlinkSync(file.storage_path);
|
await FileController.deleteFile(file);
|
||||||
await file.delete();
|
|
||||||
Logger.info('Deleted', file.storage_path, `(${file.real_name})`);
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,9 +39,13 @@
|
|||||||
{% set expires_at = file.getExpirationDate() %}
|
{% set expires_at = file.getExpirationDate() %}
|
||||||
<td>{% if expires_at %}{{ expires_at.toISOString() }}{% else %}Never{% endif %}</td>
|
<td>{% if expires_at %}{{ expires_at.toISOString() }}{% else %}Never{% endif %}</td>
|
||||||
<td>
|
<td>
|
||||||
<form action="{{ route('delete-file-frontend', file.slug) }}" method="post">
|
{% if file.shouldBeDeleted() %}
|
||||||
<button class="button danger"><i data-feather="trash"></i> Delete</button>
|
Pending deletion
|
||||||
</form>
|
{% 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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
Loading…
Reference in New Issue
Block a user