feat(ingest): 源与目录文件名过滤及钉钉文档导出

- filename_filter_rules table (000035) with source-root and per-directory rows
- additive inheritance: source default + every ancestor directory contributes
- filename_is_filtered() SQL predicate applied to catalog browsing, subtree
  counts, change-set diffing, routing fan-out and artifact fetch manifests
- fetch_state 'filtered' (CHECK extended); reconcile cancels/restores fetch
  jobs, retires planned publications and blocks items when rules change
- GET/PUT/DELETE /api/v1/filename-filters (Idempotency-Key, normalized
  extensions) + Settings and Catalog filter editors
- failJobSQL no longer swallows the claimed row when the failure projection
  upserts (fixes spurious 'lease lost' retries)
- DingTalk live fetch: adoc/axls now use the export-job pipeline (HTTP 403
  from file/download fixed), header-signature downloads supported
This commit is contained in:
姚章浩
2026-09-15 09:46:31 +08:00
parent 2005e4e7da
commit b3c9331966
25 changed files with 1080 additions and 30 deletions
@@ -0,0 +1,13 @@
DROP FUNCTION IF EXISTS compare_catalog_change_set(VARCHAR);
ALTER FUNCTION compare_catalog_change_set_unfiltered(VARCHAR) RENAME TO compare_catalog_change_set;
UPDATE catalog_import_intents SET fetch_state='missing_local' WHERE fetch_state='filtered';
ALTER TABLE catalog_import_intents
DROP CONSTRAINT IF EXISTS catalog_import_intents_fetch_state_check;
ALTER TABLE catalog_import_intents
ADD CONSTRAINT catalog_import_intents_fetch_state_check
CHECK (fetch_state IN ('pending', 'fetching', 'ready', 'missing_local', 'failed'));
DROP FUNCTION IF EXISTS filename_is_filtered(VARCHAR, TEXT, TEXT);
DROP FUNCTION IF EXISTS filename_extension(TEXT);
DROP TABLE IF EXISTS filename_filter_rules;
@@ -0,0 +1,69 @@
-- Source/directory filename filters. Rules are additive: the root path is the
-- source default and every matching ancestor directory contributes exclusions.
CREATE TABLE IF NOT EXISTS filename_filter_rules (
source_connection_id VARCHAR(36) NOT NULL
REFERENCES source_connections(id) ON DELETE CASCADE,
directory_path TEXT NOT NULL DEFAULT '',
excluded_extensions TEXT[] NOT NULL,
updated_by VARCHAR(128) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (source_connection_id, directory_path),
CHECK (cardinality(excluded_extensions) BETWEEN 1 AND 100)
);
ALTER TABLE catalog_import_intents
DROP CONSTRAINT IF EXISTS catalog_import_intents_fetch_state_check;
ALTER TABLE catalog_import_intents
ADD CONSTRAINT catalog_import_intents_fetch_state_check
CHECK (fetch_state IN ('pending', 'fetching', 'ready', 'missing_local', 'failed', 'filtered'));
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;
-- Keep the existing complete classifier as the implementation, then remove
-- filtered names before recounting. This preserves all move/scope semantics.
ALTER FUNCTION compare_catalog_change_set(VARCHAR) RENAME TO compare_catalog_change_set_unfiltered;
CREATE FUNCTION compare_catalog_change_set(p_change_set_id VARCHAR(36)) RETURNS VOID AS $$
BEGIN
PERFORM compare_catalog_change_set_unfiltered(p_change_set_id);
DELETE FROM source_catalog_change_items item
USING source_catalog_change_sets change_set
WHERE item.change_set_id = p_change_set_id
AND change_set.id = item.change_set_id
AND filename_is_filtered(
change_set.source_connection_id,
COALESCE(NULLIF(item.new_path, ''), item.old_path),
item.title
);
UPDATE source_catalog_change_sets change_set SET
add_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='add'),
update_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='update'),
move_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='move'),
move_scope_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='move_scope'),
delete_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='delete'),
no_change_count = (SELECT COUNT(*) FROM source_catalog_change_items item WHERE item.change_set_id=p_change_set_id AND item.action='no_change')
WHERE change_set.id = p_change_set_id;
END
$$ LANGUAGE plpgsql;