Helper.php 7.1 KB

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