From 5f6eccd098c1317eac870e79bfda5c73572a3a13 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Sat, 30 Sep 2023 00:06:09 -0600 Subject: [PATCH] refactor: remove old files --- .deepsource.toml | 12 ------ src/generators/token.ts | 38 ------------------- src/metrics.js | 65 -------------------------------- src/tools/script.adduser.ts | 29 -------------- src/tools/script.setpassword.ts | 19 ---------- src/tools/script.testpassword.ts | 20 ---------- 6 files changed, 183 deletions(-) delete mode 100644 .deepsource.toml delete mode 100644 src/generators/token.ts delete mode 100644 src/metrics.js delete mode 100644 src/tools/script.adduser.ts delete mode 100644 src/tools/script.setpassword.ts delete mode 100644 src/tools/script.testpassword.ts diff --git a/.deepsource.toml b/.deepsource.toml deleted file mode 100644 index 53937b7..0000000 --- a/.deepsource.toml +++ /dev/null @@ -1,12 +0,0 @@ -version = 1 - -[[analyzers]] -name = "shell" -enabled = true - -[[analyzers]] -name = "javascript" -dependency_file_paths = ["package.json"] -environment = ["nodejs", "browser"] -module_system = "commonjs" -enabled = true \ No newline at end of file diff --git a/src/generators/token.ts b/src/generators/token.ts deleted file mode 100644 index e968ea8..0000000 --- a/src/generators/token.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { v4 as uuid } from 'uuid'; -import fs from 'fs-extra'; -import path from 'path'; -import randomGen from './random'; -import { TLog } from '@tycrek/log'; -const log = new TLog(); - -const MAX_USERNAME = 20; - -export default () => uuid().replace(/-/g, ''); -module.exports = () => uuid().replace(/-/g, ''); - -// If directly called on the command line, generate a new token -if (require.main === module) { - const token = module.exports(); - const authPath = path.join(process.cwd(), 'auth.json'); - let name = ''; - - fs.readJson(authPath) - .then((auth) => { - // Generate the user - const username = process.argv[2] ? process.argv[2].replace(/[^\da-z_]/gi, '').substring(0, MAX_USERNAME) : randomGen({ length: 20 }); // skipcq: JS-0074 - if (!auth.users) auth.users = {}; - if (Object.values(auth.users).findIndex((user: any) => user.username === username) !== -1) { - log.error('Username already exists', username); - process.exit(1); - } - auth.users[token] = { username, count: 0 }; - name = auth.users[token].username; - - fs.writeJsonSync(authPath, auth, { spaces: 4 }); - }) - .then(() => log - .comment('A new token has been generated and automatically applied.') - .comment('You do not need to restart \'ass\'.') - .success('Your token', token, `username: ${name}`)) - .catch(console.error); -} diff --git a/src/metrics.js b/src/metrics.js deleted file mode 100644 index b76cde3..0000000 --- a/src/metrics.js +++ /dev/null @@ -1,65 +0,0 @@ -const fs = require('fs-extra'); -const path = require('path'); -const { s3enabled } = require('../config.json'); -const { formatBytes } = require('./utils'); -const { bucketSize } = require('./storage'); - -const { TLog } = require('@tycrek/log'); -const log = new TLog({ level: 'debug', timestamp: { enabled: false } }); - -/** - * Thank you CoPilot for helping write whatever the fuck this is -tycrek, 2022-04-18 - */ -function whileWait(expression, timeout = 1000) { - return new Promise(async (resolve, reject) => { - while (expression()) - await new Promise((resolve) => setTimeout(resolve, timeout)); - resolve(); - }); -} - -module.exports = () => { - const data = require('./data').data; - const { users } = fs.readJsonSync(path.join(process.cwd(), 'auth.json')); - Object.keys(users).forEach((token) => users[token].count = 0); - - let totalSize = 0; - let oldSize = 0; - let d = []; - - whileWait(() => data() === undefined) - .then(() => data().get()) - .then((D) => (d = D.map(([, resource]) => resource))) - .then(() => - d.forEach(({ token, size }) => { - try { - totalSize += size; - if (token === undefined) oldSize += size; // skipcq: JS-0127 - else { - if (!users[token].size) users[token].size = 0; - users[token].size += size; - users[token].count++; - } - } catch (ex) { - // Silently handle missing tokens from dev environment -tycrek - } - })) - .then(() => bucketSize()) - .then((s3size) => { - log.info('---- Usage metrics ----') - .blank() - .info('Users', Object.keys(users).length) - .info('Files', Object.keys(d).length) - .info('S3 size', s3enabled ? s3size : '--') - .blank() - .info('Total size', formatBytes(totalSize)) - .info('Old files', formatBytes(oldSize)) - .blank(); - - Object.values(users).forEach(({ username, count, size }) => log.info(`- ${username}`, formatBytes(size), `${count} files`)); - process.exit(0); - }) - .catch(console.error); -} - -if (require.main === module) module.exports(); diff --git a/src/tools/script.adduser.ts b/src/tools/script.adduser.ts deleted file mode 100644 index 147f241..0000000 --- a/src/tools/script.adduser.ts +++ /dev/null @@ -1,29 +0,0 @@ -import path from 'path'; -import fs from 'fs-extra'; -import axios from 'axios'; -import logger from '../logger'; -import { User } from '../types/auth'; - -// Port from config.json -const { port } = fs.readJsonSync(path.join(process.cwd(), 'config.json')); - -// CLI key from auth.json -const { cliKey } = fs.readJsonSync(path.join(process.cwd(), 'auth.json')); - -if (process.argv.length < 4) { - logger.error('Missing username or password'); - logger.error('Usage: node script.adduser.js [admin] [meta]'); - process.exit(1); -} else { - const username = process.argv[2]; - const password = process.argv[3]; - const admin = process.argv[4] ? process.argv[4].toLowerCase() === 'true' : false; - const meta = process.argv[5] ? JSON.parse(process.argv[5]) : {}; - - axios.post(`http://localhost:${port}/api/user`, { username, password, admin, meta }, { headers: { 'Authorization': cliKey } }) - .then((response) => { - const user = response.data as User; - logger.info('User created', username, user.unid).callback(() => process.exit(0)) - }) - .catch((err) => logger.error(err).callback(() => process.exit(1))); -} diff --git a/src/tools/script.setpassword.ts b/src/tools/script.setpassword.ts deleted file mode 100644 index ebbd55a..0000000 --- a/src/tools/script.setpassword.ts +++ /dev/null @@ -1,19 +0,0 @@ -import logger from '../logger'; -import { onStart, users, setUserPassword } from '../auth'; - -if (process.argv.length < 4) { - logger.error('Missing username/unid or password'); - process.exit(1); -} else { - const id = process.argv[2]; - const password = process.argv[3]; - - onStart(process.argv[4] || 'auth.json') - .then(() => { - const user = users.find((user) => user.unid === id || user.username === id); - if (!user) throw new Error('User not found'); - else return setUserPassword(user.unid, password); - }) - .then(() => logger.info('Password changed successfully').callback(() => process.exit(0))) - .catch((err) => logger.error(err).callback(() => process.exit(1))); -} diff --git a/src/tools/script.testpassword.ts b/src/tools/script.testpassword.ts deleted file mode 100644 index 699ec9a..0000000 --- a/src/tools/script.testpassword.ts +++ /dev/null @@ -1,20 +0,0 @@ -import logger from '../logger'; -import { onStart, users } from '../auth'; -import { compare } from 'bcrypt'; - -if (process.argv.length < 4) { - logger.error('Missing username/unid or password'); - process.exit(1); -} else { - const id = process.argv[2]; - const password = process.argv[3]; - - onStart(process.argv[4] || 'auth.json') - .then(() => { - const user = users.find((user) => user.unid === id || user.username === id); - if (!user) throw new Error('User not found'); - else return compare(password, user.passhash); - }) - .then((result) => logger.info('Matches', `${result}`).callback(() => process.exit(0))) - .catch((err) => logger.error(err).callback(() => process.exit(1))); -}