From 3abf26b937c3a5bb08acf59cb4bfea3472175115 Mon Sep 17 00:00:00 2001
From: Kath <55346310+Kathund@users.noreply.github.com>
Date: Thu, 8 Feb 2024 08:58:10 +0800
Subject: [PATCH] Added a max file size config option
---
README.md | 24 +++++++++++++++++++++++-
config.example.json | 5 +++--
index.ts | 9 ++++++---
src/endpoints/save.ts | 8 ++++++--
src/public/css/index.css | 26 +++++++++++++++++++-------
src/views/pages/index.ejs | 31 ++++++++++++++++---------------
6 files changed, 73 insertions(+), 30 deletions(-)
diff --git a/README.md b/README.md
index 1387c78..45d2184 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@ git clone https://github.com/Kathund/ShareX-API.git
npm install
```
-3. Setup Config by renaming the `config.example.json` to `config.json` and filling in the required information.
+3. Setup Config by renaming the `config.example.json` to `config.json` and filling in the required information. (More information on the configuration options can be found [here](#configuration-options))
4. Start the API by running the following command:
```bash
@@ -51,3 +51,25 @@ To setup your new ShareX API and make it work with ShareX you will need to do th
3. From there navigate to folder where you have downloaded the config file and open it with ShareX.
4. Your will be promoted if you want ot make `ShareX-Uploader` your default uploader, click yes.
5. You are now ready to use the API with ShareX.
+
+## configuration Options
+
+### Port
+
+The `port` option is the port that the API is running on. This is not required to be changed unless you are running someone else on the default `3000` port.
+
+### URL
+
+The `url` option is the url that the API is running on. For example if the API is running on `https://example.com` then the `url` option would be `https://example.com`. This needs to be set so the API knows what url to use when generating the ShareX config file and returning the file url when the user uploads a file.
+
+### Name Hide
+
+The `nameHide` option hides the original file name and generates a random name for the file. This file name is 10 characters long and is a random string of all the letters in the alphabet (upper and lower) and 0-9. This is useful for hiding the original file name and making it harder for people to guess the file name.
+
+### Key
+
+The `key` option is the key that is used to authenticate the user when uploading a file. This must be set to a random string of characters and should be kept secret. This is used to authenticate the user when uploading a file to the API. This is useful for stopping people from uploading files to your API without your permission.
+
+### Max File Size
+
+The `maxFileSize` option is the maximum file size that the API will accept. This is useful for stopping people from uploading large files to your API and using up all of your server space. This is set in bytes and the default is `104857600 Bytes` (100mb) due to Cloudflare tunnel's max `free` plan only supports 100mb file size.
diff --git a/config.example.json b/config.example.json
index 2df4419..c9437c1 100644
--- a/config.example.json
+++ b/config.example.json
@@ -1,6 +1,7 @@
{
"PORT": 3000,
- "nameHide": true,
"url": "URL_OF_THE_CDN",
- "key": "API_KEY"
+ "nameHide": true,
+ "key": "API_KEY",
+ "maxFileSize": 104857600
}
diff --git a/index.ts b/index.ts
index b54e334..9422db8 100644
--- a/index.ts
+++ b/index.ts
@@ -1,8 +1,8 @@
-import { errorMessage, otherMessage } from './src/logger';
+import { errorMessage, otherMessage, warnMessage } from './src/logger';
import { loadEndpoints } from './src/functions';
-import fileUpload from 'express-fileupload';
+import { PORT, url, key } from './config.json';
import { existsSync, mkdirSync } from 'fs';
-import { PORT, url } from './config.json';
+import fileUpload from 'express-fileupload';
import express from 'express';
if (!existsSync('./src/files')) {
@@ -25,6 +25,9 @@ try {
}
app.listen(PORT, () => {
otherMessage(`Server started on port ${PORT} @ http://localhost:${PORT}`);
+ if (key === 'API_KEY') {
+ warnMessage('The API Key is still the default key! It is recommended to change this in the config.json file.');
+ }
otherMessage(`Config is available to be generated @ ${url}/config/generate?key=${global.generateKey}`);
setTimeout(() => {
if (global.generateKey === null) return;
diff --git a/src/endpoints/save.ts b/src/endpoints/save.ts
index ee3c506..ac55ea0 100644
--- a/src/endpoints/save.ts
+++ b/src/endpoints/save.ts
@@ -1,9 +1,9 @@
+import { url, key, nameHide, maxFileSize } from '../../config.json';
import { Application, Request, Response } from 'express';
-import { url, key, nameHide } from '../../config.json';
import { apiMessage, errorMessage } from '../logger';
import { existsSync, mkdirSync } from 'fs';
-import { resolve, dirname } from 'path';
import { generateID } from '../functions';
+import { resolve, dirname } from 'path';
export default (app: Application) => {
app.post('/save/:name', async (req: Request, res: Response) => {
@@ -24,6 +24,10 @@ export default (app: Application) => {
if (!fileNamePattern.test(fileName)) {
return res.status(400).json({ error: 'Invalid file name' });
}
+ if (file.size > maxFileSize) {
+ errorMessage('File is too big');
+ return res.status(400).json({ success: false, message: 'File is too big' });
+ }
const dir = resolve(dirname(''), 'src/files');
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
diff --git a/src/public/css/index.css b/src/public/css/index.css
index c2054dc..6d1b3d9 100644
--- a/src/public/css/index.css
+++ b/src/public/css/index.css
@@ -21,6 +21,7 @@ body {
background-color: #dcdcdc;
justify-content: center;
align-items: center;
+ font-size: 1.5rem;
color: #343434;
height: 100vh;
display: flex;
@@ -29,18 +30,27 @@ body {
img,
video {
- filter: drop-shadow(#00000080 16px 16px 18px);
border-radius: 16px 16px 0 0;
+ max-height: 75vh;
max-width: 75vw;
height: auto;
+ width: auto;
}
-.text {
- filter: drop-shadow(#00000080 16px 16px 18px);
- border-radius: 0 0 16px 16px;
+.container {
+ filter: drop-shadow(#000000bf 20px 20px 30px);
background-color: #bfb9b9;
+ border-radius: 16px;
+}
+
+.text {
text-align: center;
+ max-height: 75vh;
+ max-width: 75vw;
padding: 20px;
+ height: auto;
+ width: auto;
+ z-index: 2;
}
@media (prefers-color-scheme: dark) {
@@ -49,7 +59,8 @@ video {
color: #dcdcdc;
}
- .text {
+ .container {
+ filter: drop-shadow(#ffffff1a 20px 20px 30px);
background-color: #363636;
}
}
@@ -66,8 +77,9 @@ video {
margin-left: -60px;
border-radius: 6px;
position: absolute;
- padding: 5px 0;
- color: #fff;
+ color: #dcdcdc;
+ padding: 0.25rem;
+ font-size: 1rem;
bottom: 100%;
width: 160px;
z-index: 1;
diff --git a/src/views/pages/index.ejs b/src/views/pages/index.ejs
index 7c734d5..63a8fe6 100644
--- a/src/views/pages/index.ejs
+++ b/src/views/pages/index.ejs
@@ -17,23 +17,24 @@