helper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace OCA\Files;
  3. class Helper
  4. {
  5. public static function buildFileStorageStatistics($dir) {
  6. $l = new \OC_L10N('files');
  7. $maxUploadFilesize = \OCP\Util::maxUploadFilesize($dir);
  8. $maxHumanFilesize = \OCP\Util::humanFileSize($maxUploadFilesize);
  9. $maxHumanFilesize = $l->t('Upload') . ' max. ' . $maxHumanFilesize;
  10. // information about storage capacities
  11. $storageInfo = \OC_Helper::getStorageInfo($dir);
  12. return array('uploadMaxFilesize' => $maxUploadFilesize,
  13. 'maxHumanFilesize' => $maxHumanFilesize,
  14. 'usedSpacePercent' => (int)$storageInfo['relative']);
  15. }
  16. public static function determineIcon($file) {
  17. if($file['type'] === 'dir') {
  18. $dir = $file['directory'];
  19. $absPath = \OC\Files\Filesystem::getView()->getAbsolutePath($dir.'/'.$file['name']);
  20. $mount = \OC\Files\Filesystem::getMountManager()->find($absPath);
  21. if (!is_null($mount)) {
  22. $sid = $mount->getStorageId();
  23. if (!is_null($sid)) {
  24. $sid = explode(':', $sid);
  25. if ($sid[0] === 'shared') {
  26. return \OC_Helper::mimetypeIcon('dir-shared');
  27. }
  28. if ($sid[0] !== 'local' and $sid[0] !== 'home') {
  29. return \OC_Helper::mimetypeIcon('dir-external');
  30. }
  31. }
  32. }
  33. return \OC_Helper::mimetypeIcon('dir');
  34. }
  35. if($file['isPreviewAvailable']) {
  36. $pathForPreview = $file['directory'] . '/' . $file['name'];
  37. return \OC_Helper::previewIcon($pathForPreview) . '&c=' . $file['etag'];
  38. }
  39. return \OC_Helper::mimetypeIcon($file['mimetype']);
  40. }
  41. /**
  42. * Comparator function to sort files alphabetically and have
  43. * the directories appear first
  44. * @param array $a file
  45. * @param array $b file
  46. * @return -1 if $a must come before $b, 1 otherwise
  47. */
  48. public static function fileCmp($a, $b) {
  49. if ($a['type'] === 'dir' and $b['type'] !== 'dir') {
  50. return -1;
  51. } elseif ($a['type'] !== 'dir' and $b['type'] === 'dir') {
  52. return 1;
  53. } else {
  54. return strnatcasecmp($a['name'], $b['name']);
  55. }
  56. }
  57. /**
  58. * Retrieves the contents of the given directory and
  59. * returns it as a sorted array.
  60. * @param string $dir path to the directory
  61. * @return array of files
  62. */
  63. public static function getFiles($dir) {
  64. $content = \OC\Files\Filesystem::getDirectoryContent($dir);
  65. $files = array();
  66. foreach ($content as $i) {
  67. $i['date'] = \OCP\Util::formatDate($i['mtime']);
  68. if ($i['type'] === 'file') {
  69. $fileinfo = pathinfo($i['name']);
  70. $i['basename'] = $fileinfo['filename'];
  71. if (!empty($fileinfo['extension'])) {
  72. $i['extension'] = '.' . $fileinfo['extension'];
  73. } else {
  74. $i['extension'] = '';
  75. }
  76. }
  77. $i['directory'] = $dir;
  78. $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($i['mimetype']);
  79. $i['icon'] = \OCA\Files\Helper::determineIcon($i);
  80. $files[] = $i;
  81. }
  82. usort($files, array('\OCA\Files\Helper', 'fileCmp'));
  83. return $files;
  84. }
  85. /**
  86. * Splits the given path into a breadcrumb structure.
  87. * @param string $dir path to process
  88. * @return array where each entry is a hash of the absolute
  89. * directory path and its name
  90. */
  91. public static function makeBreadcrumb($dir){
  92. $breadcrumb = array();
  93. $pathtohere = '';
  94. foreach (explode('/', $dir) as $i) {
  95. if ($i !== '') {
  96. $pathtohere .= '/' . $i;
  97. $breadcrumb[] = array('dir' => $pathtohere, 'name' => $i);
  98. }
  99. }
  100. return $breadcrumb;
  101. }
  102. /**
  103. * Returns the numeric permissions for the given directory.
  104. * @param string $dir directory without trailing slash
  105. * @return numeric permissions
  106. */
  107. public static function getDirPermissions($dir){
  108. $permissions = \OCP\PERMISSION_READ;
  109. if (\OC\Files\Filesystem::isCreatable($dir . '/')) {
  110. $permissions |= \OCP\PERMISSION_CREATE;
  111. }
  112. if (\OC\Files\Filesystem::isUpdatable($dir . '/')) {
  113. $permissions |= \OCP\PERMISSION_UPDATE;
  114. }
  115. if (\OC\Files\Filesystem::isDeletable($dir . '/')) {
  116. $permissions |= \OCP\PERMISSION_DELETE;
  117. }
  118. if (\OC\Files\Filesystem::isSharable($dir . '/')) {
  119. $permissions |= \OCP\PERMISSION_SHARE;
  120. }
  121. return $permissions;
  122. }
  123. }