fix(feishu): truncate sanitized filenames at UTF-8 rune boundary
Raw byte truncation at 200 bytes splits multi-byte codepoints (Chinese characters are 3 bytes), producing invalid UTF-8 that downstream filename validation (utf8.ValidString) rejects with "文件名包含非法字符". Peel trailing bytes via DecodeLastRuneInString until the last rune is complete. Same latent bug and fix as the Yuque connector's sanitizeFileName.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
@@ -222,6 +223,22 @@ func TestSanitizeFileName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeFileName_TruncatesAtRuneBoundary(t *testing.T) {
|
||||
// Each 测 is 3 bytes; raw byte truncation at 200 would split a rune and
|
||||
// produce invalid UTF-8 that downstream filename validation rejects.
|
||||
long := strings.Repeat("测试", 100)
|
||||
got := sanitizeFileName(long)
|
||||
if !utf8.ValidString(got) {
|
||||
t.Fatalf("sanitizeFileName produced invalid UTF-8: %q", got)
|
||||
}
|
||||
if len(got) > 200 {
|
||||
t.Errorf("len = %d, want ≤ 200", len(got))
|
||||
}
|
||||
if len(got) == 0 {
|
||||
t.Error("result is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFeishuTimestamp(t *testing.T) {
|
||||
ts := parseFeishuTimestamp("1711468800") // 2024-03-27 00:00:00 UTC
|
||||
if ts.IsZero() {
|
||||
|
||||
Reference in New Issue
Block a user