82 lines
3.4 KiB
TypeScript
82 lines
3.4 KiB
TypeScript
|
import Controller from "wms-core/Controller";
|
||
|
import {NextFunction, Request, Response} from "express";
|
||
|
import URLRedirect from "../models/URLRedirect";
|
||
|
import {REQUIRE_AUTH_MIDDLEWARE, REQUIRE_REQUEST_AUTH_MIDDLEWARE} from "wms-core/auth/AuthComponent";
|
||
|
import generateSlug from "../SlugGenerator";
|
||
|
import {BadRequestError} from "wms-core/HttpError";
|
||
|
|
||
|
export default class URLRedirectController extends Controller {
|
||
|
routes(): void {
|
||
|
this.get('/url/shorten', this.getURLShortener, 'url-shortener', REQUIRE_AUTH_MIDDLEWARE);
|
||
|
this.get('/url/shorten/script', this.downloadLinuxScript, 'url-linux-script');
|
||
|
this.post('/url/shorten', this.addURLFrontend, 'shorten-url', REQUIRE_AUTH_MIDDLEWARE);
|
||
|
this.get('/urls/:page([0-9]+)?', this.getURLRedirectManager, 'url-manager', REQUIRE_AUTH_MIDDLEWARE);
|
||
|
|
||
|
this.post('/', this.addURL, 'post-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE);
|
||
|
this.delete('/:slug', this.deleteURL, 'delete-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE);
|
||
|
this.get('/:slug', this.getURLRedirect, 'get-url');
|
||
|
this.put('/:slug', this.addURL, 'put-url', REQUIRE_REQUEST_AUTH_MIDDLEWARE);
|
||
|
}
|
||
|
|
||
|
protected async getURLShortener(req: Request, res: Response): Promise<void> {
|
||
|
res.render('url-shortener');
|
||
|
}
|
||
|
|
||
|
protected async downloadLinuxScript(req: Request, res: Response): Promise<void> {
|
||
|
res.download('assets/files/shorten_url.sh', 'shorten_url.sh');
|
||
|
}
|
||
|
|
||
|
protected async getURLRedirectManager(req: Request, res: Response): Promise<void> {
|
||
|
res.render('url-manager', {
|
||
|
urls: await URLRedirect.paginateForUser(req, 100, req.models.user!.id!),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
protected async getURLRedirect(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||
|
const url = await URLRedirect.getBySlug(req.params.slug);
|
||
|
if (!url) return next();
|
||
|
|
||
|
res.redirect(url.target_url, 301);
|
||
|
}
|
||
|
|
||
|
protected async deleteURL(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||
|
const urlRedirect = await URLRedirect.getBySlug(req.params.slug);
|
||
|
if (!urlRedirect) return next();
|
||
|
|
||
|
throw new BadRequestError(
|
||
|
'Deleting url redirects is disabled for security reasons.',
|
||
|
'If you still want to disable the redirection, please contact us via email.',
|
||
|
req.url
|
||
|
);
|
||
|
}
|
||
|
|
||
|
protected async addURLFrontend(req: Request, res: Response, next: NextFunction): Promise<void> {
|
||
|
req.body.type = 'url';
|
||
|
await this.addURL(req, res, next, req.body.autogen_url === undefined && req.body.slug ? req.body.slug : await generateSlug(10));
|
||
|
}
|
||
|
|
||
|
protected async addURL(req: Request, res: Response, next: NextFunction, slug?: string): Promise<void> {
|
||
|
if (req.body.type !== 'url') return next();
|
||
|
|
||
|
slug = slug || req.params.slug || req.body.slug || await generateSlug(10);
|
||
|
const urlRedirect = new URLRedirect({
|
||
|
user_id: req.models.user!.id,
|
||
|
slug: slug,
|
||
|
target_url: req.body.target_url,
|
||
|
});
|
||
|
|
||
|
await urlRedirect.save();
|
||
|
|
||
|
res.format({
|
||
|
json: () => res.json({
|
||
|
url: urlRedirect.getURL(),
|
||
|
}),
|
||
|
text: () => res.send(urlRedirect.getURL()),
|
||
|
html: () => {
|
||
|
req.flash('success', 'URL shortened successfully!');
|
||
|
req.flash('url', urlRedirect.getURL());
|
||
|
res.redirectBack('/');
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
}
|