Skip to content

Commit

Permalink
Add generate endpoint allowing use to generate a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Feb 7, 2024
1 parent a4bc90d commit 1b68a71
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 38 deletions.
33 changes: 5 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,8 @@ npm start

To setup your new ShareX API and make it work with ShareX you will need to do the following:

1. Open [ShareX](https://getsharex.com) and then on the right go to `Destinations` and then click on `Custom uploader settings`.
![Going to the custom uploader settings](.github/readme/assets/ShareX_Setup_CustomUploader_Settings.png)
2. Copy the default config from the code block below

```json
{
"Version": "15.0.0",
"Name": "ShareX-Uploader",
"DestinationType": "ImageUploader",
"RequestMethod": "POST",
"RequestURL": "URL_OF_THE_CDN/save/{filename}",
"Headers": {
"api-key": "API_KEY"
},
"Body": "MultipartFormData",
"FileFormName": "file",
"URL": "{json:url}",
"DeletionURL": "{json:delete}",
"ErrorMessage": "{json:message}"
}
```

3. Go back to ShareX and click `Import` then select `From clipboard` and then click `OK`.
![Importing Default Config](.github/readme/assets/ShareX_Setup_Importing_Config.png)
4. Change the `Request URL` and the `API-KEY` to the correct values that are set in your `config.json` file.
![Changing Default Config](.github/readme/assets/ShareX_Setup_Changing_Config.png)
5. Under `Image Uploader` in the bottom right make sure its set to `ShareX-Uploader` and then click `Test` to make sure it works.
![Testing Image Uploading](.github/readme/assets/ShareX_Setup_Testing.png)
1. Have the api running
2. In your console you will see `Config is available to be generated @ <URL>`, After going to that url you will be promoted to save a file called `ShareX-API-Config.sxcu` This file is a ShareX Custom Uploader Configuration file.
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.
9 changes: 8 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { errorMessage, otherMessage } from './src/logger';
import { loadEndpoints } from './src/functions';
import fileUpload from 'express-fileupload';
import { existsSync, mkdirSync } from 'fs';
import { PORT } from './config.json';
import { PORT, url } from './config.json';
import express from 'express';

if (!existsSync('./src/files')) {
Expand All @@ -16,6 +16,7 @@ try {
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use(fileUpload());
global.generateKey = crypto.randomUUID();
const result = await loadEndpoints(app);
if (result !== undefined) {
otherMessage(`Loaded ${result} endpoints`);
Expand All @@ -24,6 +25,12 @@ try {
}
app.listen(PORT, () => {
otherMessage(`Server started on port ${PORT} @ http://localhost:${PORT}`);
otherMessage(`Config is available to be generated @ ${url}/config/generate?key=${global.generateKey}`);
setTimeout(() => {
if (global.generateKey === null) return;
global.generateKey = null;
otherMessage(`Config is no longer available to be generated. Please restart to generate a new key.`);
}, 300000);
});
})();
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolve, dirname } from 'path';
import { url } from '../../config.json';

export default (app: Application) => {
app.get('/:name', async (req: Request, res: Response) => {
app.get('/view/:name', async (req: Request, res: Response) => {
try {
const fileName = req.params.name;
if (fileName === 'favicon.ico') return;
Expand Down
42 changes: 42 additions & 0 deletions src/endpoints/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Application, Request, Response } from 'express';
import { apiMessage, errorMessage } from '../logger';
import { url, key } from '../../config.json';

export default (app: Application) => {
app.get('/config/generate', async (req: Request, res: Response) => {
try {
if (global.generateKey === null) {
return res.status(500).send({ success: false, message: 'You have already generated a config' });
}
const genKey = req.query.key;
if (genKey !== global.generateKey) {
errorMessage('Invalid Generate key provided');
return res.status(400).send({ success: false, message: 'Invalid Generate key provided' });
}
apiMessage(req.path, 'User is generating a config file');
apiMessage(req.path, `Config file has been generated`);
global.generateKey = null;
return res
.status(200)
.attachment(`ShareX-API-Config.sxcu`)
.send({
Version: '15.0.0',
Name: 'ShareX-Uploader',
DestinationType: 'ImageUploader',
RequestMethod: 'POST',
RequestURL: `${url}/save/{filename}`,
Headers: {
'api-key': key,
},
Body: 'MultipartFormData',
FileFormName: 'file',
URL: '{json:url}',
DeletionURL: '{json:delete}',
ErrorMessage: '{json:message}',
});
} catch (err) {
errorMessage(err as string);
return res.status(500).send({ success: false, message: 'Internal server error' });
}
});
};
4 changes: 2 additions & 2 deletions src/endpoints/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default (app: Application) => {
apiMessage(req.path, `File ${fileName} has been saved`);
return res.status(200).json({
success: true,
message: `File has been saved at ${url}/${fileName}`,
url: `${url}/${fileName}`,
message: `File has been saved at ${url}/view/${fileName}`,
url: `${url}/view/${fileName}`,
delete: `${url}/delete/${fileName}`,
});
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable no-var */
declare var generateKey: string | null;
10 changes: 5 additions & 5 deletions src/views/pages/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File - <%= data.name %></title>
<link rel="icon" type="image/x-icon" href="files/favicon.ico">
<link rel="stylesheet" href="css/index.css">
<meta name="robots" content="noindex">
<link rel="icon" type="image/x-icon" href="../files/favicon.ico">
<meta name="twitter:card" content="summary_large_image" />
<meta property="og:image" content="<%= img %>" />
<link rel="stylesheet" href="../css/index.css">
<meta name="theme-color" content="#000000" />
<meta property="og:description" content="" />
<meta property="og:site_name" content="">
<title>File - <%= data.name %></title>
<meta name="robots" content="noindex">
<meta charset="UTF-8">
</head>

<body>
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2021",
"outDir": "./dist",
"target": "ES2022",
"module": "CommonJS",
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"typeRoots": ["./src/types"],
"forceConsistentCasingInFileNames": true
}
}

0 comments on commit 1b68a71

Please sign in to comment.