123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace OCA\Files;
- use OC\Files\Filesystem;
- use OCP\Files\FileInfo;
- use OCP\Files\NotFoundException;
- use OCP\ITagManager;
- use OCP\Util;
- class Helper {
-
- public static function buildFileStorageStatistics($dir) {
-
- $storageInfo = \OC_Helper::getStorageInfo($dir);
- $l = Util::getL10N('files');
- $maxUploadFileSize = Util::maxUploadFilesize($dir, $storageInfo['free']);
- $maxHumanFileSize = Util::humanFileSize($maxUploadFileSize);
- $maxHumanFileSize = $l->t('Upload (max. %s)', [$maxHumanFileSize]);
- return [
- 'uploadMaxFilesize' => $maxUploadFileSize,
- 'maxHumanFilesize' => $maxHumanFileSize,
- 'freeSpace' => $storageInfo['free'],
- 'quota' => $storageInfo['quota'],
- 'total' => $storageInfo['total'],
- 'used' => $storageInfo['used'],
- 'usedSpacePercent' => $storageInfo['relative'],
- 'owner' => $storageInfo['owner'],
- 'ownerDisplayName' => $storageInfo['ownerDisplayName'],
- 'mountType' => $storageInfo['mountType'],
- 'mountPoint' => $storageInfo['mountPoint'],
- ];
- }
-
- public static function determineIcon($file) {
- if ($file['type'] === 'dir') {
- $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
-
- if ($file->isShared()) {
- $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
- } elseif ($file->isMounted()) {
- $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
- }
- } else {
- $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
- }
- return substr($icon, 0, -3) . 'svg';
- }
-
- public static function compareFileNames(FileInfo $a, FileInfo $b) {
- $aType = $a->getType();
- $bType = $b->getType();
- if ($aType === 'dir' and $bType !== 'dir') {
- return -1;
- } elseif ($aType !== 'dir' and $bType === 'dir') {
- return 1;
- } else {
- return Util::naturalSortCompare($a->getName(), $b->getName());
- }
- }
-
- public static function compareTimestamp(FileInfo $a, FileInfo $b) {
- $aTime = $a->getMTime();
- $bTime = $b->getMTime();
- return ($aTime < $bTime) ? -1 : 1;
- }
-
- public static function compareSize(FileInfo $a, FileInfo $b) {
- $aSize = $a->getSize();
- $bSize = $b->getSize();
- return ($aSize < $bSize) ? -1 : 1;
- }
-
- public static function formatFileInfo(FileInfo $i) {
- $entry = [];
- $entry['id'] = $i->getId();
- $entry['parentId'] = $i->getParentId();
- $entry['mtime'] = $i->getMtime() * 1000;
-
- $entry['name'] = $i->getName();
- $entry['permissions'] = $i->getPermissions();
- $entry['mimetype'] = $i->getMimetype();
- $entry['size'] = $i->getSize();
- $entry['type'] = $i->getType();
- $entry['etag'] = $i->getEtag();
-
-
- if (isset($i['tags'])) {
- $entry['tags'] = $i['tags'];
- }
- if (isset($i['displayname_owner'])) {
- $entry['shareOwner'] = $i['displayname_owner'];
- }
- if (isset($i['is_share_mount_point'])) {
- $entry['isShareMountPoint'] = $i['is_share_mount_point'];
- }
- if (isset($i['extraData'])) {
- $entry['extraData'] = $i['extraData'];
- }
- $mountType = null;
- $mount = $i->getMountPoint();
- $mountType = $mount->getMountType();
- if ($mountType !== '') {
- if ($i->getInternalPath() === '') {
- $mountType .= '-root';
- }
- $entry['mountType'] = $mountType;
- }
- return $entry;
- }
-
- public static function formatFileInfos($fileInfos) {
- $files = [];
- foreach ($fileInfos as $i) {
- $files[] = self::formatFileInfo($i);
- }
- return $files;
- }
-
- public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
- $content = Filesystem::getDirectoryContent($dir, $mimetypeFilter);
- return self::sortFiles($content, $sortAttribute, $sortDescending);
- }
-
- public static function populateTags(array $fileList, ITagManager $tagManager) {
- $tagger = $tagManager->load('files');
- $tags = $tagger->getTagsForObjects(array_map(static fn (array $fileData) => $fileData['file_source'], $fileList));
- if (!is_array($tags)) {
- throw new \UnexpectedValueException('$tags must be an array');
- }
-
- foreach ($fileList as &$fileData) {
- $fileData['tags'] = [];
- }
- unset($fileData);
- if (!empty($tags)) {
- foreach ($tags as $fileId => $fileTags) {
- foreach ($fileList as &$fileData) {
- if ($fileId !== $fileData['file_source']) {
- continue;
- }
- $fileData['tags'] = $fileTags;
- }
- unset($fileData);
- }
- }
- return $fileList;
- }
-
- 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;
- }
- }
|