From feddab63a235b6faaa48dcacf0168c27bf07be95 Mon Sep 17 00:00:00 2001 From: Viren070 Date: Sun, 19 Oct 2025 21:14:08 +0100 Subject: [PATCH] feat: add regexes from custom/built-in templates to trusted regexes. --- packages/core/src/utils/feature.ts | 26 +++++++++-------- packages/core/src/utils/logger.ts | 1 + packages/core/src/utils/templates.ts | 42 ++++++++++++++++++++-------- packages/server/src/server.ts | 10 +------ 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/packages/core/src/utils/feature.ts b/packages/core/src/utils/feature.ts index bfee01b0..70f7e5b4 100644 --- a/packages/core/src/utils/feature.ts +++ b/packages/core/src/utils/feature.ts @@ -123,6 +123,20 @@ export class FeatureControl { } } + public static _addPatterns(patterns: string[]): void { + const initialCount = this._patternState.patterns.length; + const allPatterns = [ + ...new Set([...this._patternState.patterns, ...patterns]), + ]; + this._patternState.patterns = allPatterns; + const newCount = allPatterns.length - initialCount; + if (newCount > 0) { + logger.info( + `Accumulated ${newCount} new regex patterns. Total: ${allPatterns.length}` + ); + } + } + /** * Fetches patterns from all configured URLs and accumulates them. */ @@ -145,17 +159,7 @@ export class FeatureControl { .flatMap((result) => result.value); if (patternsFromUrls.length > 0) { - const initialCount = this._patternState.patterns.length; - const allPatterns = [ - ...new Set([...this._patternState.patterns, ...patternsFromUrls]), - ]; - this._patternState.patterns = allPatterns; - const newCount = allPatterns.length - initialCount; - if (newCount > 0) { - logger.info( - `Accumulated ${newCount} new regex patterns from URLs. Total: ${allPatterns.length}` - ); - } + FeatureControl._addPatterns(patternsFromUrls); } } diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index 8afd0e01..3dfd7a36 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -47,6 +47,7 @@ const moduleMap: { [key: string]: string } = { torrent: '👤 TORRENT', knaben: '🔍 KNABEN', 'torrent-galaxy': '🌐 TGx', + templates: '📄 TEMPLATES', }; // Define colors for each log level using full names diff --git a/packages/core/src/utils/templates.ts b/packages/core/src/utils/templates.ts index c5d5a73e..39b516eb 100644 --- a/packages/core/src/utils/templates.ts +++ b/packages/core/src/utils/templates.ts @@ -5,6 +5,10 @@ import { getDataFolder } from './general.js'; import { Template, TemplateSchema } from '../db/schemas.js'; import { ZodError } from 'zod'; import { formatZodError } from './config.js'; +import { FeatureControl } from './feature.js'; +import { createLogger } from './logger.js'; + +const logger = createLogger('templates'); // Get __dirname equivalent in ESM const __filename = fileURLToPath(import.meta.url); @@ -19,11 +23,7 @@ export class TemplateManager { return TemplateManager.templates; } - static loadTemplates(): { - detected: number; - loaded: number; - errors: { file: string; error: string }[]; - } { + static loadTemplates(): void { const builtinTemplatePath = path.join(RESOURCE_DIR, 'templates'); const customTemplatesPath = path.join(getDataFolder(), 'templates'); @@ -41,11 +41,28 @@ export class TemplateManager { ...customTemplates.templates, ...builtinTemplates.templates, ]; - return { - detected: builtinTemplates.detected + customTemplates.detected, - loaded: builtinTemplates.loaded + customTemplates.loaded, - errors: [...builtinTemplates.errors, ...customTemplates.errors], - }; + const patternsInTemplates = this.templates.flatMap((template) => { + return [ + ...(template.config.excludedRegexPatterns || []), + ...(template.config.includedRegexPatterns || []), + ...(template.config.requiredRegexPatterns || []), + ...(template.config.preferredRegexPatterns || []).map( + (pattern) => pattern.pattern + ), + ]; + }); + const errors = [...builtinTemplates.errors, ...customTemplates.errors]; + logger.info( + `Loaded ${this.templates.length} templates from ${builtinTemplates.detected + customTemplates.detected} detected templates. ${patternsInTemplates.length} regex patterns detected. ${errors.length} errors occurred.` + ); + if (patternsInTemplates.length > 0) { + FeatureControl._addPatterns(patternsInTemplates); + } + if (errors.length > 0) { + logger.error( + `Errors loading templates: \n${errors.map((error) => ` ${error.file} - ${error.error}`).join('\n')}` + ); + } } private static loadTemplatesFromPath( @@ -83,7 +100,10 @@ export class TemplateManager { file: file, error: error instanceof ZodError - ? `Failed to parse template: ${formatZodError(error)}` + ? `Failed to parse template:\n${formatZodError(error) + .split('\n') + .map((line) => ' ' + line) + .join('\n')}` : `Failed to load template: ${error}`, }); } diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index b5543eec..e120ca32 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -71,15 +71,7 @@ async function initialiseProwlarr() { async function initialiseTemplates() { try { - const templates = TemplateManager.loadTemplates(); - logger.info( - `Loaded ${templates.loaded} templates from ${templates.detected} detected templates. ${templates.errors.length} errors occurred.` - ); - if (templates.errors.length > 0) { - logger.error( - `Errors loading templates: \n${templates.errors.map((error) => `- ${error.file}: ${error.error}`).join('\n')}` - ); - } + TemplateManager.loadTemplates(); } catch (error) { logger.error('Failed to initialise templates:', error); }