Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1211,12 +1211,13 @@
// oc_filecache `name` column has a limit of 250 chars
$maxLength = 250;
if ($length > $maxLength) {
$trashFilename = substr_replace(
$trashFilename,
'',
$maxLength / 2,
$length - $maxLength
);
// truncate at the middle, since the last characters are fairly likely to have meaningful information such as version numbering

$charsToRemove = $length - $maxLength + 1;
$charLength = mb_strlen($trashFilename);
$start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove));

Check failure on line 1218 in apps/files_trashbin/lib/Trashbin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidScalarArgument

apps/files_trashbin/lib/Trashbin.php:1218:42: InvalidScalarArgument: Argument 3 of mb_substr expects int|null, but float provided (see https://psalm.dev/012)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apps/files_trashbin/lib/Trashbin.php:1218:42: InvalidScalarArgument: Argument 3 of mb_substr expects int|null, but float provided (see https://psalm.dev/012)

We don't have strict_types?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove));
$start = mb_substr($trashFilename, 0, intdiv($charLength, 2) - $charsToRemove));

$end = mb_substr($trashFilename, floor($charLength / 2));

Check failure on line 1219 in apps/files_trashbin/lib/Trashbin.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidScalarArgument

apps/files_trashbin/lib/Trashbin.php:1219:37: InvalidScalarArgument: Argument 2 of mb_substr expects int, but float provided (see https://psalm.dev/012)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$end = mb_substr($trashFilename, floor($charLength / 2));
$end = mb_substr($trashFilename, intdiv($charLength, 2));

return $start . '_' . $end;
Comment on lines +1215 to +1220

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$charsToRemove = $length - $maxLength + 1;
$charLength = mb_strlen($trashFilename);
$start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove));
$end = mb_substr($trashFilename, floor($charLength / 2));
return $start . '_' . $end;
$half = intdiv($maxLength - 1, 2); // One char for _
return mb_substr($trashFilename, 0, $half)
. '_'
. mb_substr($trashFilename, -$half);

}
return $trashFilename;
}
Expand Down
28 changes: 28 additions & 0 deletions apps/files_trashbin/tests/TrashbinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IUserManager;
use OCP\Server;
use OCP\Share\IShare;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* Class Test_Encryption
Expand Down Expand Up @@ -706,6 +707,33 @@ public static function loginHelper($user, $create = false) {
\OC_Util::setupFS($user);
Server::get(IRootFolder::class)->getUserFolder($user);
}

public static function trashFilenameProvider(): array {
return [
['foo.txt', 'foo.txt.d1234'],
[
'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_' .
'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt',
'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_' .
'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt.d1234'
],
[
'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_' .
'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt',
'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_' .
'timestamp_because_then_the_combined_length_would_overflow_the_column_limit_of_the_filecache_and_truncate_in_db.txt.d1234'
],
];
}


#[DataProvider(methodName: 'trashFilenameProvider')]
public function testGetTrashFilename(string $filename, string $expected): void {
$result = Trashbin::getTrashFilename($filename, 1234);
$this->assertTrue(mb_check_encoding($result, 'UTF-8'));
$this->assertEquals($expected, $result);
$this->assertTrue(strlen($result) <= 250);
}
}

// just a dummy class to make protected methods available for testing
Expand Down
Loading