Skip to content

Commit 26e1c3c

Browse files
Merge pull request #63811 from nextcloud/backport/62698/stable34
[stable34] Trashbin truncate filename unicode
2 parents 9e80ca8 + 0eaf41a commit 26e1c3c

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

apps/files_trashbin/lib/Trashbin.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,12 +1246,13 @@ public static function getTrashFilename(string $filename, int $timestamp): strin
12461246
// oc_filecache `name` column has a limit of 250 chars
12471247
$maxLength = 250;
12481248
if ($length > $maxLength) {
1249-
$trashFilename = substr_replace(
1250-
$trashFilename,
1251-
'',
1252-
$maxLength / 2,
1253-
$length - $maxLength
1254-
);
1249+
// truncate at the middle, since the last characters are fairly likely to have meaningful information such as version numbering
1250+
1251+
$charsToRemove = $length - $maxLength + 1;
1252+
$charLength = mb_strlen($trashFilename);
1253+
$start = mb_substr($trashFilename, 0, intdiv($charLength, 2) - $charsToRemove);
1254+
$end = mb_substr($trashFilename, intdiv($charLength, 2));
1255+
return $start . '_' . $end;
12551256
}
12561257
return $trashFilename;
12571258
}

apps/files_trashbin/tests/StorageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function testSingleStorageDeleteFolder(): void {
227227
* Test that deleting a file with a long filename puts it into the trashbin.
228228
*/
229229
public function testSingleStorageDeleteLongFilename(): void {
230-
$truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
230+
$truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
231231

232232
$this->assertTrue($this->userView->file_exists(static::LONG_FILENAME));
233233
$this->userView->unlink(static::LONG_FILENAME);
@@ -246,7 +246,7 @@ public function testSingleStorageDeleteLongFilename(): void {
246246
* Test that deleting a file with the max filename length puts it into the trashbin.
247247
*/
248248
public function testSingleStorageDeleteMaxLengthFilename(): void {
249-
$truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
249+
$truncatedFilename = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt';
250250

251251
$this->assertTrue($this->userView->file_exists(static::MAX_FILENAME));
252252
$this->userView->unlink(static::MAX_FILENAME);

apps/files_trashbin/tests/TrashbinTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\IUserManager;
3232
use OCP\Server;
3333
use OCP\Share\IShare;
34+
use PHPUnit\Framework\Attributes\DataProvider;
3435

3536
/**
3637
* Class Test_Encryption
@@ -707,6 +708,32 @@ public static function loginHelper($user, $create = false) {
707708
\OC_Util::setupFS($user);
708709
Server::get(IRootFolder::class)->getUserFolder($user);
709710
}
711+
712+
public static function trashFilenameProvider(): array {
713+
return [
714+
['foo.txt', 'foo.txt.d1234'],
715+
[
716+
'a_very_long_filename_with_a_lot_a_characters_such_that_it_reaches_the_file_length_limit_and_would_cause_issues_if_we_just_appended_the_'
717+
. 'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt',
718+
'a_very_long_filename_with_a_lot_a_characters_such_that_it_reaches_the_file_length_limit_and_would_cause_issues_if_we_just_ded_the_'
719+
. 'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt.d1234'
720+
],
721+
[
722+
'a_very_long_filename_with_a_lot_a_characters_such_that_it_reaches_the_file_length_limit_and_would_cause_issues_if_we_just_äøšá_the_'
723+
. 'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt',
724+
'a_very_long_filename_with_a_lot_a_characters_such_that_it_reaches_the_file_length_limit_and_would_cause_issues_if_we_ju_á_the_'
725+
. 'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt.d1234'
726+
],
727+
];
728+
}
729+
730+
#[DataProvider(methodName: 'trashFilenameProvider')]
731+
public function testGetTrashFilename(string $filename, string $expected): void {
732+
$result = Trashbin::getTrashFilename($filename, 1234);
733+
$this->assertTrue(mb_check_encoding($result, 'UTF-8'));
734+
$this->assertEquals($expected, $result);
735+
$this->assertTrue(strlen($result) <= 250);
736+
}
710737
}
711738

712739

0 commit comments

Comments
 (0)