feat(filters): allowlist mode for filename filters

- filter rules gain mode ('exclude'|'allow'); nearest allow rule governs a
  directory subtree, extensions outside it are filtered before fetch
- filename_extension() now requires a well-formed suffix, so dated names like
  'CRA_V3.1手册_2024-07-31_en' no longer fake an extension; extensionless
  files are never filtered
- API accepts {mode, extensions}; UI editor becomes grouped format chips with
  keep/exclude modes (keep-list default: docs/text/scripts/images)
- migration 000036 + integration tests for allow, nesting and restore
This commit is contained in:
姚章浩
2026-09-15 10:31:55 +08:00
parent f7c65571e4
commit e6ef5a4693
13 changed files with 360 additions and 54 deletions
@@ -0,0 +1,27 @@
-- Restore the 000035 exclude-only evaluator and loose extension parser.
-- Allow rules cannot be expressed as a blocklist, so they are dropped.
DELETE FROM filename_filter_rules WHERE mode = 'allow';
CREATE OR REPLACE FUNCTION filename_extension(p_name TEXT) RETURNS TEXT AS $$
SELECT CASE
WHEN COALESCE(p_name, '') ~ '\.[^.]+$'
THEN LOWER(SUBSTRING(p_name FROM '\.([^.]+)$'))
ELSE ''
END
$$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION filename_is_filtered(
p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT
) RETURNS BOOLEAN AS $$
SELECT EXISTS (
SELECT 1
FROM filename_filter_rules rule
CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension
WHERE rule.source_connection_id = p_connection_id
AND (rule.directory_path = ''
OR p_relative_path LIKE rule.directory_path || '/%')
AND extension = filename_extension(p_name)
)
$$ LANGUAGE SQL STABLE PARALLEL SAFE;
ALTER TABLE filename_filter_rules DROP COLUMN IF EXISTS mode;
@@ -0,0 +1,51 @@
-- Filename filters gain a mode: 'allow' turns a rule into a whitelist (the
-- nearest matching allow rule governs a file and only listed extensions may
-- be ingested), 'exclude' keeps the legacy additive blocklist. Both modes can
-- coexist; a file must pass both. Files with no recognizable extension are
-- never filtered (extensionless names, dates-with-dots, "手册 V2.1" style).
ALTER TABLE filename_filter_rules
ADD COLUMN mode VARCHAR(10) NOT NULL DEFAULT 'exclude'
CHECK (mode IN ('exclude', 'allow'));
-- Only well-formed suffixes count as extensions; 'CRA_V3.1手册_2024-07-31_en'
-- style names with stray dots no longer produce fake extensions.
CREATE OR REPLACE FUNCTION filename_extension(p_name TEXT) RETURNS TEXT AS $$
SELECT CASE
WHEN COALESCE(p_name, '') ~ '\.[^.]+$'
AND LOWER(SUBSTRING(p_name FROM '\.([^.]+)$')) ~ '^[a-z0-9][a-z0-9_-]{0,15}$'
THEN LOWER(SUBSTRING(p_name FROM '\.([^.]+)$'))
ELSE ''
END
$$ LANGUAGE SQL IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION filename_is_filtered(
p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT
) RETURNS BOOLEAN AS $$
WITH nearest_allow AS (
SELECT rule.excluded_extensions AS extensions
FROM filename_filter_rules rule
WHERE rule.source_connection_id = p_connection_id
AND rule.mode = 'allow'
AND (rule.directory_path = ''
OR p_relative_path LIKE rule.directory_path || '/%')
ORDER BY LENGTH(rule.directory_path) DESC
LIMIT 1
)
SELECT CASE
WHEN filename_extension(p_name) = '' THEN FALSE
WHEN EXISTS (
SELECT 1
FROM filename_filter_rules rule
CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension
WHERE rule.source_connection_id = p_connection_id
AND rule.mode = 'exclude'
AND (rule.directory_path = ''
OR p_relative_path LIKE rule.directory_path || '/%')
AND extension = filename_extension(p_name)
) THEN TRUE
WHEN EXISTS (SELECT 1 FROM nearest_allow)
THEN NOT EXISTS (SELECT 1 FROM nearest_allow
WHERE filename_extension(p_name) = ANY(extensions))
ELSE FALSE
END
$$ LANGUAGE SQL STABLE PARALLEL SAFE;