From eab2cf9f67994dc8cf4b5ecf26e8f66c045db692 Mon Sep 17 00:00:00 2001 From: Thomas Lindop Date: Sat, 24 May 2025 10:58:03 +0100 Subject: [PATCH] >> deal with invalid UTF-8 in filenames > make restrictRuneset a method on context --- main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 98ef992..fcbc9ef 100644 --- a/main.go +++ b/main.go @@ -132,10 +132,10 @@ func isFatValid(r rune) bool { return false } -func restrictRuneset(s, strategy string) string { +func (ctx *context) restrictRuneset(s string) string { result := s toValidSubs := make(map[rune]string) - if strategy == "remove" { + if ctx.Strategy == "remove" { for _, r := range result { if !isFatValid(r) { toValidSubs[r] = "" @@ -250,7 +250,10 @@ func main() { for _, file := range ctx.FileList { oldName := fpath.Base(file) dirName := fpath.Dir(file) - newPath := fpath.Join(dirName, restrictRuneset(oldName, ctx.Strategy)) + newName := ctx.restrictRuneset( + strings.ToValidUTF8(oldName, "_INVALID_"), + ) + newPath := fpath.Join(dirName, newName) if newPath == file { continue }