optimized scripts

pull/19/head
tycrek 4 years ago
parent efe3440903
commit ecdebf73ad
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -23,6 +23,7 @@
- ✔️ Multiple access types - ✔️ Multiple access types
- **[ZWS](https://zws.im)** - **[ZWS](https://zws.im)**
- **Mixed-case alphanumeric** - **Mixed-case alphanumeric**
- **Gfycat**
- **Original** - **Original**
- ❌ Thumbnail support - ❌ Thumbnail support
- ❌ Multiple database types - ❌ Multiple database types
@ -39,6 +40,7 @@
| ---- | ----------- | | ---- | ----------- |
| **[ZWS](https://zws.im)** (Zero-width spaces) | The "fancy" mode. When pasted elsewhere, the URL appears to be *just* your domain name. ![ZWS sample](https://user-images.githubusercontent.com/29926144/113785625-bf43a480-96f4-11eb-8dd7-7f164f33ada2.png "ZWS sample") | | **[ZWS](https://zws.im)** (Zero-width spaces) | The "fancy" mode. When pasted elsewhere, the URL appears to be *just* your domain name. ![ZWS sample](https://user-images.githubusercontent.com/29926144/113785625-bf43a480-96f4-11eb-8dd7-7f164f33ada2.png "ZWS sample") |
| **Mixed-case alphanumeric** | The "safe" mode. URL's are browser safe as the character set is just letters & numbers. | | **Mixed-case alphanumeric** | The "safe" mode. URL's are browser safe as the character set is just letters & numbers. |
| **Gfycat** | Gfycat-style ID's (for example: `https://gfycat.com/unsungdiscretegrub` "unsung discrete grub") |
| **Original** | The "basic" mode. URL matches the same filename as when the file was uploaded. This may be prone to conflicts with files of the same name. | | **Original** | The "basic" mode. URL matches the same filename as when the file was uploaded. This may be prone to conflicts with files of the same name. |
## Installation ## Installation
@ -90,7 +92,7 @@ If you need to override a specific part of the config to be different from the g
| Header | Purpose | | Header | Purpose |
| ------ | ------- | | ------ | ------- |
| **`X-Ass-Domain`** | Override the domain returned for the clipboard (useful for multi-domain hosts) | | **`X-Ass-Domain`** | Override the domain returned for the clipboard (useful for multi-domain hosts) |
| **`X-Ass-Access`** | Override the generator used for the resource URI. Must be one of: `original`, `zws`, or `random` ([see above](#access-types)) | | **`X-Ass-Access`** | Override the generator used for the resource URI. Must be one of: `original`, `zws`, `gfycat`, or `random` ([see above](#access-types)) |
### Fancy embeds ### Fancy embeds

@ -1,19 +1,17 @@
const fs = require('fs-extra'); const fs = require('fs-extra');
const adjectiveList = fs.readFileSync("./generators/gfycat/adjectives.txt", "utf-8").split('\n'); const adjectives = fs.readFileSync('./generators/gfycat/adjectives.txt').toString().split('\n');
const animalsList = fs.readFileSync("./generators/gfycat/animals.txt", "utf-8").split('\n'); const animals = fs.readFileSync('./generators/gfycat/animals.txt').toString().split('\n');
const MIN_LENGTH = require('../setup').gfyIdSize;
function genString(adjCount) { function genString(count = MIN_LENGTH) {
let adjectivesString = ""; let gfycat = '';
for (i = 0; i < adjCount; i++) adjectivesString += genAdjective(); for (i = 0; i < (count < MIN_LENGTH ? MIN_LENGTH : count); i++)
return adjectivesString + genAnimal(); gfycat += getWord(adjectives, '-');
return gfycat.concat(getWord(animals));
}; };
function genAnimal() { function getWord(list, delim = '') {
return animalsList[Math.floor(Math.random()*animalsList.length)]; return list[Math.floor(Math.random() * list.length)].concat(delim);
} }
function genAdjective() { module.exports = ({ gfyLength }) => genString(gfyLength);
return adjectiveList[Math.floor(Math.random()*adjectiveList.length)] + "-";
}
module.exports = (adjectives) => genString(adjectives);

@ -1,2 +1,2 @@
const cryptoRandomString = require('crypto-random-string'); const cryptoRandomString = require('crypto-random-string');
module.exports = (length) => cryptoRandomString({ length, type: 'alphanumeric' }); module.exports = ({ length }) => cryptoRandomString({ length, type: 'alphanumeric' });

@ -1,3 +1,3 @@
const { randomBytes } = require('crypto'); const { randomBytes } = require('crypto');
const zeroWidthChars = ['\u200B', '\u200C', '\u200D', '\u2060']; const zeroWidthChars = ['\u200B', '\u200C', '\u200D', '\u2060'];
module.exports = (size) => [...randomBytes(size)].map(byte => zeroWidthChars[+byte % zeroWidthChars.length]).join('').slice(1) + zeroWidthChars[0]; module.exports = ({ length }) => [...randomBytes(length)].map(byte => zeroWidthChars[+byte % zeroWidthChars.length]).join('').slice(1) + zeroWidthChars[0];

@ -1,12 +1,3 @@
const { path, log } = require('./utils');
const fs = require('fs-extra');
const prompt = require('prompt');
// Disabled the annoying "prompt: " prefix and removes colours
prompt.message = '';
prompt.colors = false;
prompt.start();
// Default configuration // Default configuration
const config = { const config = {
host: '0.0.0.0', host: '0.0.0.0',
@ -22,109 +13,123 @@ const config = {
saveAsOriginal: true, saveAsOriginal: true,
}; };
// Schema for setup prompts // If directly called on the command line, run setup script
const setupSchema = { if (require.main === module) {
properties: { const { path, log } = require('./utils');
host: { const fs = require('fs-extra');
description: 'Local IP to listen on', const prompt = require('prompt');
type: 'string',
default: config.host,
required: false
},
port: {
description: 'Port number to listen on',
type: 'integer',
default: config.port,
required: false
},
domain: {
description: `Domain name to send to ShareX clients (example: ${config.domain})`,
type: 'string',
required: true,
message: 'You must input a valid domain name or IP to continue'
},
useSsl: {
description: 'Use SSL (requires reverse proxy!)',
type: 'boolean',
default: config.useSsl,
required: false
},
isProxied: {
description: 'Will you be running through a reverse proxy',
type: 'boolean',
default: config.isProxied,
required: false
},
resourceIdSize: {
description: 'Resource ID size (by using a higher value, you will be able to upload more files)',
type: 'integer',
default: config.resourceIdSize,
required: false
},
gfyIdSize: {
description: 'Adjective count for "gfycat" resource mode',
type: 'integer',
default: config.gfyIdSize,
required: false
},
resourceIdType: {
description: 'Resource ID type (determines what kind of URL your uploads are visible at. Can be one of: original, zws, random)',
type: 'string',
default: config.resourceIdType,
require: false,
pattern: /(original|zws|random|gfycat)/gi,
message: 'Must be one of: original, zws, random, gfycat'
},
diskFilePath: { // Disabled the annoying "prompt: " prefix and removes colours
description: 'Relative path to save uploads to', prompt.message = '';
type: 'string', prompt.colors = false;
default: config.diskFilePath, prompt.start();
required: false
}, // Schema for setup prompts
saveWithDate: { const setupSchema = {
description: 'Use date folder structure (e.x. uploads/2021-04/image.png)', properties: {
type: 'boolean', host: {
default: config.saveWithDate, description: 'Local IP to listen on',
required: false type: 'string',
}, default: config.host,
saveAsOriginal: { required: false
description: 'Save as original file name instead of random', },
type: 'boolean', port: {
default: config.saveAsOriginal, description: 'Port number to listen on',
required: false type: 'integer',
}, default: config.port,
} required: false
}; },
domain: {
description: `Domain name to send to ShareX clients (example: ${config.domain})`,
type: 'string',
required: true,
message: 'You must input a valid domain name or IP to continue'
},
useSsl: {
description: 'Use SSL (requires reverse proxy!)',
type: 'boolean',
default: config.useSsl,
required: false
},
isProxied: {
description: 'Will you be running through a reverse proxy',
type: 'boolean',
default: config.isProxied,
required: false
},
resourceIdSize: {
description: 'Resource ID size (by using a higher value, you will be able to upload more files)',
type: 'integer',
default: config.resourceIdSize,
required: false
},
gfyIdSize: {
description: 'Adjective count for "gfycat" Resource ID type',
type: 'integer',
default: config.gfyIdSize,
required: false
},
resourceIdType: {
description: 'Resource ID type (determines what kind of URL your uploads are visible at. Can be one of: original, zws, random)',
type: 'string',
default: config.resourceIdType,
require: false,
pattern: /(original|zws|random|gfycat)/gi,
message: 'Must be one of: original, zws, random, gfycat'
},
// Schema for confirm prompt. User must enter 'y' or 'n' (case-insensitive) diskFilePath: {
const confirmSchema = { description: 'Relative path to save uploads to',
properties: { type: 'string',
confirm: { default: config.diskFilePath,
description: '\nIs the above information correct? (y/n)', required: false
type: 'string', },
pattern: /^[y|n]/gim, saveWithDate: {
message: 'Must respond with either \'y\' or \'n\'', description: 'Use date folder structure (e.x. uploads/2021-04/image.png)',
default: 'y', type: 'boolean',
required: false, default: config.saveWithDate,
before: (value) => value.toLowerCase().startsWith('y') required: false
},
saveAsOriginal: {
description: 'Save as original file name instead of random',
type: 'boolean',
default: config.saveAsOriginal,
required: false
},
} }
};
// Schema for confirm prompt. User must enter 'y' or 'n' (case-insensitive)
const confirmSchema = {
properties: {
confirm: {
description: '\nIs the above information correct? (y/n)',
type: 'string',
pattern: /^[y|n]/gim,
message: 'Must respond with either \'y\' or \'n\'',
default: 'y',
required: false,
before: (value) => value.toLowerCase().startsWith('y')
}
}
};
function setup() {
log('<<< ass setup >>>\n');
let results;
prompt.get(setupSchema)
.then((r) => results = r)
.then(() => log('\nPlease verify your information:\n\n' + Object.keys(results).map((result) => (` ${result}: ${results[result]}`)).join('\n') + '\n'))
.then(() => prompt.get(confirmSchema))
.then(({ confirm }) => confirm ? fs.writeJson(path('config.json'), results, { spaces: 4 }) : process.exit(1))
.then(() => log('\nConfig has been saved!'))
.catch((err) => console.error(err));
} }
};
function setup() { setup();
log('<<< ass setup >>>\n');
let results;
prompt.get(setupSchema)
.then((r) => results = r)
.then(() => log('\nPlease verify your information:\n\n' + Object.keys(results).map((result) => (` ${result}: ${results[result]}`)).join('\n') + '\n'))
.then(() => prompt.get(confirmSchema))
.then(({ confirm }) => confirm ? fs.writeJson(path('config.json'), results, { spaces: 4 }) : process.exit(1))
.then(() => log('\nConfig has been saved!'))
.catch((err) => console.error(err));
} }
setup(); module.exports = config;
/*{ /*{
description: 'Enter your password', // Prompt displayed to the user. If not supplied name will be used. description: 'Enter your password', // Prompt displayed to the user. If not supplied name will be used.

@ -9,20 +9,21 @@ const idModes = {
zws: 'zws', // Zero-width spaces (see: https://zws.im/) zws: 'zws', // Zero-width spaces (see: https://zws.im/)
og: 'original', // Use original uploaded filename og: 'original', // Use original uploaded filename
r: 'random', // Use a randomly generated ID with a mixed-case alphanumeric character set r: 'random', // Use a randomly generated ID with a mixed-case alphanumeric character set
gfy: 'gfycat' //gfycat-style ID's (example.com/correct-horse-battery-staple) gfy: 'gfycat' // Gfycat-style ID's (https://gfycat.com/unsungdiscretegrub)
}; };
const GENERATORS = new Map();
GENERATORS.set(idModes.zws, zwsGen);
GENERATORS.set(idModes.r, randomGen);
GENERATORS.set(idModes.gfy, gfyGen);
module.exports = { module.exports = {
log: console.log, log: console.log,
path: (...paths) => Path.join(__dirname, ...paths), path: (...paths) => Path.join(__dirname, ...paths),
saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }), saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }),
verify: (req, tokens) => req.headers.authorization && tokens.includes(req.headers.authorization), verify: (req, tokens) => req.headers.authorization && tokens.includes(req.headers.authorization),
generateToken: () => token(), generateToken: () => token(),
generateId: (mode, lenth, gfyLength, originalName) => generateId: (mode, length, gfyLength, originalName) => GENERATORS.has(mode) ? GENERATORS.get(mode)({ length, gfyLength }) : originalName,
(mode == idModes.zws) ? zwsGen(lenth)
: (mode == idModes.r) ? randomGen(lenth)
: (mode == idModes.gfy) ? gfyGen(gfyLength)
: originalName,
formatBytes: (bytes, decimals = 2) => { formatBytes: (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes'; if (bytes === 0) return '0 Bytes';
let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
@ -32,7 +33,8 @@ module.exports = {
randomHexColour: () => { // From: https://www.geeksforgeeks.org/javascript-generate-random-hex-codes-color/ randomHexColour: () => { // From: https://www.geeksforgeeks.org/javascript-generate-random-hex-codes-color/
let letters = "0123456789ABCDEF"; let letters = "0123456789ABCDEF";
let colour = '#'; let colour = '#';
for (var i = 0; i < 6; i++) colour += letters[(Math.floor(Math.random() * 16))]; for (var i = 0; i < 6; i++)
colour += letters[(Math.floor(Math.random() * 16))];
return colour; return colour;
} }
} }

Loading…
Cancel
Save