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) <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. 'used' => $storageInfo['used'],
  59. 'usedSpacePercent' => (int)$storageInfo['relative'],
  60. 'owner' => $storageInfo['owner'],
  61. 'ownerDisplayName' => $storageInfo['ownerDisplayName'],
  62. 'mountType' => $storageInfo['mountType'],
  63. 'mountPoint' => $storageInfo['mountPoint'],
  64. ];
  65. }
  66. /**
  67. * Determine icon for a given file
  68. *
  69. * @param \OCP\Files\FileInfo $file file info
  70. * @return string icon URL
  71. */
  72. public static function determineIcon($file) {
  73. if ($file['type'] === 'dir') {
  74. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
  75. // TODO: move this part to the client side, using mountType
  76. if ($file->isShared()) {
  77. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
  78. } elseif ($file->isMounted()) {
  79. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
  80. }
  81. } else {
  82. $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
  83. }
  84. return substr($icon, 0, -3) . 'svg';
  85. }
  86. /**
  87. * Comparator function to sort files alphabetically and have
  88. * the directories appear first
  89. *
  90. * @param \OCP\Files\FileInfo $a file
  91. * @param \OCP\Files\FileInfo $b file
  92. * @return int -1 if $a must come before $b, 1 otherwise
  93. */
  94. public static function compareFileNames(FileInfo $a, FileInfo $b) {
  95. $aType = $a->getType();
  96. $bType = $b->getType();
  97. if ($aType === 'dir' and $bType !== 'dir') {
  98. return -1;
  99. } elseif ($aType !== 'dir' and $bType === 'dir') {
  100. return 1;
  101. } else {
  102. return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
  103. }
  104. }
  105. /**
  106. * Comparator function to sort files by date
  107. *
  108. * @param \OCP\Files\FileInfo $a file
  109. * @param \OCP\Files\FileInfo $b file
  110. * @return int -1 if $a must come before $b, 1 otherwise
  111. */
  112. public static function compareTimestamp(FileInfo $a, FileInfo $b) {
  113. $aTime = $a->getMTime();
  114. $bTime = $b->getMTime();
  115. return ($aTime < $bTime) ? -1 : 1;
  116. }
  117. /**
  118. * Comparator function to sort files by size
  119. *
  120. * @param \OCP\Files\FileInfo $a file
  121. * @param \OCP\Files\FileInfo $b file
  122. * @return int -1 if $a must come before $b, 1 otherwise
  123. */
  124. public static function compareSize(FileInfo $a, FileInfo $b) {
  125. $aSize = $a->getSize();
  126. $bSize = $b->getSize();
  127. return ($aSize < $bSize) ? -1 : 1;
  128. }
  129. /**
  130. * Formats the file info to be returned as JSON to the client.
  131. *
  132. * @param \OCP\Files\FileInfo $i
  133. * @return array formatted file info
  134. */
  135. public static function formatFileInfo(FileInfo $i) {
  136. $entry = [];
  137. $entry['id'] = $i['fileid'];
  138. $entry['parentId'] = $i['parent'];
  139. $entry['mtime'] = $i['mtime'] * 1000;
  140. // only pick out the needed attributes
  141. $entry['name'] = $i->getName();
  142. $entry['permissions'] = $i['permissions'];
  143. $entry['mimetype'] = $i['mimetype'];
  144. $entry['size'] = $i['size'];
  145. $entry['type'] = $i['type'];
  146. $entry['etag'] = $i['etag'];
  147. if (isset($i['tags'])) {
  148. $entry['tags'] = $i['tags'];
  149. }
  150. if (isset($i['displayname_owner'])) {
  151. $entry['shareOwner'] = $i['displayname_owner'];
  152. }
  153. if (isset($i['is_share_mount_point'])) {
  154. $entry['isShareMountPoint'] = $i['is_share_mount_point'];
  155. }
  156. $mountType = null;
  157. $mount = $i->getMountPoint();
  158. $mountType = $mount->getMountType();
  159. if ($mountType !== '') {
  160. if ($i->getInternalPath() === '') {
  161. $mountType .= '-root';
  162. }
  163. $entry['mountType'] = $mountType;
  164. }
  165. if (isset($i['extraData'])) {
  166. $entry['extraData'] = $i['extraData'];
  167. }
  168. return $entry;
  169. }
  170. /**
  171. * Format file info for JSON
  172. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  173. * @return array
  174. */
  175. public static function formatFileInfos($fileInfos) {
  176. $files = [];
  177. foreach ($fileInfos as $i) {
  178. $files[] = self::formatFileInfo($i);
  179. }
  180. return $files;
  181. }
  182. /**
  183. * Retrieves the contents of the given directory and
  184. * returns it as a sorted array of FileInfo.
  185. *
  186. * @param string $dir path to the directory
  187. * @param string $sortAttribute attribute to sort on
  188. * @param bool $sortDescending true for descending sort, false otherwise
  189. * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
  190. * @return \OCP\Files\FileInfo[] files
  191. */
  192. public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
  193. $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
  194. return self::sortFiles($content, $sortAttribute, $sortDescending);
  195. }
  196. /**
  197. * Populate the result set with file tags
  198. *
  199. * @param array $fileList
  200. * @param string $fileIdentifier identifier attribute name for values in $fileList
  201. * @param ITagManager $tagManager
  202. * @return array file list populated with tags
  203. */
  204. public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) {
  205. $ids = [];
  206. foreach ($fileList as $fileData) {
  207. $ids[] = $fileData[$fileIdentifier];
  208. }
  209. $tagger = $tagManager->load('files');
  210. $tags = $tagger->getTagsForObjects($ids);
  211. if (!is_array($tags)) {
  212. throw new \UnexpectedValueException('$tags must be an array');
  213. }
  214. // Set empty tag array
  215. foreach ($fileList as $key => $fileData) {
  216. $fileList[$key]['tags'] = [];
  217. }
  218. if (!empty($tags)) {
  219. foreach ($tags as $fileId => $fileTags) {
  220. foreach ($fileList as $key => $fileData) {
  221. if ($fileId !== $fileData[$fileIdentifier]) {
  222. continue;
  223. }
  224. $fileList[$key]['tags'] = $fileTags;
  225. }
  226. }
  227. }
  228. return $fileList;
  229. }
  230. /**
  231. * Sort the given file info array
  232. *
  233. * @param \OCP\Files\FileInfo[] $files files to sort
  234. * @param string $sortAttribute attribute to sort on
  235. * @param bool $sortDescending true for descending sort, false otherwise
  236. * @return \OCP\Files\FileInfo[] sorted files
  237. */
  238. public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
  239. $sortFunc = 'compareFileNames';
  240. if ($sortAttribute === 'mtime') {
  241. $sortFunc = 'compareTimestamp';
  242. } elseif ($sortAttribute === 'size') {
  243. $sortFunc = 'compareSize';
  244. }
  245. usort($files, [Helper::class, $sortFunc]);
  246. if ($sortDescending) {
  247. $files = array_reverse($files);
  248. }
  249. return $files;
  250. }
  251. }