2013-01-18 18:31:09 -05:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2014-04-11 11:00:39 -04:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-04-11 11:00:39 -04:00
|
|
|
*/
|
2013-09-20 10:46:33 -04:00
|
|
|
namespace OCA\Files;
|
2013-01-18 18:31:09 -05:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
use OC\Files\Filesystem;
|
2014-10-24 08:13:40 -04:00
|
|
|
use OCP\Files\FileInfo;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\Util;
|
2014-10-24 08:13:40 -04:00
|
|
|
|
2014-04-11 11:00:39 -04:00
|
|
|
/**
|
|
|
|
|
* Helper class for manipulating file information
|
|
|
|
|
*/
|
2015-02-04 09:58:16 -05:00
|
|
|
class Helper {
|
2013-08-17 07:07:18 -04:00
|
|
|
/**
|
|
|
|
|
* Comparator function to sort files alphabetically and have
|
|
|
|
|
* the directories appear first
|
2014-03-19 08:53:59 -04:00
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param FileInfo $a file
|
|
|
|
|
* @param FileInfo $b file
|
2014-03-19 08:53:59 -04:00
|
|
|
* @return int -1 if $a must come before $b, 1 otherwise
|
2013-08-17 07:07:18 -04:00
|
|
|
*/
|
2014-10-24 08:13:40 -04:00
|
|
|
public static function compareFileNames(FileInfo $a, FileInfo $b) {
|
2014-03-19 08:53:59 -04:00
|
|
|
$aType = $a->getType();
|
|
|
|
|
$bType = $b->getType();
|
|
|
|
|
if ($aType === 'dir' && $bType !== 'dir') {
|
2013-08-17 07:07:18 -04:00
|
|
|
return -1;
|
2014-03-19 08:53:59 -04:00
|
|
|
} elseif ($aType !== 'dir' && $bType === 'dir') {
|
2013-08-17 07:07:18 -04:00
|
|
|
return 1;
|
|
|
|
|
} else {
|
2024-10-10 06:40:31 -04:00
|
|
|
return Util::naturalSortCompare($a->getName(), $b->getName());
|
2013-08-17 07:07:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 14:57:06 -04:00
|
|
|
/**
|
|
|
|
|
* Comparator function to sort files by date
|
|
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param FileInfo $a file
|
|
|
|
|
* @param FileInfo $b file
|
2014-04-03 14:57:06 -04:00
|
|
|
* @return int -1 if $a must come before $b, 1 otherwise
|
|
|
|
|
*/
|
2014-10-24 08:13:40 -04:00
|
|
|
public static function compareTimestamp(FileInfo $a, FileInfo $b) {
|
2014-04-03 14:57:06 -04:00
|
|
|
$aTime = $a->getMTime();
|
|
|
|
|
$bTime = $b->getMTime();
|
2014-10-08 16:40:57 -04:00
|
|
|
return ($aTime < $bTime) ? -1 : 1;
|
2014-04-03 14:57:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Comparator function to sort files by size
|
|
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param FileInfo $a file
|
|
|
|
|
* @param FileInfo $b file
|
2014-04-03 14:57:06 -04:00
|
|
|
* @return int -1 if $a must come before $b, 1 otherwise
|
|
|
|
|
*/
|
2014-10-24 08:13:40 -04:00
|
|
|
public static function compareSize(FileInfo $a, FileInfo $b) {
|
2014-04-03 14:57:06 -04:00
|
|
|
$aSize = $a->getSize();
|
|
|
|
|
$bSize = $b->getSize();
|
2014-10-07 19:17:45 -04:00
|
|
|
return ($aSize < $bSize) ? -1 : 1;
|
2014-04-03 14:57:06 -04:00
|
|
|
}
|
|
|
|
|
|
2013-08-17 07:07:18 -04:00
|
|
|
/**
|
2013-10-28 15:22:06 -04:00
|
|
|
* Formats the file info to be returned as JSON to the client.
|
|
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param FileInfo $i
|
2013-10-28 15:22:06 -04:00
|
|
|
* @return array formatted file info
|
2013-08-17 07:07:18 -04:00
|
|
|
*/
|
2014-10-24 08:13:40 -04:00
|
|
|
public static function formatFileInfo(FileInfo $i) {
|
2020-03-26 04:30:18 -04:00
|
|
|
$entry = [];
|
2013-08-17 07:07:18 -04:00
|
|
|
|
2024-09-05 17:00:27 -04:00
|
|
|
$entry['id'] = $i->getId();
|
|
|
|
|
$entry['parentId'] = $i->getParentId();
|
|
|
|
|
$entry['mtime'] = $i->getMtime() * 1000;
|
2013-10-28 15:22:06 -04:00
|
|
|
// only pick out the needed attributes
|
2014-05-27 08:24:35 -04:00
|
|
|
$entry['name'] = $i->getName();
|
2024-09-05 17:00:27 -04:00
|
|
|
$entry['permissions'] = $i->getPermissions();
|
|
|
|
|
$entry['mimetype'] = $i->getMimetype();
|
|
|
|
|
$entry['size'] = $i->getSize();
|
|
|
|
|
$entry['type'] = $i->getType();
|
|
|
|
|
$entry['etag'] = $i->getEtag();
|
|
|
|
|
// TODO: this is using the private implementation of FileInfo
|
|
|
|
|
// the array access is not part of the public interface
|
2014-11-18 12:53:45 -05:00
|
|
|
if (isset($i['tags'])) {
|
|
|
|
|
$entry['tags'] = $i['tags'];
|
|
|
|
|
}
|
2013-10-28 15:22:06 -04:00
|
|
|
if (isset($i['displayname_owner'])) {
|
|
|
|
|
$entry['shareOwner'] = $i['displayname_owner'];
|
2013-08-17 07:07:18 -04:00
|
|
|
}
|
2014-04-17 09:54:45 -04:00
|
|
|
if (isset($i['is_share_mount_point'])) {
|
|
|
|
|
$entry['isShareMountPoint'] = $i['is_share_mount_point'];
|
|
|
|
|
}
|
2024-09-05 17:00:27 -04:00
|
|
|
if (isset($i['extraData'])) {
|
|
|
|
|
$entry['extraData'] = $i['extraData'];
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-10 11:25:46 -04:00
|
|
|
$mountType = null;
|
2017-04-26 08:53:11 -04:00
|
|
|
$mount = $i->getMountPoint();
|
|
|
|
|
$mountType = $mount->getMountType();
|
|
|
|
|
if ($mountType !== '') {
|
2014-07-10 11:25:46 -04:00
|
|
|
if ($i->getInternalPath() === '') {
|
|
|
|
|
$mountType .= '-root';
|
|
|
|
|
}
|
|
|
|
|
$entry['mountType'] = $mountType;
|
|
|
|
|
}
|
2013-10-28 15:22:06 -04:00
|
|
|
return $entry;
|
|
|
|
|
}
|
2013-09-11 18:39:52 -04:00
|
|
|
|
2013-08-17 07:07:18 -04:00
|
|
|
/**
|
2013-10-28 15:22:06 -04:00
|
|
|
* Retrieves the contents of the given directory and
|
|
|
|
|
* returns it as a sorted array of FileInfo.
|
|
|
|
|
*
|
|
|
|
|
* @param string $dir path to the directory
|
2014-04-03 14:57:06 -04:00
|
|
|
* @param string $sortAttribute attribute to sort on
|
|
|
|
|
* @param bool $sortDescending true for descending sort, false otherwise
|
2015-04-13 04:35:45 -04:00
|
|
|
* @param string $mimetypeFilter limit returned content to this mimetype or mimepart
|
2024-10-18 06:04:22 -04:00
|
|
|
* @return FileInfo[] files
|
2013-08-17 07:07:18 -04:00
|
|
|
*/
|
2015-04-13 04:35:45 -04:00
|
|
|
public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
|
2024-10-10 06:40:31 -04:00
|
|
|
$content = Filesystem::getDirectoryContent($dir, $mimetypeFilter);
|
2013-10-28 15:22:06 -04:00
|
|
|
|
2014-04-03 14:57:06 -04:00
|
|
|
return self::sortFiles($content, $sortAttribute, $sortDescending);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sort the given file info array
|
|
|
|
|
*
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param FileInfo[] $files files to sort
|
2014-04-03 14:57:06 -04:00
|
|
|
* @param string $sortAttribute attribute to sort on
|
|
|
|
|
* @param bool $sortDescending true for descending sort, false otherwise
|
2024-10-18 06:04:22 -04:00
|
|
|
* @return FileInfo[] sorted files
|
2014-04-03 14:57:06 -04:00
|
|
|
*/
|
|
|
|
|
public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
|
2014-04-11 11:00:39 -04:00
|
|
|
$sortFunc = 'compareFileNames';
|
2014-04-03 14:57:06 -04:00
|
|
|
if ($sortAttribute === 'mtime') {
|
2014-04-11 11:00:39 -04:00
|
|
|
$sortFunc = 'compareTimestamp';
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($sortAttribute === 'size') {
|
2014-04-11 11:00:39 -04:00
|
|
|
$sortFunc = 'compareSize';
|
2014-04-03 14:57:06 -04:00
|
|
|
}
|
2020-03-26 04:30:18 -04:00
|
|
|
usort($files, [Helper::class, $sortFunc]);
|
2014-04-03 14:57:06 -04:00
|
|
|
if ($sortDescending) {
|
|
|
|
|
$files = array_reverse($files);
|
|
|
|
|
}
|
|
|
|
|
return $files;
|
2013-08-17 07:07:18 -04:00
|
|
|
}
|
2013-01-18 18:31:09 -05:00
|
|
|
}
|