Skip to content

Commit

Permalink
Filename validation: Allow (, ), [, ] and single spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
espebra committed Oct 6, 2024
1 parent 3193916 commit f9051db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions dbl/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions dbl/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down

0 comments on commit f9051db

Please sign in to comment.