From f9051dbb1c7ecc014b28d64c7f30ccca8618c427 Mon Sep 17 00:00:00 2001 From: Espen Braastad Date: Sun, 6 Oct 2024 22:56:18 +0200 Subject: [PATCH] Filename validation: Allow (, ), [, ] and single spaces --- dbl/file.go | 8 +++++--- dbl/file_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dbl/file.go b/dbl/file.go index 4470cdda..bc33bfdf 100644 --- a/dbl/file.go +++ b/dbl/file.go @@ -61,7 +61,7 @@ func (d *FileDao) ValidateInput(file *ds.File) error { //fmt.Printf("Character check: r=%q is a letter\n", r) return r // Allow certain other characters - case strings.ContainsAny(string(r), "-_=+,."): + case strings.ContainsAny(string(r), "-_=+,.()[] "): //fmt.Printf("Character check: r=%q is a valid character\n", r) return r } @@ -77,6 +77,9 @@ func (d *FileDao) ValidateInput(file *ds.File) error { n = strings.Replace(n, ".", "_", 1) } + // Replace redundant spaces with single spaces + n = strings.Join(strings.Fields(n), " ") + // Truncate long filenames // XXX: The maximum length could be made configurable if len(n) > 120 { @@ -86,8 +89,7 @@ func (d *FileDao) ValidateInput(file *ds.File) error { if file.Filename != n { // Log that the filename was modified. - fmt.Printf("Modifying filename during upload from %q to %q\n", file.Filename, n) - + fmt.Printf("Modifying filename during upload from %q to %q (%s)\n", file.Filename, n, file.Bin) } file.Filename = n diff --git a/dbl/file_test.go b/dbl/file_test.go index 0eee237e..a6b1a3f4 100644 --- a/dbl/file_test.go +++ b/dbl/file_test.go @@ -559,6 +559,14 @@ func TestUpsertWiderCharacterSet(t *testing.T) { InputFilename: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", Valid: true, ModifiedFilename: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + }, { + InputFilename: " a b ( ) [ ] ", + Valid: true, + ModifiedFilename: "a b ( ) [ ]", + }, { + InputFilename: "a b c d", + Valid: true, + ModifiedFilename: "a b c d", }, }