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
+86
View File
@@ -0,0 +1,86 @@
package governance
import (
"errors"
"path/filepath"
"sort"
"strings"
"time"
)
var (
ErrInvalidFilenameFilter = errors.New("invalid filename filter")
ErrFilenameFilterTargetNotFound = errors.New("filename filter target not found")
)
// FilenameFilterRule is additive: the source rule uses an empty directory path;
// a directory inherits that rule plus every matching ancestor rule.
type FilenameFilterRule struct {
ConnectionID string `json:"connection_id"`
DirectoryPath string `json:"directory_path"`
ExcludedExtensions []string `json:"excluded_extensions"`
UpdatedBy string `json:"updated_by"`
UpdatedAt time.Time `json:"updated_at"`
}
type FilenameFilterMutation struct {
ConnectionID string
DirectoryPath string
ExcludedExtensions []string
OperatorRef string
Clear bool
Now time.Time
}
func NormalizeExcludedExtensions(values []string) ([]string, error) {
if len(values) == 0 || len(values) > 100 {
return nil, errors.New("at least one and no more than 100 extensions are required")
}
seen := make(map[string]bool, len(values))
result := make([]string, 0, len(values))
for _, raw := range values {
value := strings.ToLower(strings.TrimSpace(raw))
value = strings.TrimPrefix(value, ".")
if value == "" || len(value) > 16 {
return nil, errors.New("file extension is invalid")
}
for _, character := range value {
if character >= 'a' && character <= 'z' || character >= '0' && character <= '9' || character == '_' || character == '-' {
continue
}
return nil, errors.New("file extension is invalid")
}
if !seen[value] {
seen[value] = true
result = append(result, value)
}
}
sort.Strings(result)
return result, nil
}
func (request FilenameFilterMutation) Validate() error {
if strings.TrimSpace(request.ConnectionID) == "" || len(request.ConnectionID) > 36 || !validScopePath(request.DirectoryPath) || strings.TrimSpace(request.OperatorRef) == "" || len(request.OperatorRef) > 128 || request.Now.IsZero() {
return errors.New("filename filter target is invalid")
}
if request.Clear {
if len(request.ExcludedExtensions) != 0 {
return errors.New("clear filename filter must not include extensions")
}
return nil
}
normalized, err := NormalizeExcludedExtensions(request.ExcludedExtensions)
if err != nil || len(normalized) != len(request.ExcludedExtensions) {
return errors.New("excluded extensions must be normalized and unique")
}
for index := range normalized {
if normalized[index] != request.ExcludedExtensions[index] {
return errors.New("excluded extensions must be normalized and sorted")
}
}
return nil
}
func FilenameExtension(name string) string {
return strings.TrimPrefix(strings.ToLower(filepath.Ext(name)), ".")
}
@@ -0,0 +1,49 @@
package governance
import (
"reflect"
"testing"
"time"
)
func TestNormalizeExcludedExtensionsCanonicalizesAndDeduplicates(t *testing.T) {
got, err := NormalizeExcludedExtensions([]string{".ZIP", " rar ", "Mp4", "zip"})
if err != nil {
t.Fatal(err)
}
want := []string{"mp4", "rar", "zip"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("NormalizeExcludedExtensions() = %#v, want %#v", got, want)
}
}
func TestNormalizeExcludedExtensionsRejectsFilenamePatternsAndUnsafeValues(t *testing.T) {
for _, input := range [][]string{{"*.zip"}, {"a/b"}, {"zip;rar"}, {""}, {"this-extension-is-far-too-long"}} {
if _, err := NormalizeExcludedExtensions(input); err == nil {
t.Fatalf("NormalizeExcludedExtensions(%#v) succeeded", input)
}
}
}
func TestFilenameFilterMutationAcceptsSourceAndNestedDirectory(t *testing.T) {
now := time.Now()
for _, path := range []string{"", "02 产品资料/固件"} {
request := FilenameFilterMutation{ConnectionID: "conn-1", DirectoryPath: path, ExcludedExtensions: []string{"mp4", "rar", "zip"}, OperatorRef: "admin-1", Now: now}
if err := request.Validate(); err != nil {
t.Errorf("Validate(%q) error = %v", path, err)
}
}
}
func TestFilenameFilterMutationRejectsUnsafePathAndUnnormalizedExtension(t *testing.T) {
now := time.Now()
for _, request := range []FilenameFilterMutation{
{ConnectionID: "conn-1", DirectoryPath: "../private", ExcludedExtensions: []string{"zip"}, OperatorRef: "admin", Now: now},
{ConnectionID: "conn-1", ExcludedExtensions: []string{".zip"}, OperatorRef: "admin", Now: now},
{ConnectionID: "", ExcludedExtensions: []string{"zip"}, OperatorRef: "admin", Now: now},
} {
if err := request.Validate(); err == nil {
t.Fatalf("Validate(%#v) succeeded", request)
}
}
}