Helper.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author brumsel <brumsel@losecatcher.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Michael Jobst <mjobst+github@tecratech.de>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\Files;
  35. use OCP\Files\FileInfo;
  36. use OCP\ITagManager;
  37. /**
  38. * Helper class for manipulating file information
  39. */
  40. class Helper {
  41. /**
  42. * @param string $dir
  43. * @return array
  44. * @throws \OCP\Files\NotFoundException
  45. */
  46. public static function buildFileStorageStatistics($dir) {
  47. // information about storage capacities
  48. $storageInfo = \OC_Helper::getStorageInfo($dir);
  49. $l = \OC::$server->getL10N('files');
  50. $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
  51. $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
  52. $maxHumanFileSize = $l->t('Upload (max. %s)', [$maxHumanFileSize]);
  53. return [
  54. 'uploadMaxFilesize' => $maxUploadFileSize,
  55. 'maxHumanFilesize' => $maxHumanFileSize,
  56. 'freeSpace' => $storageInfo['free'],
  57. 'quota' => $storageInfo['quota'],
  58. 'total' => $storageInfo['total'],
  59. 'used' => $storageInfo['used'],
  60. 'usedSpacePercent' => $storageInfo['relative'],
  61. 'owner' => $storageInfo['owner'],
  62. 'ownerDisplayName' => $storageInfo['ownerDisplayName'],
  63. 'mountType' => $storageInfo['mountType'],
  64. 'mountPoint' => $storageInfo['mountPoint'],
  65. ];
  66. }
  67. /**
  68. * Determine icon for a given file
  69. *
  70. * @param \OCP\Files\FileInfo $file file info
  71. * @return string icon URL
  72. */
  73. public static function determineIcon($file) {
  74. if ($file['type'] === 'dir') {
  75. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
  76. // TODO: move this part to the client side, using mountType
  77. if ($file->isShared()) {
  78. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
  79. } elseif ($file->isMounted()) {
  80. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
  81. }
  82. } else {
  83. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
  84. }
  85. return substr($icon, 0, -3) . 'svg';
  86. }
  87. /**
  88. * Comparator function to sort files alphabetically and have
  89. * the directories appear first
  90. *
  91. * @param \OCP\Files\FileInfo $a file
  92. * @param \OCP\Files\FileInfo $b file
  93. * @return int -1 if $a must come before $b, 1 otherwise
  94. */
  95. public static function compareFileNames(FileInfo $a, FileInfo $b) {
  96. $aType = $a->getType();
  97. $bType = $b->getType();
  98. if ($aType === 'dir' and $bType !== 'dir') {
  99. return -1;
  100. } elseif ($aType !== 'dir' and $bType === 'dir') {
  101. return 1;
  102. } else {
  103. return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
  104. }
  105. }
  106. /**
  107. * Comparator function to sort files by date
  108. *
  109. * @param \OCP\Files\FileInfo $a file
  110. * @param \OCP\Files\FileInfo $b file
  111. * @return int -1 if $a must come before $b, 1 otherwise
  112. */
  113. public static function compareTimestamp(FileInfo $a, FileInfo $b) {
  114. $aTime = $a->getMTime();
  115. $bTime = $b->getMTime();
  116. return ($aTime < $bTime) ? -1 : 1;
  117. }
  118. /**
  119. * Comparator function to sort files by size
  120. *
  121. * @param \OCP\Files\FileInfo $a file
  122. * @param \OCP\Files\FileInfo $b file
  123. * @return int -1 if $a must come before $b, 1 otherwise
  124. */
  125. public static function compareSize(FileInfo $a, FileInfo $b) {
  126. $aSize = $a->getSize();
  127. $bSize = $b->getSize();
  128. return ($aSize < $bSize) ? -1 : 1;
  129. }
  130. /**
  131. * Formats the file info to be returned as JSON to the client.
  132. *
  133. * @param \OCP\Files\FileInfo $i
  134. * @return array formatted file info
  135. */
  136. public static function formatFileInfo(FileInfo $i) {
  137. $entry = [];
  138. $entry['id'] = $i['fileid'];
  139. $entry['parentId'] = $i['parent'];
  140. $entry['mtime'] = $i['mtime'] * 1000;
  141. // only pick out the needed attributes
  142. $entry['name'] = $i->getName();
  143. $entry['permissions'] = $i['permissions'];
  144. $entry['mimetype'] = $i['mimetype'];
  145. $entry['size'] = $i['size'];
  146. $entry['type'] = $i['type'];
  147. $entry['etag'] = $i['etag'];
  148. if (isset($i['tags'])) {
  149. $entry['tags'] = $i['tags'];
  150. }
  151. if (isset($i['displayname_owner'])) {
  152. $entry['shareOwner'] = $i['displayname_owner'];
  153. }
  154. if (isset($i['is_share_mount_point'])) {
  155. $entry['isShareMountPoint'] = $i['is_share_mount_point'];
  156. }
  157. $mountType = null;
  158. $mount = $i->getMountPoint();
  159. $mountType = $mount->getMountType();
  160. if ($mountType !== '') {
  161. if ($i->getInternalPath() === '') {
  162. $mountType .= '-root';
  163. }
  164. $entry['mountType'] = $mountType;
  165. }
  166. if (isset($i['extraData'])) {
  167. $entry['extraData'] = $i['extraData'];
  168. }
  169. return $entry;
  170. }
  171. /**
  172. * Format file info for JSON
  173. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  174. * @return array
  175. */
  176. public static function formatFileInfos($fileInfos) {
  177. $files = [];
  178. foreach ($fileInfos as $i) {
  179. $files[] = self::formatFileInfo($i);
  180. }
  181. return $files;
  182. }
  183. /**
  184. * Retrieves the contents of the given directory and
  185. * returns it as a sorted array of FileInfo.
  186. *
  187. * @param string $dir path to the directory
  188. * @param string $sortAttribute attribute to sort on
  189. * @param bool $sortDescending true for descending sort, false otherwise
  190. * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
  191. * @return \OCP\Files\FileInfo[] files
  192. */
  193. public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
  194. $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
  195. return self::sortFiles($content, $sortAttribute, $sortDescending);
  196. }
  197. /**
  198. * Populate the result set with file tags
  199. *
  200. * @param array $fileList
  201. * @param string $fileIdentifier identifier attribute name for values in $fileList
  202. * @param ITagManager $tagManager
  203. * @return array file list populated with tags
  204. */
  205. public static function populateTags(array $fileList, $fileIdentifier, ITagManager $tagManager) {
  206. $ids = [];
  207. foreach ($fileList as $fileData) {
  208. $ids[] = $fileData[$fileIdentifier];
  209. }
  210. $tagger = $tagManager->load('files');
  211. $tags = $tagger->getTagsForObjects($ids);
  212. if (!is_array($tags)) {
  213. throw new \UnexpectedValueException('$tags must be an array');
  214. }
  215. // Set empty tag array
  216. foreach ($fileList as $key => $fileData) {
  217. $fileList[$key]['tags'] = [];
  218. }
  219. if (!empty($tags)) {
  220. foreach ($tags as $fileId => $fileTags) {
  221. foreach ($fileList as $key => $fileData) {
  222. if ($fileId !== $fileData[$fileIdentifier]) {
  223. continue;
  224. }
  225. $fileList[$key]['tags'] = $fileTags;
  226. }
  227. }
  228. }
  229. return $fileList;
  230. }
  231. /**
  232. * Sort the given file info array
  233. *
  234. * @param \OCP\Files\FileInfo[] $files files to sort
  235. * @param string $sortAttribute attribute to sort on
  236. * @param bool $sortDescending true for descending sort, false otherwise
  237. * @return \OCP\Files\FileInfo[] sorted files
  238. */
  239. public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
  240. $sortFunc = 'compareFileNames';
  241. if ($sortAttribute === 'mtime') {
  242. $sortFunc = 'compareTimestamp';
  243. } elseif ($sortAttribute === 'size') {
  244. $sortFunc = 'compareSize';
  245. }
  246. usort($files, [Helper::class, $sortFunc]);
  247. if ($sortDescending) {
  248. $files = array_reverse($files);
  249. }
  250. return $files;
  251. }
  252. }