diff --git a/README.md b/README.md index f8739ec..1387c78 100644 --- a/README.md +++ b/README.md @@ -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 @ `, 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. diff --git a/index.ts b/index.ts index 9a63aa1..b54e334 100644 --- a/index.ts +++ b/index.ts @@ -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')) { @@ -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`); @@ -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) { diff --git a/src/endpoints/file.ts b/src/endpoints/file.ts index e4f79b2..f5887a9 100644 --- a/src/endpoints/file.ts +++ b/src/endpoints/file.ts @@ -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; diff --git a/src/endpoints/generate.ts b/src/endpoints/generate.ts new file mode 100644 index 0000000..307ce05 --- /dev/null +++ b/src/endpoints/generate.ts @@ -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' }); + } + }); +}; diff --git a/src/endpoints/save.ts b/src/endpoints/save.ts index c2429bb..ee3c506 100644 --- a/src/endpoints/save.ts +++ b/src/endpoints/save.ts @@ -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) { diff --git a/src/types/global.d.ts b/src/types/global.d.ts new file mode 100644 index 0000000..5223e02 --- /dev/null +++ b/src/types/global.d.ts @@ -0,0 +1,2 @@ +/* eslint-disable no-var */ +declare var generateKey: string | null; diff --git a/src/views/pages/index.ejs b/src/views/pages/index.ejs index 546f311..7c734d5 100644 --- a/src/views/pages/index.ejs +++ b/src/views/pages/index.ejs @@ -2,17 +2,17 @@ - - File - <%= data.name %> - - - + + + File - <%= data.name %> + + diff --git a/tsconfig.json b/tsconfig.json index 618ea2e..4f7f8dc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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 } }