BUG FileNameFilter should remove any amount of underscores from start of filename

When a user renames a file to "__test.txt" (two underscores or more),
then FileNameFilter will only remove the very first underscore from the
filename. This is not sufficient, as any number of underscores in the
filename will be problematic when Filesystem::sync() is called, it will
remove that File record thinking it's an internal file. This fixes it
so any number of underscores are stripped out at the start of the filename.
This commit is contained in:
Sean Harvey 2012-10-29 17:07:58 +13:00
parent 624f427c2a
commit 1ce279ec9d
2 changed files with 14 additions and 2 deletions

View File

@ -41,7 +41,7 @@ class FileNameFilter extends Object {
'/_/' => '-', // underscores to dashes '/_/' => '-', // underscores to dashes
'/[^A-Za-z0-9+.-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot '/[^A-Za-z0-9+.-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot
'/[\-]{2,}/' => '-', // remove duplicate dashes '/[\-]{2,}/' => '-', // remove duplicate dashes
'/^[\.\-_]/' => '', // Remove all leading dots, dashes or underscores '/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores
); );
/** /**

View File

@ -54,4 +54,16 @@ class FileNameFilterTest extends SapphireTest {
); );
} }
function testUnderscoresStartOfNameRemoved() {
$name = '_test.txt';
$filter = new FileNameFilter();
$this->assertEquals('test.txt', $filter->filter($name));
}
function testDoubleUnderscoresStartOfNameRemoved() {
$name = '__test.txt';
$filter = new FileNameFilter();
$this->assertEquals('test.txt', $filter->filter($name));
}
} }