nearly 2am and I'm fixing random things from DeepSource still

pull/20/head
tycrek 3 years ago
parent 8a8b409a75
commit 15b5f01b46
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -7,7 +7,7 @@ try {
}
// Load the config
const { host, port, domain, useSsl, resourceIdSize, gfyIdSize, resourceIdType, isProxied, s3enabled, saveAsOriginal } = require('./config.json');
const { host, port, resourceIdSize, gfyIdSize, resourceIdType, isProxied, s3enabled, saveAsOriginal } = require('./config.json');
//#region Imports
const fs = require('fs-extra');
@ -25,7 +25,7 @@ const Vibrant = require('./vibrant');
const Hash = require('./hash');
const Path = require('path');
const { uploadLocal, uploadS3, deleteS3 } = require('./storage');
const { path, saveData, log, verify, generateToken, generateId, formatBytes, arrayEquals, getS3url, downloadTempS3, sanitize } = require('./utils');
const { path, saveData, log, verify, getTrueHttp, getTrueDomain, renameFile, generateToken, generateId, formatBytes, arrayEquals, getS3url, downloadTempS3, sanitize } = require('./utils');
//#endregion
//#region Variables, module setup
@ -37,9 +37,6 @@ let users = {};
let data = {};
//#endregion
preStartup();
startup();
function preStartup() {
// Make sure data.json exists
if (!fs.existsSync(path('data.json'))) {
@ -82,7 +79,7 @@ function startup() {
}));
// Don't process favicon requests
app.use((req, res, next) => req.url.includes('favicon.ico') ? res.sendStatus(204) : next());
app.use((req, res, next) => (req.url.includes('favicon.ico') ? res.sendStatus(204) : next()));
// Index
app.get('/', (_req, res) => fs.readFile(path('README.md')).then((bytes) => bytes.toString()).then(marked).then((d) => res.render('index', { data: d })));
@ -123,22 +120,9 @@ function startup() {
))
// Remove the temp file if using S3 storage, otherwise rename the local file
.then(() => s3enabled ? fs.remove(path('uploads/', req.file.originalname)) : renameFile(saveAsOriginal ? req.file.originalname : req.file.sha1))
.then(() => (s3enabled ? fs.remove(path('uploads/', req.file.originalname)) : renameFile(req, saveAsOriginal ? req.file.originalname : req.file.sha1)))
.then(() => next())
.catch((err) => next(err));
function renameFile(newName) {
return new Promise((resolve, reject) => {
try {
const paths = [req.file.destination, newName];
fs.rename(path(req.file.path), path(...paths));
req.file.path = Path.join(...paths);
resolve();
} catch (err) {
reject(err);
}
});
}
});
// Process uploaded file
@ -313,10 +297,5 @@ function startup() {
app.listen(port, host, () => log(`Server started on [${host}:${port}]\nAuthorized users: ${Object.keys(users).length}\nAvailable files: ${Object.keys(data).length}`));
}
function getTrueHttp() {
return ('http').concat(useSsl ? 's' : '').concat('://');
}
function getTrueDomain(d = domain) {
return d.concat((port === 80 || port === 443 || isProxied) ? '' : `:${port}`);
}
preStartup();
startup();

@ -3,6 +3,10 @@ const adjectives = fs.readFileSync('./generators/gfycat/adjectives.txt').toStrin
const animals = fs.readFileSync('./generators/gfycat/animals.txt').toString().split('\n');
const MIN_LENGTH = require('../setup').gfyIdSize;
function getWord(list, delim = '') {
return list[Math.floor(Math.random() * list.length)].concat(delim);
}
function genString(count = MIN_LENGTH) {
let gfycat = '';
for (let i = 0; i < (count < MIN_LENGTH ? MIN_LENGTH : count); i++)
@ -10,8 +14,4 @@ function genString(count = MIN_LENGTH) {
return gfycat.concat(getWord(animals));
};
function getWord(list, delim = '') {
return list[Math.floor(Math.random() * list.length)].concat(delim);
}
module.exports = ({ gfyLength }) => genString(gfyLength);

@ -7,7 +7,7 @@ const { s3enabled } = require('./config.json');
module.exports = (file) =>
new Promise((resolve, reject) =>
toArray((fs.createReadStream(s3enabled ? path('uploads/', file.originalname) : path(file.path))))
.then((parts) => Buffer.concat(parts.map((part) => Buffer.isBuffer(part) ? part : Buffer.from(part))))
.then((parts) => Buffer.concat(parts.map((part) => (Buffer.isBuffer(part) ? part : Buffer.from(part)))))
.then((buf) => crypto.createHash('sha1').update(buf).digest('hex'))
.then(resolve)
.catch(reject));

@ -160,7 +160,7 @@ if (require.main === module) {
.then(() => Object.keys(results).map((result) => (` ${result}: ${results[result]}`)).join('\n'))
.then((resultString) => log(`\nPlease verify your information:\n\n${resultString}\n`))
.then(() => prompt.get(confirmSchema))
.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!'))
.catch((err) => console.error(err));
}

@ -44,9 +44,6 @@ const uploadLocal = multer({
})
}).single('file');
const bucketSize = () =>
new Promise((resolve, reject) => s3enabled ? listAllKeys(resolve, reject) : resolve(0));
function listAllKeys(resolve, reject, token) {
let allKeys = [];
s3.listObjectsV2({ Bucket: s3bucket, ContinuationToken: token }).promise()
@ -54,4 +51,7 @@ function listAllKeys(resolve, reject, token) {
.catch(reject);
}
const bucketSize = () =>
new Promise((resolve, reject) => (s3enabled ? listAllKeys(resolve, reject) : resolve(0)));
module.exports = { uploadLocal, uploadS3, deleteS3, bucketSize };

@ -19,8 +19,16 @@ function getCommand(src, dest) {
]);
}
function getNewName(oldName) {
return oldName.concat('.thumbnail.jpg');
}
function getNewNamePath(oldName) {
return path('uploads/thumbnails/', getNewName(oldName));
}
function getVideoThumbnail(file) {
return new Promise((resolve, reject) => exec(getCommand(s3enabled ? path('uploads/', file.originalname) : path(file.path), getNewNamePath(file.originalname)), (err) => err ? reject(err) : resolve()));
return new Promise((resolve, reject) => exec(getCommand((s3enabled ? path('uploads/', file.originalname) : path(file.path)), getNewNamePath(file.originalname)), (err) => (err ? reject(err) : resolve())));
}
function getResizedThumbnail(file) {
@ -34,14 +42,6 @@ function getResizedThumbnail(file) {
.catch(reject));
}
function getNewNamePath(oldName) {
return path('uploads/thumbnails/', getNewName(oldName));
}
function getNewName(oldName) {
return oldName.concat('.thumbnail.jpg');
}
module.exports = (file) =>
new Promise((resolve, reject) =>
(file.mimetype.includes('video') ? getVideoThumbnail : getResizedThumbnail)(file)

@ -6,7 +6,15 @@ const token = require('./generators/token');
const zwsGen = require('./generators/zws');
const randomGen = require('./generators/random');
const gfyGen = require('./generators/gfycat');
const { s3bucket, s3endpoint } = require('./config.json');
const { useSsl, port, isProxied, s3bucket, s3endpoint } = require('./config.json');
function getS3url(s3key, type) {
return `https://${s3bucket}.${s3endpoint}/${s3key}${getSafeExt(type)}`;
}
function getSafeExt(type) {
return type.includes('video') ? '.mp4' : type.includes('gif') ? '.gif' : '';
}
const idModes = {
zws: 'zws', // Zero-width spaces (see: https://zws.im/)
@ -25,8 +33,20 @@ module.exports = {
path: (...paths) => Path.join(__dirname, ...paths),
saveData: (data) => fs.writeJsonSync(Path.join(__dirname, 'data.json'), data, { spaces: 4 }),
verify: (req, users) => req.headers.authorization && Object.prototype.hasOwnProperty.call(users, req.headers.authorization),
getTrueHttp: () => ('http').concat(useSsl ? 's' : '').concat('://'),
getTrueDomain: (d = domain) => d.concat((port === 80 || port === 443 || isProxied) ? '' : `:${port}`),
renameFile: (req, newName) => new Promise((resolve, reject) => {
try {
const paths = [req.file.destination, newName];
fs.rename(path(req.file.path), path(...paths));
req.file.path = Path.join(...paths);
resolve();
} catch (err) {
reject(err);
}
}),
generateToken: () => token(),
generateId: (mode, length, gfyLength, originalName) => GENERATORS.has(mode) ? GENERATORS.get(mode)({ length, gfyLength }) : originalName,
generateId: (mode, length, gfyLength, originalName) => (GENERATORS.has(mode) ? GENERATORS.get(mode)({ length, gfyLength }) : originalName),
formatBytes: (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
@ -49,11 +69,3 @@ module.exports = {
getSafeExt,
sanitize
}
function getS3url(s3key, type) {
return `https://${s3bucket}.${s3endpoint}/${s3key}${getSafeExt(type)}`;
}
function getSafeExt(type) {
return type.includes('video') ? '.mp4' : type.includes('gif') ? '.gif' : '';
}

@ -6,10 +6,10 @@ const COLOR_COUNT = 256;
const QUALITY = 3;
module.exports = (file) =>
new Promise((resolve, reject) =>
new Promise((resolve, reject) => (
file.mimetype.includes('video')
? resolve(randomHexColour())
: Vibrant.from(s3enabled ? path('uploads/', file.originalname) : path(file.path))
.maxColorCount(COLOR_COUNT).quality(QUALITY).getPalette()
.then((palettes) => resolve(palettes[Object.keys(palettes).sort((a, b) => palettes[b].population - palettes[a].population)[0]].hex))
.catch(reject));
.catch(reject)));

Loading…
Cancel
Save