123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace OCA\Files;
- use OCP\Files\FileInfo;
- use OCP\ITagManager;
- class Helper {
-
- public static function buildFileStorageStatistics($dir) {
-
- $storageInfo = \OC_Helper::getStorageInfo($dir);
- $l = \OCP\Util::getL10N('files');
- $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
- $maxHumanFileSize = \OCP\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 \OCP\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['fileid'];
- $entry['parentId'] = $i['parent'];
- $entry['mtime'] = $i['mtime'] * 1000;
-
- $entry['name'] = $i->getName();
- $entry['permissions'] = $i['permissions'];
- $entry['mimetype'] = $i['mimetype'];
- $entry['size'] = $i['size'];
- $entry['type'] = $i['type'];
- $entry['etag'] = $i['etag'];
- 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'];
- }
- $mountType = null;
- $mount = $i->getMountPoint();
- $mountType = $mount->getMountType();
- if ($mountType !== '') {
- if ($i->getInternalPath() === '') {
- $mountType .= '-root';
- }
- $entry['mountType'] = $mountType;
- }
- if (isset($i['extraData'])) {
- $entry['extraData'] = $i['extraData'];
- }
- 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 = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
- return self::sortFiles($content, $sortAttribute, $sortDescending);
- }
-
- public static function populateTags(array $fileList, $fileIdentifier, ITagManager $tagManager) {
- $ids = [];
- foreach ($fileList as $fileData) {
- $ids[] = $fileData[$fileIdentifier];
- }
- $tagger = $tagManager->load('files');
- $tags = $tagger->getTagsForObjects($ids);
- if (!is_array($tags)) {
- throw new \UnexpectedValueException('$tags must be an array');
- }
-
- foreach ($fileList as $key => $fileData) {
- $fileList[$key]['tags'] = [];
- }
- if (!empty($tags)) {
- foreach ($tags as $fileId => $fileTags) {
- foreach ($fileList as $key => $fileData) {
- if ($fileId !== $fileData[$fileIdentifier]) {
- continue;
- }
- $fileList[$key]['tags'] = $fileTags;
- }
- }
- }
- 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;
- }
- }
|