Helper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files;
  8. use OCP\Files\FileInfo;
  9. use OCP\ITagManager;
  10. /**
  11. * Helper class for manipulating file information
  12. */
  13. class Helper {
  14. /**
  15. * @param string $dir
  16. * @return array
  17. * @throws \OCP\Files\NotFoundException
  18. */
  19. public static function buildFileStorageStatistics($dir) {
  20. // information about storage capacities
  21. $storageInfo = \OC_Helper::getStorageInfo($dir);
  22. $l = \OCP\Util::getL10N('files');
  23. $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
  24. $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
  25. $maxHumanFileSize = $l->t('Upload (max. %s)', [$maxHumanFileSize]);
  26. return [
  27. 'uploadMaxFilesize' => $maxUploadFileSize,
  28. 'maxHumanFilesize' => $maxHumanFileSize,
  29. 'freeSpace' => $storageInfo['free'],
  30. 'quota' => $storageInfo['quota'],
  31. 'total' => $storageInfo['total'],
  32. 'used' => $storageInfo['used'],
  33. 'usedSpacePercent' => $storageInfo['relative'],
  34. 'owner' => $storageInfo['owner'],
  35. 'ownerDisplayName' => $storageInfo['ownerDisplayName'],
  36. 'mountType' => $storageInfo['mountType'],
  37. 'mountPoint' => $storageInfo['mountPoint'],
  38. ];
  39. }
  40. /**
  41. * Determine icon for a given file
  42. *
  43. * @param \OCP\Files\FileInfo $file file info
  44. * @return string icon URL
  45. */
  46. public static function determineIcon($file) {
  47. if ($file['type'] === 'dir') {
  48. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
  49. // TODO: move this part to the client side, using mountType
  50. if ($file->isShared()) {
  51. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
  52. } elseif ($file->isMounted()) {
  53. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
  54. }
  55. } else {
  56. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
  57. }
  58. return substr($icon, 0, -3) . 'svg';
  59. }
  60. /**
  61. * Comparator function to sort files alphabetically and have
  62. * the directories appear first
  63. *
  64. * @param \OCP\Files\FileInfo $a file
  65. * @param \OCP\Files\FileInfo $b file
  66. * @return int -1 if $a must come before $b, 1 otherwise
  67. */
  68. public static function compareFileNames(FileInfo $a, FileInfo $b) {
  69. $aType = $a->getType();
  70. $bType = $b->getType();
  71. if ($aType === 'dir' and $bType !== 'dir') {
  72. return -1;
  73. } elseif ($aType !== 'dir' and $bType === 'dir') {
  74. return 1;
  75. } else {
  76. return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
  77. }
  78. }
  79. /**
  80. * Comparator function to sort files by date
  81. *
  82. * @param \OCP\Files\FileInfo $a file
  83. * @param \OCP\Files\FileInfo $b file
  84. * @return int -1 if $a must come before $b, 1 otherwise
  85. */
  86. public static function compareTimestamp(FileInfo $a, FileInfo $b) {
  87. $aTime = $a->getMTime();
  88. $bTime = $b->getMTime();
  89. return ($aTime < $bTime) ? -1 : 1;
  90. }
  91. /**
  92. * Comparator function to sort files by size
  93. *
  94. * @param \OCP\Files\FileInfo $a file
  95. * @param \OCP\Files\FileInfo $b file
  96. * @return int -1 if $a must come before $b, 1 otherwise
  97. */
  98. public static function compareSize(FileInfo $a, FileInfo $b) {
  99. $aSize = $a->getSize();
  100. $bSize = $b->getSize();
  101. return ($aSize < $bSize) ? -1 : 1;
  102. }
  103. /**
  104. * Formats the file info to be returned as JSON to the client.
  105. *
  106. * @param \OCP\Files\FileInfo $i
  107. * @return array formatted file info
  108. */
  109. public static function formatFileInfo(FileInfo $i) {
  110. $entry = [];
  111. $entry['id'] = $i->getId();
  112. $entry['parentId'] = $i->getParentId();
  113. $entry['mtime'] = $i->getMtime() * 1000;
  114. // only pick out the needed attributes
  115. $entry['name'] = $i->getName();
  116. $entry['permissions'] = $i->getPermissions();
  117. $entry['mimetype'] = $i->getMimetype();
  118. $entry['size'] = $i->getSize();
  119. $entry['type'] = $i->getType();
  120. $entry['etag'] = $i->getEtag();
  121. // TODO: this is using the private implementation of FileInfo
  122. // the array access is not part of the public interface
  123. if (isset($i['tags'])) {
  124. $entry['tags'] = $i['tags'];
  125. }
  126. if (isset($i['displayname_owner'])) {
  127. $entry['shareOwner'] = $i['displayname_owner'];
  128. }
  129. if (isset($i['is_share_mount_point'])) {
  130. $entry['isShareMountPoint'] = $i['is_share_mount_point'];
  131. }
  132. if (isset($i['extraData'])) {
  133. $entry['extraData'] = $i['extraData'];
  134. }
  135. $mountType = null;
  136. $mount = $i->getMountPoint();
  137. $mountType = $mount->getMountType();
  138. if ($mountType !== '') {
  139. if ($i->getInternalPath() === '') {
  140. $mountType .= '-root';
  141. }
  142. $entry['mountType'] = $mountType;
  143. }
  144. return $entry;
  145. }
  146. /**
  147. * Format file info for JSON
  148. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  149. * @return array
  150. */
  151. public static function formatFileInfos($fileInfos) {
  152. $files = [];
  153. foreach ($fileInfos as $i) {
  154. $files[] = self::formatFileInfo($i);
  155. }
  156. return $files;
  157. }
  158. /**
  159. * Retrieves the contents of the given directory and
  160. * returns it as a sorted array of FileInfo.
  161. *
  162. * @param string $dir path to the directory
  163. * @param string $sortAttribute attribute to sort on
  164. * @param bool $sortDescending true for descending sort, false otherwise
  165. * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
  166. * @return \OCP\Files\FileInfo[] files
  167. */
  168. public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
  169. $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
  170. return self::sortFiles($content, $sortAttribute, $sortDescending);
  171. }
  172. /**
  173. * Populate the result set with file tags
  174. *
  175. * @param array $fileList
  176. * @param string $fileIdentifier identifier attribute name for values in $fileList
  177. * @param ITagManager $tagManager
  178. * @return array file list populated with tags
  179. */
  180. public static function populateTags(array $fileList, $fileIdentifier, ITagManager $tagManager) {
  181. $ids = [];
  182. foreach ($fileList as $fileData) {
  183. $ids[] = $fileData[$fileIdentifier];
  184. }
  185. $tagger = $tagManager->load('files');
  186. $tags = $tagger->getTagsForObjects($ids);
  187. if (!is_array($tags)) {
  188. throw new \UnexpectedValueException('$tags must be an array');
  189. }
  190. // Set empty tag array
  191. foreach ($fileList as $key => $fileData) {
  192. $fileList[$key]['tags'] = [];
  193. }
  194. if (!empty($tags)) {
  195. foreach ($tags as $fileId => $fileTags) {
  196. foreach ($fileList as $key => $fileData) {
  197. if ($fileId !== $fileData[$fileIdentifier]) {
  198. continue;
  199. }
  200. $fileList[$key]['tags'] = $fileTags;
  201. }
  202. }
  203. }
  204. return $fileList;
  205. }
  206. /**
  207. * Sort the given file info array
  208. *
  209. * @param \OCP\Files\FileInfo[] $files files to sort
  210. * @param string $sortAttribute attribute to sort on
  211. * @param bool $sortDescending true for descending sort, false otherwise
  212. * @return \OCP\Files\FileInfo[] sorted files
  213. */
  214. public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
  215. $sortFunc = 'compareFileNames';
  216. if ($sortAttribute === 'mtime') {
  217. $sortFunc = 'compareTimestamp';
  218. } elseif ($sortAttribute === 'size') {
  219. $sortFunc = 'compareSize';
  220. }
  221. usort($files, [Helper::class, $sortFunc]);
  222. if ($sortDescending) {
  223. $files = array_reverse($files);
  224. }
  225. return $files;
  226. }
  227. }