Helper.php 7.9 KB

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