helper.php 7.4 KB

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