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,8 +13,19 @@ 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) {
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();
// Schema for setup prompts
const setupSchema = {
properties: { properties: {
host: { host: {
description: 'Local IP to listen on', description: 'Local IP to listen on',
@ -62,7 +64,7 @@ const setupSchema = {
required: false required: false
}, },
gfyIdSize: { gfyIdSize: {
description: 'Adjective count for "gfycat" resource mode', description: 'Adjective count for "gfycat" Resource ID type',
type: 'integer', type: 'integer',
default: config.gfyIdSize, default: config.gfyIdSize,
required: false required: false
@ -95,10 +97,10 @@ const setupSchema = {
required: false required: false
}, },
} }
}; };
// Schema for confirm prompt. User must enter 'y' or 'n' (case-insensitive) // Schema for confirm prompt. User must enter 'y' or 'n' (case-insensitive)
const confirmSchema = { const confirmSchema = {
properties: { properties: {
confirm: { confirm: {
description: '\nIs the above information correct? (y/n)', description: '\nIs the above information correct? (y/n)',
@ -110,9 +112,9 @@ const confirmSchema = {
before: (value) => value.toLowerCase().startsWith('y') before: (value) => value.toLowerCase().startsWith('y')
} }
} }
}; };
function setup() { function setup() {
log('<<< ass setup >>>\n'); log('<<< ass setup >>>\n');
let results; let results;
prompt.get(setupSchema) prompt.get(setupSchema)
@ -122,9 +124,12 @@ function setup() {
.then(({ confirm }) => confirm ? fs.writeJson(path('config.json'), results, { spaces: 4 }) : process.exit(1)) .then(({ confirm }) => confirm ? fs.writeJson(path('config.json'), results, { spaces: 4 }) : process.exit(1))
.then(() => log('\nConfig has been saved!')) .then(() => log('\nConfig has been saved!'))
.catch((err) => console.error(err)); .catch((err) => console.error(err));
}
setup();
} }
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