fix(FilenameValidator): use `_` as default replacement for invalid chars

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
pull/53539/head
Ferdinand Thiessen 2025-06-17 12:25:54 +07:00
parent 3bc4e0ff28
commit 35a27b5fd5
No known key found for this signature in database
GPG Key ID: 45FAE7268762B400
4 changed files with 9 additions and 9 deletions

@ -54,7 +54,7 @@ Feature: Windows compatible filenames
And invoking occ with "files:windows-compatible-filenames --enable"
And invoking occ with "files:sanitize-filenames user0"
Then as "user0" the file "/2*2=4.txt" does not exist
And as "user0" the file "/2 2=4.txt" exists
And as "user0" the file "/2_2=4.txt" exists
Scenario: renaming a file with invalid character and replacement setup
Given As an "admin"

@ -232,7 +232,7 @@ class FilenameValidator implements IFilenameValidator {
$forbiddenCharacters = $this->getForbiddenCharacters();
if ($charReplacement === null) {
$charReplacement = array_diff([' ', '_', '-'], $forbiddenCharacters);
$charReplacement = array_diff(['_', '-', ' '], $forbiddenCharacters);
$charReplacement = reset($charReplacement) ?: '';
}
if (mb_strlen($charReplacement) !== 1) {

@ -43,7 +43,7 @@ interface IFilenameValidator {
* If no sanitizing is needed the same name is returned.
*
* @param string $name The filename to sanitize
* @param null|string $charReplacement Character to use for replacing forbidden ones - by default space, dash or underscore is used if allowed.
* @param null|string $charReplacement Character to use for replacing forbidden ones - by default underscore, dash or space is used if allowed.
* @throws \InvalidArgumentException if no character replacement was given (and the default could not be applied) or the replacement is not valid.
* @since 32.0.0
*/

@ -438,7 +438,7 @@ class FilenameValidatorTest extends TestCase {
'.thumbs.db', ['.htaccess'], ['.thumbs'], [], [], '.thumbs (renamed).db'
],
'invalid character' => [
'a: b.txt', ['.htaccess'], [], [], [':'], 'a b.txt',
'a: b.txt', ['.htaccess'], [], [], [':'], 'a_ b.txt',
],
'invalid extension' => [
'a: b.txt', ['.htaccess'], [], ['.txt'], [], 'a: b'
@ -492,13 +492,13 @@ class FilenameValidatorTest extends TestCase {
public static function dataSanitizeFilenameCharacterReplacement(): array {
return [
'default' => [
'foo*bar', ['*'], null, 'foo bar'
'foo*bar', ['*'], null, 'foo_bar'
],
'default - space not allowed' => [
'foo*bar', ['*', ' '], null, 'foo_bar'
'default - underscore not allowed' => [
'foo*bar', ['*', '_'], null, 'foo-bar'
],
'default - space and underscore not allowed' => [
'foo*bar', ['*', ' ', '_'], null, 'foo-bar'
'default - dash and underscore not allowed' => [
'foo*bar', ['*', '-', '_'], null, 'foo bar'
],
'default - no replacement' => [
'foo*bar', ['*', ' ', '_', '-'], null, null