1
0

Helper.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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['fileid'];
  112. $entry['parentId'] = $i['parent'];
  113. $entry['mtime'] = $i['mtime'] * 1000;
  114. // only pick out the needed attributes
  115. $entry['name'] = $i->getName();
  116. $entry['permissions'] = $i['permissions'];
  117. $entry['mimetype'] = $i['mimetype'];
  118. $entry['size'] = $i['size'];
  119. $entry['type'] = $i['type'];
  120. $entry['etag'] = $i['etag'];
  121. if (isset($i['tags'])) {
  122. $entry['tags'] = $i['tags'];
  123. }
  124. if (isset($i['displayname_owner'])) {
  125. $entry['shareOwner'] = $i['displayname_owner'];
  126. }
  127. if (isset($i['is_share_mount_point'])) {
  128. $entry['isShareMountPoint'] = $i['is_share_mount_point'];
  129. }
  130. $mountType = null;
  131. $mount = $i->getMountPoint();
  132. $mountType = $mount->getMountType();
  133. if ($mountType !== '') {
  134. if ($i->getInternalPath() === '') {
  135. $mountType .= '-root';
  136. }
  137. $entry['mountType'] = $mountType;
  138. }
  139. if (isset($i['extraData'])) {
  140. $entry['extraData'] = $i['extraData'];
  141. }
  142. return $entry;
  143. }
  144. /**
  145. * Format file info for JSON
  146. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  147. * @return array
  148. */
  149. public static function formatFileInfos($fileInfos) {
  150. $files = [];
  151. foreach ($fileInfos as $i) {
  152. $files[] = self::formatFileInfo($i);
  153. }
  154. return $files;
  155. }
  156. /**
  157. * Retrieves the contents of the given directory and
  158. * returns it as a sorted array of FileInfo.
  159. *
  160. * @param string $dir path to the directory
  161. * @param string $sortAttribute attribute to sort on
  162. * @param bool $sortDescending true for descending sort, false otherwise
  163. * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
  164. * @return \OCP\Files\FileInfo[] files
  165. */
  166. public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
  167. $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
  168. return self::sortFiles($content, $sortAttribute, $sortDescending);
  169. }
  170. /**
  171. * Populate the result set with file tags
  172. *
  173. * @param array $fileList
  174. * @param string $fileIdentifier identifier attribute name for values in $fileList
  175. * @param ITagManager $tagManager
  176. * @return array file list populated with tags
  177. */
  178. public static function populateTags(array $fileList, $fileIdentifier, ITagManager $tagManager) {
  179. $ids = [];
  180. foreach ($fileList as $fileData) {
  181. $ids[] = $fileData[$fileIdentifier];
  182. }
  183. $tagger = $tagManager->load('files');
  184. $tags = $tagger->getTagsForObjects($ids);
  185. if (!is_array($tags)) {
  186. throw new \UnexpectedValueException('$tags must be an array');
  187. }
  188. // Set empty tag array
  189. foreach ($fileList as $key => $fileData) {
  190. $fileList[$key]['tags'] = [];
  191. }
  192. if (!empty($tags)) {
  193. foreach ($tags as $fileId => $fileTags) {
  194. foreach ($fileList as $key => $fileData) {
  195. if ($fileId !== $fileData[$fileIdentifier]) {
  196. continue;
  197. }
  198. $fileList[$key]['tags'] = $fileTags;
  199. }
  200. }
  201. }
  202. return $fileList;
  203. }
  204. /**
  205. * Sort the given file info array
  206. *
  207. * @param \OCP\Files\FileInfo[] $files files to sort
  208. * @param string $sortAttribute attribute to sort on
  209. * @param bool $sortDescending true for descending sort, false otherwise
  210. * @return \OCP\Files\FileInfo[] sorted files
  211. */
  212. public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
  213. $sortFunc = 'compareFileNames';
  214. if ($sortAttribute === 'mtime') {
  215. $sortFunc = 'compareTimestamp';
  216. } elseif ($sortAttribute === 'size') {
  217. $sortFunc = 'compareSize';
  218. }
  219. usort($files, [Helper::class, $sortFunc]);
  220. if ($sortDescending) {
  221. $files = array_reverse($files);
  222. }
  223. return $files;
  224. }
  225. }