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
86 changes: 0 additions & 86 deletions apps/files/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
43 changes: 42 additions & 1 deletion apps/files_trashbin/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,52 @@
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;
use OCP\Util;

class Helper {
/**
* Sort the given file info array.
*
* @param list<IFileInfo> $files
* @return list<IFileInfo>
*/
public static function sortFiles(array $files, string $sortAttribute = 'name', bool $sortDescending = false): array {
$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(IFileInfo $a, IFileInfo $b): int {
$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(IFileInfo $a, IFileInfo $b): int {
return ($a->getMTime() < $b->getMTime()) ? -1 : 1;
}

private static function compareSize(IFileInfo $a, IFileInfo $b): int {
return ($a->getSize() < $b->getSize()) ? -1 : 1;
}

/**
* Retrieves the contents of a trash bin directory.
*
Expand Down Expand Up @@ -48,7 +89,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading