1
0

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