Skip to content

Commit

Permalink
Bad request error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Feb 8, 2024
1 parent 3abf26b commit 24ce0ee
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/endpoints/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ export default (app: Application) => {
}
apiMessage(req.path, 'User is trying to delete a file');
const fileName = req.params.name;
const fileNamePattern = /^[a-zA-Z0-9_-]+\.[a-zA-Z0-9]+$/;
const fileNamePattern = /^[a-zA-Z0-9]+\.(jpg|jpeg|png|mp4)$/;
if (!fileNamePattern.test(fileName)) {
return res.status(400).json({ error: 'Invalid file name' });
return res
.status(400)
.json({
error:
'Invalid file name. Please only use English Alphabet characters, 0-9. .jpg .jpeg .png .mp4 are the only supported file types',
});
}
const dir = resolve(dirname(''), 'src/files');
if (!existsSync(dir)) {
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default (app: Application) => {
const fileName = req.params.name;
if (fileName === 'favicon.ico') return;
apiMessage(req.path, `User is trying to get a file - ${fileName}`);
const fileNamePattern = /^[a-zA-Z0-9_-]+\.[a-zA-Z0-9]+$/;
const fileNamePattern = /^[a-zA-Z0-9]+\.(jpg|jpeg|png|mp4)$/;
if (!fileNamePattern.test(fileName)) {
return res.status(400).json({ error: 'Invalid file name' });
return res.status(400).render('pages/badName');
}
const dir = resolve(dirname(''), 'src/files');
if (!existsSync(dir)) {
Expand All @@ -22,7 +22,7 @@ export default (app: Application) => {
const filePath = resolve(dir, fileName);
if (!existsSync(filePath)) {
errorMessage(`File ${fileName} doesn't exists`);
return res.status(400).json({ success: false, message: `File ${fileName} doesn't exist` });
return res.status(400).render('pages/missingFile');
}
apiMessage(req.path, `File ${fileName} found`);
const stats = statSync(filePath);
Expand Down
9 changes: 7 additions & 2 deletions src/endpoints/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ export default (app: Application) => {
const fileName = req.params.name;
if (fileName === 'favicon.ico') return;
apiMessage(req.path, `User is trying to get a file - ${fileName}`);
const fileNamePattern = /^[a-zA-Z0-9_-]+\.[a-zA-Z0-9]+$/;
const fileNamePattern = /^[a-zA-Z0-9]+\.(jpg|jpeg|png|mp4)$/;
if (!fileNamePattern.test(fileName)) {
return res.status(400).json({ error: 'Invalid file name' });
return res
.status(400)
.json({
error:
'Invalid file name. Please only use English Alphabet characters, 0-9. .jpg .jpeg .png .mp4 are the only supported file types',
});
}
const dir = resolve(dirname(''), 'src/files');
if (!existsSync(dir)) {
Expand Down
9 changes: 7 additions & 2 deletions src/endpoints/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ export default (app: Application) => {
return res.status(400).send({ success: false, message: 'No file provided' });
}
const fileName = nameHide ? `${generateID(10)}.${req.params.name.split('.')[1]}` : req.params.name;
const fileNamePattern = /^[a-zA-Z0-9_-]+\.[a-zA-Z0-9]+$/;
const fileNamePattern = /^[a-zA-Z0-9]+\.(jpg|jpeg|png|mp4)$/;
if (!fileNamePattern.test(fileName)) {
return res.status(400).json({ error: 'Invalid file name' });
return res
.status(400)
.json({
error:
'Invalid file name. Please only use English Alphabet characters, 0-9. .jpg .jpeg .png .mp4 are the only supported file types',
});
}
if (file.size > maxFileSize) {
errorMessage('File is too big');
Expand Down
6 changes: 6 additions & 0 deletions src/public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ video {
.tooltip:hover .tooltiptext {
visibility: visible;
}

.warningMsg {
color: red;
font-size: 1.5rem;
text-decoration: underline;
}
27 changes: 27 additions & 0 deletions src/views/pages/badName.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="../files/favicon.ico">
<link rel="stylesheet" href="../css/index.css">
<meta name="robots" content="noindex">
<title>Missing File</title>
<meta charset="UTF-8">
</head>

<body>
<main>
<div class="container">
<img src="https://images.unsplash.com/photo-1582845512747-e42001c95638" alt="Image of a rubber duck">
<div class="text">
<p class="warningMsg">Invalid file name.</p>
<p class="warningMsg">Please only use English Alphabet characters, 0-9.</p>
<p class="warningMsg">.jpg .jpeg .png .mp4 are the only supported file types </p>
<p>Heres a duck instead</p>
</div>
</div>
</main>
</body>

</html>
25 changes: 25 additions & 0 deletions src/views/pages/missingFile.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="../files/favicon.ico">
<link rel="stylesheet" href="../css/index.css">
<meta name="robots" content="noindex">
<title>Missing File</title>
<meta charset="UTF-8">
</head>

<body>
<main>
<div class="container">
<img src="https://images.unsplash.com/photo-1582845512747-e42001c95638" alt="Image of a rubber duck">
<div class="text">
<p class="warningMsg">The File you requested doesn't exist</p>
<p>Heres a duck instead</p>
</div>
</div>
</main>
</body>

</html>

0 comments on commit 24ce0ee

Please sign in to comment.