PageRenderTime 48ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/files_trashbin/lib/helper.php

https://gitlab.com/wuhang2003/core
PHP | 129 lines | 79 code | 10 blank | 40 comment | 14 complexity | 3fcf1bb30a0b805f9932e3bcaffb1493 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Trashbin;
  28. use OC\Files\FileInfo;
  29. use OCP\Constants;
  30. class Helper
  31. {
  32. /**
  33. * Retrieves the contents of a trash bin directory.
  34. *
  35. * @param string $dir path to the directory inside the trashbin
  36. * or empty to retrieve the root of the trashbin
  37. * @param string $user
  38. * @param string $sortAttribute attribute to sort on or empty to disable sorting
  39. * @param bool $sortDescending true for descending sort, false otherwise
  40. * @return \OCP\Files\FileInfo[]
  41. */
  42. public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){
  43. $result = array();
  44. $timestamp = null;
  45. $view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
  46. if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
  47. throw new \Exception('Directory does not exists');
  48. }
  49. $dirContent = $view->opendir($dir);
  50. if ($dirContent === false) {
  51. return $result;
  52. }
  53. $mount = $view->getMount($dir);
  54. $storage = $mount->getStorage();
  55. $absoluteDir = $view->getAbsolutePath($dir);
  56. $internalPath = $mount->getInternalPath($absoluteDir);
  57. if (is_resource($dirContent)) {
  58. $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
  59. while (($entryName = readdir($dirContent)) !== false) {
  60. if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
  61. $id = $entryName;
  62. if ($dir === '' || $dir === '/') {
  63. $size = $view->filesize($id);
  64. $pathparts = pathinfo($entryName);
  65. $timestamp = substr($pathparts['extension'], 1);
  66. $id = $pathparts['filename'];
  67. } else if ($timestamp === null) {
  68. // for subfolders we need to calculate the timestamp only once
  69. $size = $view->filesize($dir . '/' . $id);
  70. $parts = explode('/', ltrim($dir, '/'));
  71. $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
  72. }
  73. $originalPath = '';
  74. if (isset($originalLocations[$id][$timestamp])) {
  75. $originalPath = $originalLocations[$id][$timestamp];
  76. if (substr($originalPath, -1) === '/') {
  77. $originalPath = substr($originalPath, 0, -1);
  78. }
  79. }
  80. $i = array(
  81. 'name' => $id,
  82. 'mtime' => $timestamp,
  83. 'mimetype' => $view->is_dir($dir . '/' . $entryName) ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($id),
  84. 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
  85. 'directory' => ($dir === '/') ? '' : $dir,
  86. 'size' => $size,
  87. 'etag' => '',
  88. 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE
  89. );
  90. if ($originalPath) {
  91. $i['extraData'] = $originalPath.'/'.$id;
  92. }
  93. $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
  94. }
  95. }
  96. closedir($dirContent);
  97. }
  98. if ($sortAttribute !== '') {
  99. return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
  100. }
  101. return $result;
  102. }
  103. /**
  104. * Format file infos for JSON
  105. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  106. */
  107. public static function formatFileInfos($fileInfos) {
  108. $files = array();
  109. $id = 0;
  110. foreach ($fileInfos as $i) {
  111. $entry = \OCA\Files\Helper::formatFileInfo($i);
  112. $entry['id'] = $id++;
  113. $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
  114. $entry['permissions'] = \OCP\Constants::PERMISSION_READ;
  115. $files[] = $entry;
  116. }
  117. return $files;
  118. }
  119. }