From 996b8c15723945c6815c4df6f430be4500e71d91 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Sat, 17 Oct 2020 09:41:52 -0500 Subject: [PATCH] Small QOL tweaks --- backend/src/app/controllers/img/putImageHandler.ts | 7 +++---- backend/src/app/controllers/txt/getTxtHandler.ts | 2 +- backend/src/app/controllers/url/putUrlHandler.ts | 10 +++++++++- frontend/src/components/TextUpload.jsx | 2 +- frontend/src/components/UrlShortener.jsx | 12 ++++++++++++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/backend/src/app/controllers/img/putImageHandler.ts b/backend/src/app/controllers/img/putImageHandler.ts index 1959681..f83fc1e 100644 --- a/backend/src/app/controllers/img/putImageHandler.ts +++ b/backend/src/app/controllers/img/putImageHandler.ts @@ -1,7 +1,6 @@ import { Request, Response } from 'express' -import Images from '../../db/img.schema' import fs from 'fs' -import path from 'path' +import Images from '../../db/img.schema' export default async (req: Request, res: Response) => { try { @@ -15,7 +14,7 @@ export default async (req: Request, res: Response) => { /* If some other IP uploads, we want to add them too */ img.uploaders.push(req.clientIp as string) await img.save() - return res.status(200).send(img._id); + return res.status(200).send(img._id) } } @@ -27,7 +26,7 @@ export default async (req: Request, res: Response) => { await img.save() - return res.status(200).send(img._id); + return res.status(200).send(img._id) } catch (ex) { console.error(ex) return res.status(500).send({ diff --git a/backend/src/app/controllers/txt/getTxtHandler.ts b/backend/src/app/controllers/txt/getTxtHandler.ts index 0610047..9b1297b 100644 --- a/backend/src/app/controllers/txt/getTxtHandler.ts +++ b/backend/src/app/controllers/txt/getTxtHandler.ts @@ -10,7 +10,7 @@ export default async (req: Request, res: Response) => { // Check for an expired link if (moment().isAfter(moment(paste.expiresAt))) { await Pastes.findByIdAndDelete(paste._id) - return res.status(404).send("PASTE_EXPIRED") + return res.status(404).send("PASTE EXPIRED") } return res.json({ diff --git a/backend/src/app/controllers/url/putUrlHandler.ts b/backend/src/app/controllers/url/putUrlHandler.ts index 8628952..0286439 100644 --- a/backend/src/app/controllers/url/putUrlHandler.ts +++ b/backend/src/app/controllers/url/putUrlHandler.ts @@ -1,13 +1,21 @@ import { Request, Response } from 'express' import URLs from '../../db/url.schema' +const isUrlValid = (url: string) => { + return url.startsWith("http") || url.startsWith("www") +} + export default async (req: Request, res: Response) => { try { + if (!isUrlValid(req.body.url)) { + return res.status(422).send("INVALID URL") + } + const url = new URLs() url.url = req.body.url await url.save() - return res.status(200).send(url._id); + return res.status(200).send(url._id) } catch (ex) { console.error(ex) return res.status(500).send({ diff --git a/frontend/src/components/TextUpload.jsx b/frontend/src/components/TextUpload.jsx index 9c7723e..3d31224 100644 --- a/frontend/src/components/TextUpload.jsx +++ b/frontend/src/components/TextUpload.jsx @@ -47,7 +47,7 @@ export default () => { if (renderState.notFound) { return ( -

Paste Not Found

+

Paste Not Found Or Expired

); } diff --git a/frontend/src/components/UrlShortener.jsx b/frontend/src/components/UrlShortener.jsx index f041566..59796bb 100644 --- a/frontend/src/components/UrlShortener.jsx +++ b/frontend/src/components/UrlShortener.jsx @@ -10,6 +10,7 @@ export default () => { url: null, loading: false, result: null, + error: false, }); const submit = (evt) => { @@ -21,7 +22,12 @@ export default () => { ...state, result: `http://${window.location.host}/u/${data}`.replace('5000', '3000'), loading: false, + error: false, }); + }).catch((ex) => { + if (ex.response && ex.response.data === 'INVALID URL') { + setState({ ...state, error: true }); + } }); } }; @@ -47,6 +53,12 @@ export default () => { + {state.error && ( + + Please enter a valid URL + + )} +