You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <rmccorkell@karoshi.org.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files;
  30. use OCP\Files\FileInfo;
  31. /**
  32. * Helper class for manipulating file information
  33. */
  34. class Helper {
  35. /**
  36. * @param string $dir
  37. * @return array
  38. * @throws \OCP\Files\NotFoundException
  39. */
  40. public static function buildFileStorageStatistics($dir) {
  41. // information about storage capacities
  42. $storageInfo = \OC_Helper::getStorageInfo($dir);
  43. $l = new \OC_L10N('files');
  44. $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
  45. $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
  46. $maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize));
  47. return [
  48. 'uploadMaxFilesize' => $maxUploadFileSize,
  49. 'maxHumanFilesize' => $maxHumanFileSize,
  50. 'freeSpace' => $storageInfo['free'],
  51. 'usedSpacePercent' => (int)$storageInfo['relative']
  52. ];
  53. }
  54. /**
  55. * Determine icon for a given file
  56. *
  57. * @param \OCP\Files\FileInfo $file file info
  58. * @return string icon URL
  59. */
  60. public static function determineIcon($file) {
  61. if($file['type'] === 'dir') {
  62. $icon = \OC_Helper::mimetypeIcon('dir');
  63. // TODO: move this part to the client side, using mountType
  64. if ($file->isShared()) {
  65. $icon = \OC_Helper::mimetypeIcon('dir-shared');
  66. } elseif ($file->isMounted()) {
  67. $icon = \OC_Helper::mimetypeIcon('dir-external');
  68. }
  69. }else{
  70. $icon = \OC_Helper::mimetypeIcon($file->getMimetype());
  71. }
  72. return substr($icon, 0, -3) . 'svg';
  73. }
  74. /**
  75. * Comparator function to sort files alphabetically and have
  76. * the directories appear first
  77. *
  78. * @param \OCP\Files\FileInfo $a file
  79. * @param \OCP\Files\FileInfo $b file
  80. * @return int -1 if $a must come before $b, 1 otherwise
  81. */
  82. public static function compareFileNames(FileInfo $a, FileInfo $b) {
  83. $aType = $a->getType();
  84. $bType = $b->getType();
  85. if ($aType === 'dir' and $bType !== 'dir') {
  86. return -1;
  87. } elseif ($aType !== 'dir' and $bType === 'dir') {
  88. return 1;
  89. } else {
  90. return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
  91. }
  92. }
  93. /**
  94. * Comparator function to sort files by date
  95. *
  96. * @param \OCP\Files\FileInfo $a file
  97. * @param \OCP\Files\FileInfo $b file
  98. * @return int -1 if $a must come before $b, 1 otherwise
  99. */
  100. public static function compareTimestamp(FileInfo $a, FileInfo $b) {
  101. $aTime = $a->getMTime();
  102. $bTime = $b->getMTime();
  103. return ($aTime < $bTime) ? -1 : 1;
  104. }
  105. /**
  106. * Comparator function to sort files by size
  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 compareSize(FileInfo $a, FileInfo $b) {
  113. $aSize = $a->getSize();
  114. $bSize = $b->getSize();
  115. return ($aSize < $bSize) ? -1 : 1;
  116. }
  117. /**
  118. * Formats the file info to be returned as JSON to the client.
  119. *
  120. * @param \OCP\Files\FileInfo $i
  121. * @return array formatted file info
  122. */
  123. public static function formatFileInfo(FileInfo $i) {
  124. $entry = array();
  125. $entry['id'] = $i['fileid'];
  126. $entry['parentId'] = $i['parent'];
  127. $entry['date'] = \OCP\Util::formatDate($i['mtime']);
  128. $entry['mtime'] = $i['mtime'] * 1000;
  129. // only pick out the needed attributes
  130. $entry['icon'] = \OCA\Files\Helper::determineIcon($i);
  131. if (\OC::$server->getPreviewManager()->isAvailable($i)) {
  132. $entry['isPreviewAvailable'] = true;
  133. }
  134. $entry['name'] = $i->getName();
  135. $entry['permissions'] = $i['permissions'];
  136. $entry['mimetype'] = $i['mimetype'];
  137. $entry['size'] = $i['size'];
  138. $entry['type'] = $i['type'];
  139. $entry['etag'] = $i['etag'];
  140. if (isset($i['tags'])) {
  141. $entry['tags'] = $i['tags'];
  142. }
  143. if (isset($i['displayname_owner'])) {
  144. $entry['shareOwner'] = $i['displayname_owner'];
  145. }
  146. if (isset($i['is_share_mount_point'])) {
  147. $entry['isShareMountPoint'] = $i['is_share_mount_point'];
  148. }
  149. $mountType = null;
  150. if ($i->isShared()) {
  151. $mountType = 'shared';
  152. } else if ($i->isMounted()) {
  153. $mountType = 'external';
  154. }
  155. if ($mountType !== null) {
  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. * @return \OCP\Files\FileInfo[] files
  186. */
  187. public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false) {
  188. $content = \OC\Files\Filesystem::getDirectoryContent($dir);
  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. }