Helper.php 7.4 KB

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