From 75d4ba2f931a5e6220fd16b5228353a0a2022ec0 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 26 Oct 2026 09:59:12 -0400 Subject: [PATCH 1/4] refactor(files_trashbin): move file sorting into trashbin helper The sorting logic in OCA\Files\Helpers is no longer used by anything other than the Trashbin app (it used to be used by the ancient Files PHP front-end / AJAX backend - e.g. `list.php`). - Move it from OCA\Files\Helper to OCA\Files_Trashbin\Helper. Signed-off-by: Josh --- apps/files_trashbin/lib/Helper.php | 44 +++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/apps/files_trashbin/lib/Helper.php b/apps/files_trashbin/lib/Helper.php index cdfa847633b14..5605503377e35 100644 --- a/apps/files_trashbin/lib/Helper.php +++ b/apps/files_trashbin/lib/Helper.php @@ -15,8 +15,50 @@ use OCP\Files\IMimeTypeDetector; use OCP\Files\Mount\IMountPoint; use OCP\Server; +use OCP\Util; class Helper { + /** + * Sort the given file info array. + * + * @param FileInfo[] $files + * @param string $sortAttribute + * @param bool $sortDescending + * @return FileInfo[] + */ + public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) { + $sortFunc = 'compareFileNames'; + if ($sortAttribute === 'mtime') { + $sortFunc = 'compareTimestamp'; + } elseif ($sortAttribute === 'size') { + $sortFunc = 'compareSize'; + } + usort($files, [self::class, $sortFunc]); + if ($sortDescending) { + $files = array_reverse($files); + } + return $files; + } + + private static function compareFileNames(FileInfo $a, FileInfo $b) { + $aType = $a->getType(); + $bType = $b->getType(); + if ($aType === 'dir' && $bType !== 'dir') { + return -1; + } elseif ($aType !== 'dir' && $bType === 'dir') { + return 1; + } + return Util::naturalSortCompare($a->getName(), $b->getName()); + } + + private static function compareTimestamp(FileInfo $a, FileInfo $b) { + return ($a->getMTime() < $b->getMTime()) ? -1 : 1; + } + + private static function compareSize(FileInfo $a, FileInfo $b) { + return ($a->getSize() < $b->getSize()) ? -1 : 1; + } + /** * Retrieves the contents of a trash bin directory. * @@ -48,7 +90,7 @@ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDesc } if ($sortAttribute !== '') { - return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); + return self::sortFiles($result, $sortAttribute, $sortDescending); } return $result; } From 9ed4a435a8784b3a32f4a148452157920f79c948 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 26 Oct 2026 10:12:20 -0400 Subject: [PATCH 2/4] test(files_trashbin): move trash specific file sorting helper tests Move the trash specific sorting helper tests into files_trashbin. Also add missing namespace. Signed-off-by: Josh --- apps/{files => files_trashbin}/tests/HelperTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename apps/{files => files_trashbin}/tests/HelperTest.php (97%) diff --git a/apps/files/tests/HelperTest.php b/apps/files_trashbin/tests/HelperTest.php similarity index 97% rename from apps/files/tests/HelperTest.php rename to apps/files_trashbin/tests/HelperTest.php index 776e72a729b07..9e0f52c769015 100644 --- a/apps/files/tests/HelperTest.php +++ b/apps/files_trashbin/tests/HelperTest.php @@ -7,8 +7,10 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +namespace OCA\Files_Trashbin\Tests; + use OC\Files\FileInfo; -use OCA\Files\Helper; +use OCA\Files_Trashbin\Helper; class HelperTest extends \Test\TestCase { private static function makeFileInfo($name, $size, $mtime, $isDir = false): FileInfo { From 248e743ba58850c06750b4197685e36d3af31016 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 26 Oct 2026 10:20:59 -0400 Subject: [PATCH 3/4] refactor(files): remove unused file listing and sorting helpers Remove sortFiles() and their comparator methods from OCA\Files\Helper now that trash-bin sorting is owned by OCA\Files_Trashbin\Helper. Remove getFiles() which was already dead code and the only remaining caller of sortFiles(). Related: Retaining formatFileInfo(), which is still used by some apps to format recent-file responses (see #65395). Signed-off-by: Josh --- apps/files/lib/Helper.php | 86 --------------------------------------- 1 file changed, 86 deletions(-) diff --git a/apps/files/lib/Helper.php b/apps/files/lib/Helper.php index be8f2229495a9..c79b60ecaf204 100644 --- a/apps/files/lib/Helper.php +++ b/apps/files/lib/Helper.php @@ -8,60 +8,12 @@ namespace OCA\Files; -use OC\Files\Filesystem; use OCP\Files\FileInfo; -use OCP\Util; /** * Helper class for manipulating file information */ class Helper { - /** - * Comparator function to sort files alphabetically and have - * the directories appear first - * - * @param FileInfo $a file - * @param FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareFileNames(FileInfo $a, FileInfo $b) { - $aType = $a->getType(); - $bType = $b->getType(); - if ($aType === 'dir' && $bType !== 'dir') { - return -1; - } elseif ($aType !== 'dir' && $bType === 'dir') { - return 1; - } else { - return Util::naturalSortCompare($a->getName(), $b->getName()); - } - } - - /** - * Comparator function to sort files by date - * - * @param FileInfo $a file - * @param FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareTimestamp(FileInfo $a, FileInfo $b) { - $aTime = $a->getMTime(); - $bTime = $b->getMTime(); - return ($aTime < $bTime) ? -1 : 1; - } - - /** - * Comparator function to sort files by size - * - * @param FileInfo $a file - * @param FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareSize(FileInfo $a, FileInfo $b) { - $aSize = $a->getSize(); - $bSize = $b->getSize(); - return ($aSize < $bSize) ? -1 : 1; - } - /** * Formats the file info to be returned as JSON to the client. * @@ -107,42 +59,4 @@ public static function formatFileInfo(FileInfo $i) { } return $entry; } - - /** - * Retrieves the contents of the given directory and - * returns it as a sorted array of FileInfo. - * - * @param string $dir path to the directory - * @param string $sortAttribute attribute to sort on - * @param bool $sortDescending true for descending sort, false otherwise - * @param string $mimetypeFilter limit returned content to this mimetype or mimepart - * @return FileInfo[] files - */ - public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') { - $content = Filesystem::getDirectoryContent($dir, $mimetypeFilter); - - return self::sortFiles($content, $sortAttribute, $sortDescending); - } - - /** - * Sort the given file info array - * - * @param FileInfo[] $files files to sort - * @param string $sortAttribute attribute to sort on - * @param bool $sortDescending true for descending sort, false otherwise - * @return FileInfo[] sorted files - */ - public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) { - $sortFunc = 'compareFileNames'; - if ($sortAttribute === 'mtime') { - $sortFunc = 'compareTimestamp'; - } elseif ($sortAttribute === 'size') { - $sortFunc = 'compareSize'; - } - usort($files, [Helper::class, $sortFunc]); - if ($sortDescending) { - $files = array_reverse($files); - } - return $files; - } } From 72815557fea37d6c8adf2d068b9cd6fd00faa8f5 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 26 Oct 2026 11:17:57 -0400 Subject: [PATCH 4/4] refactor(files_trashbin): type moved sorting helpers Add parameter and return types to the moved sorting helper and its comparators. Type the sorter against the public OCP\Files\FileInfo interface, aliased as IFileInfo, to make its accepted contract explicit. The alias distinguishes the public interface from the concrete OC\Files\FileInfo class, which buildFileInfo() constructs. Signed-off-by: Josh --- apps/files_trashbin/lib/Helper.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/files_trashbin/lib/Helper.php b/apps/files_trashbin/lib/Helper.php index 5605503377e35..9fa9315cdd605 100644 --- a/apps/files_trashbin/lib/Helper.php +++ b/apps/files_trashbin/lib/Helper.php @@ -12,6 +12,7 @@ use OC\Files\View; use OCP\Constants; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\FileInfo as IFileInfo; use OCP\Files\IMimeTypeDetector; use OCP\Files\Mount\IMountPoint; use OCP\Server; @@ -21,12 +22,10 @@ class Helper { /** * Sort the given file info array. * - * @param FileInfo[] $files - * @param string $sortAttribute - * @param bool $sortDescending - * @return FileInfo[] + * @param list $files + * @return list */ - public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) { + public static function sortFiles(array $files, string $sortAttribute = 'name', bool $sortDescending = false): array { $sortFunc = 'compareFileNames'; if ($sortAttribute === 'mtime') { $sortFunc = 'compareTimestamp'; @@ -40,7 +39,7 @@ public static function sortFiles($files, $sortAttribute = 'name', $sortDescendin return $files; } - private static function compareFileNames(FileInfo $a, FileInfo $b) { + private static function compareFileNames(IFileInfo $a, IFileInfo $b): int { $aType = $a->getType(); $bType = $b->getType(); if ($aType === 'dir' && $bType !== 'dir') { @@ -51,11 +50,11 @@ private static function compareFileNames(FileInfo $a, FileInfo $b) { return Util::naturalSortCompare($a->getName(), $b->getName()); } - private static function compareTimestamp(FileInfo $a, FileInfo $b) { + private static function compareTimestamp(IFileInfo $a, IFileInfo $b): int { return ($a->getMTime() < $b->getMTime()) ? -1 : 1; } - private static function compareSize(FileInfo $a, FileInfo $b) { + private static function compareSize(IFileInfo $a, IFileInfo $b): int { return ($a->getSize() < $b->getSize()) ? -1 : 1; }