diff --git a/index.js b/index.js index 16179ab..1f8c1e3 100755 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import { config, getProviders, refreshConfig } from './lib/utils.js'; +import { checkIfConfigIsAccessible, config, getProviders, refreshConfig } from './lib/utils.js'; import * as similarityCache from './lib/services/similarity-check/similarityCache.js'; import * as jobStorage from './lib/services/storage/jobStorage.js'; import FredyRuntime from './lib/FredyRuntime.js'; @@ -16,6 +16,13 @@ import { initActiveCheckerCron } from './lib/services/crons/listing-alive-cron.j // Load configuration before any other startup steps await refreshConfig(); +const isConfigAccessible = await checkIfConfigIsAccessible(); + +if (!isConfigAccessible) { + logger.error('Configuration exists, but is not accessible. Please check the file permission'); + process.exit(1); +} + // Ensure sqlite directory exists before loading anything else (based on config.sqlitepath) const rawDir = config.sqlitepath || '/db'; const relDir = rawDir.startsWith('/') ? rawDir.slice(1) : rawDir; diff --git a/lib/utils.js b/lib/utils.js index e440796..b2d8689 100755 --- a/lib/utils.js +++ b/lib/utils.js @@ -180,6 +180,23 @@ function buildHash(...inputs) { */ let config = {}; +/** + * If the config exists, but cannot be accessed, we quit Fredy as something is fishy here. + * @returns {Promise} + */ +export async function checkIfConfigIsAccessible() { + const path = new URL('../conf/config.json', import.meta.url); + try { + if (!fs.existsSync(path)) { + return true; + } + fs.accessSync(path, fs.constants.R_OK); + return true; + } catch { + return false; + } +} + /** * Read config JSON from disk (conf/config.json) and parse it. * @returns {Promise} Parsed configuration object.