From 423fa4a78c7049fc68d44a8a870b19de85944378 Mon Sep 17 00:00:00 2001 From: tycrek Date: Wed, 14 Jul 2021 11:32:19 -0600 Subject: [PATCH] setup autocalled from ass.js & handles initial auth & other tasks --- ass.js | 21 +++++++++------------ auth.js | 11 +---------- setup.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/ass.js b/ass.js index 10fa947..579479d 100755 --- a/ass.js +++ b/ass.js @@ -1,13 +1,19 @@ +let doSetup = null; try { // Check if config.json exists require('./config.json'); } catch (err) { - console.error('No config.json found! Please run \'npm run setup\''); - process.exit(1); + doSetup = require('./setup').doSetup; +} + +// Run first time setup if using Docker (pseudo-process, setup will be run with docker exec) +if (doSetup) { + doSetup(); + return; } // Load the config -const { host, port, useSsl, diskFilePath, isProxied, s3enabled } = require('./config.json'); +const { host, port, useSsl, isProxied, s3enabled } = require('./config.json'); //#region Imports const fs = require('fs-extra'); @@ -40,9 +46,6 @@ const users = require('./auth'); const data = require('./data'); //#endregion -// Create thumbnails directory -fs.ensureDirSync(path(diskFilePath, 'thumbnails')); - // Enable/disable Express features app.enable('case sensitive routing'); app.disable('x-powered-by'); @@ -97,10 +100,4 @@ log .info('Frontend', ASS_PREMIUM.enabled ? ASS_PREMIUM.brand : 'disabled', `${ASS_PREMIUM.enabled ? `${getTrueHttp()}${getTrueDomain()}${ASS_PREMIUM.endpoint}` : ''}`) .info('Index redirect', ASS_PREMIUM.enabled && ASS_PREMIUM.index ? `enable` : 'disabled') .blank() - .callback(() => { - if (process.argv[2] && process.argv[2] === '--docker-compose') - fs.ensureDir(require('path').join(process.cwd(), 'share')).then(() => log - .info('docker-compose', 'Exiting in 5 seconds...') - .callback(() => setTimeout(() => process.exit(0), 5000))) - }) .express().Host(app, port, host, () => log.success('Ready for uploads', `Storing resources ${s3enabled ? 'in S3' : 'on disk'}`)); diff --git a/auth.js b/auth.js index d0af5ed..1bb0578 100644 --- a/auth.js +++ b/auth.js @@ -3,16 +3,7 @@ */ const fs = require('fs-extra'); -const { log, path, arrayEquals, generateToken } = require('./utils'); - -// Make sure auth.json exists and generate the first key -if (!fs.existsSync(path('auth.json'))) { - let users = {}; - users[generateToken()] = { username: 'ass', count: 0 }; - fs.writeJsonSync(path('auth.json'), { users }, { spaces: 4 }); - log.debug('File created', 'auth.json') - .success('!! Important', `Save this token in a secure spot: ${Object.keys(users)[0]}`) -} +const { log, path, arrayEquals } = require('./utils'); const users = require('./auth.json').users || {}; diff --git a/setup.js b/setup.js index 77b0389..47c6e99 100755 --- a/setup.js +++ b/setup.js @@ -27,6 +27,7 @@ function doSetup() { const TLog = require('@tycrek/log'); const fs = require('fs-extra'); const prompt = require('prompt'); + const token = require('./generators/token'); const log = new TLog({ timestamp: { enabled: false } }); @@ -196,7 +197,34 @@ function doSetup() { // Confirm .then(() => prompt.get(confirmSchema)) .then(({ confirm }) => (confirm ? fs.writeJson(path('config.json'), results, { spaces: 4 }) : process.exit(1))) - .then(() => log.blank().success('Config has been saved!')) + + // Other setup tasks + .then(() => { + + // Make sure auth.json exists and generate the first key + let users = {}; + users[token()] = { username: 'ass', count: 0 }; + fs.writeJsonSync(path('auth.json'), { users }, { spaces: 4 }); + log.debug('File created', 'auth.json') + .success('!! Important', `Save this token in a secure spot: ${Object.keys(users)[0]}`) + + let existingData = {} + try { + existingData = fs.readJsonSync(path('data.json')); + } catch (ex) { + log.warn('data.json', 'File empty, fixing') + } + + // All 3 as a Promise.all + return Promise.all([ + fs.ensureDir(path('share')), + fs.ensureDir(path(results.diskFilePath, 'thumbnails')), + fs.writeJson(path('data.json'), existingData, { spaces: 4 }) + ]); + }) + + // Complete & exit + .then(() => log.blank().success('Setup complete').callback(() => process.exit(0))) .catch((err) => log.blank().error(err)); }