feat: add regexes from custom/built-in templates to trusted regexes.

This commit is contained in:
Viren070
2025-10-19 21:14:08 +01:00
parent 56a02f97ce
commit feddab63a2
4 changed files with 48 additions and 31 deletions
+15 -11
View File
@@ -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);
}
}
+1
View File
@@ -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
+31 -11
View File
@@ -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}`,
});
}
+1 -9
View File
@@ -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);
}