From 2efed9dcfc41d8a7d4ce8bb1b3a7bc6601e5c5f1 Mon Sep 17 00:00:00 2001 From: MasutaYunikon Date: Fri, 20 Mar 2026 16:55:03 +0100 Subject: [PATCH] fix: add config.ts with config variable (BASE_DIR...) --- server/auth.ts | 3 ++- server/config.ts | 13 +++++++++++++ server/index.ts | 17 ++--------------- server/logger.ts | 3 ++- server/organize-worker.ts | 10 ++-------- server/secret.ts | 3 ++- server/settings.ts | 6 +----- server/torrent-clients/index.ts | 3 ++- 8 files changed, 26 insertions(+), 32 deletions(-) create mode 100644 server/config.ts diff --git a/server/auth.ts b/server/auth.ts index 45ba851..20b0035 100644 --- a/server/auth.ts +++ b/server/auth.ts @@ -4,8 +4,9 @@ import bcrypt from 'bcryptjs' import jwt from 'jsonwebtoken' import type { Request, Response, NextFunction } from 'express' import { JWT_SECRET } from './secret.js' +import { BASE_DIR } from './config.js' -const DATA_PATH = path.join(process.cwd(), 'config', 'auth.json') +const DATA_PATH = path.join(BASE_DIR, 'config', 'auth.json') const SALT_ROUNDS = 10 interface AuthData { diff --git a/server/config.ts b/server/config.ts new file mode 100644 index 0000000..e19c0a2 --- /dev/null +++ b/server/config.ts @@ -0,0 +1,13 @@ +import fs from 'fs' +import path from 'path' + +const _isBunBinary = typeof (globalThis as any).Bun !== 'undefined' + && path.dirname((process as any).execPath) !== process.cwd() + +export const BASE_DIR = _isBunBinary + ? path.dirname((process as any).execPath) + : process.cwd() + +export const DATA_DIR = fs.existsSync('/.dockerenv') + ? '/config' + : path.join(BASE_DIR, 'data') \ No newline at end of file diff --git a/server/index.ts b/server/index.ts index 701e3e6..5340b18 100644 --- a/server/index.ts +++ b/server/index.ts @@ -12,23 +12,10 @@ import { import qbittorrentDriver from './torrent-clients/qbittorrent.js' import { organizeTorrent, autoOrganizeAll, scanMediaPath } from './organize.js' import { logger, readLogs, clearLogs, logsFileSize } from './logger.js' +import { DATA_DIR, BASE_DIR, _isBunBinary } from './config.js' registerDriver(qbittorrentDriver) -// ── Base directory ───────────────────────────────────────────── -// Binaire Bun compilé : data/ et public/ sont à côté de l'exécutable -// Docker / dev : relatif au cwd (comportement inchangé) -const _isBunBinary = typeof (globalThis as any).Bun !== 'undefined' - && path.dirname((process as any).execPath) !== process.cwd() -export const BASE_DIR = _isBunBinary - ? path.dirname((process as any).execPath) - : process.cwd() - -// DATA_DIR : /config -export const DATA_DIR = fs.existsSync('/.dockerenv') - ? '/config' - : path.join(BASE_DIR, 'config') - const app = express() const PORT = Number(process.env.PORT) || 9898 const FANKAI_API = 'https://metadata.fankai.fr' @@ -416,7 +403,7 @@ app.get('/api/downloads', requireAuth, async (_req, res) => { let torrentFinal: any[] = [] try { - const tfPath = path.join(process.cwd(), 'config', 'torrent_final.json') + const tfPath = path.join(BASE_DIR, 'config', 'torrent_final.json') if (fs.existsSync(tfPath)) torrentFinal = JSON.parse(fs.readFileSync(tfPath, 'utf-8')) } catch {} diff --git a/server/logger.ts b/server/logger.ts index 5a25bda..e1d6cd9 100644 --- a/server/logger.ts +++ b/server/logger.ts @@ -8,6 +8,7 @@ import fs from 'fs' import path from 'path' +import { BASE_DIR } from './config.js' export type LogLevel = 'debug' | 'info' | 'warn' | 'error' @@ -19,7 +20,7 @@ export interface LogEntry { meta? : Record } -const LOGS_PATH = path.join(process.cwd(), 'config', 'logs.jsonl') +const LOGS_PATH = path.join(BASE_DIR, 'config', 'logs.jsonl') const MAX_LINES = 2000 // rotation auto au-delà const IS_PROD = process.env.NODE_ENV === 'production' diff --git a/server/organize-worker.ts b/server/organize-worker.ts index 8b47be4..14681cd 100644 --- a/server/organize-worker.ts +++ b/server/organize-worker.ts @@ -15,13 +15,7 @@ import fs from 'fs' import fsp from 'fs/promises' import path from 'path' import { parentPort } from 'worker_threads' - -// Base directory : binaire Bun ou cwd (Docker/dev) -const _isBunBinary = typeof (globalThis as any).Bun !== 'undefined' - && path.dirname((process as any).execPath) !== process.cwd() -const BASE_DIR = _isBunBinary - ? path.dirname((process as any).execPath) - : process.cwd() +import { BASE_DIR, _isBunBinary } from './config.js' const TORRENTS_PATH = path.join(BASE_DIR, 'config', 'torrent_final.json') const ORGANIZED_PATH = path.join(BASE_DIR, 'config', 'organized.json') @@ -39,7 +33,7 @@ function debug(msg: string) { parentPort?.postMessage({ type: 'log', level: 'deb function readSettings(): { mediaPath: string; completePath: string; organizeMode: string; nfoSupport: boolean } { try { - const p = path.join(process.cwd(), 'config', 'settings.json') + const p = path.join(BASE_DIR, 'config', 'settings.json') if (!fs.existsSync(p)) return { mediaPath: '', completePath: '', organizeMode: 'hardlink', nfoSupport: false } return JSON.parse(fs.readFileSync(p, 'utf-8')) } catch { return { mediaPath: '', completePath: '', organizeMode: 'hardlink', nfoSupport: false } } diff --git a/server/secret.ts b/server/secret.ts index 827d986..ec51195 100644 --- a/server/secret.ts +++ b/server/secret.ts @@ -9,8 +9,9 @@ import fs from 'fs' import path from 'path' import crypto from 'crypto' +import { BASE_DIR } from './config.js' -const SECRET_PATH = path.join(process.cwd(), 'config', 'secret.key') +const SECRET_PATH = path.join(BASE_DIR, 'config', 'secret.key') function generateSecret(): string { return crypto.randomBytes(48).toString('hex') diff --git a/server/settings.ts b/server/settings.ts index 2425267..fdbd971 100644 --- a/server/settings.ts +++ b/server/settings.ts @@ -1,11 +1,7 @@ import fs from 'fs' import path from 'path' +import { BASE_DIR, _isBunBinary } from './config.js' -const _isBunBinary = typeof (globalThis as any).Bun !== 'undefined' - && path.dirname((process as any).execPath) !== process.cwd() -const BASE_DIR = _isBunBinary - ? path.dirname((process as any).execPath) - : process.cwd() const DATA_PATH = path.join(BASE_DIR, 'config', 'settings.json') export interface Settings { diff --git a/server/torrent-clients/index.ts b/server/torrent-clients/index.ts index d3de2b3..f4700ff 100644 --- a/server/torrent-clients/index.ts +++ b/server/torrent-clients/index.ts @@ -9,6 +9,7 @@ import { randomUUID } from 'crypto' import fs from 'fs' import path from 'path' +import { BASE_DIR } from '../config.js' // ─── Types ──────────────────────────────────────────────────────────────────── @@ -77,7 +78,7 @@ export function getAvailableClients(): TorrentClientDefinition[] { // ─── Persistence (clients enregistrés) ──────────────────────────────────────── -const CLIENTS_PATH = path.join(process.cwd(), 'config', 'torrent_clients.json') +const CLIENTS_PATH = path.join(BASE_DIR, 'config', 'torrent_clients.json') function loadClients(): SavedClient[] { try {