1
0

helper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <rmccorkell@karoshi.org.uk>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, 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. class Helper
  30. {
  31. /**
  32. * Retrieves the contents of a trash bin directory.
  33. *
  34. * @param string $dir path to the directory inside the trashbin
  35. * or empty to retrieve the root of the trashbin
  36. * @param string $user
  37. * @param string $sortAttribute attribute to sort on or empty to disable sorting
  38. * @param bool $sortDescending true for descending sort, false otherwise
  39. * @return \OCP\Files\FileInfo[]
  40. */
  41. public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){
  42. $result = array();
  43. $timestamp = null;
  44. $view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
  45. if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
  46. throw new \Exception('Directory does not exists');
  47. }
  48. $dirContent = $view->opendir($dir);
  49. if ($dirContent === false) {
  50. return $result;
  51. }
  52. $mount = $view->getMount($dir);
  53. $storage = $mount->getStorage();
  54. $absoluteDir = $view->getAbsolutePath($dir);
  55. $internalPath = $mount->getInternalPath($absoluteDir);
  56. if (is_resource($dirContent)) {
  57. $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
  58. while (($entryName = readdir($dirContent)) !== false) {
  59. if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
  60. $id = $entryName;
  61. if ($dir === '' || $dir === '/') {
  62. $pathparts = pathinfo($entryName);
  63. $timestamp = substr($pathparts['extension'], 1);
  64. $id = $pathparts['filename'];
  65. } else if ($timestamp === null) {
  66. // for subfolders we need to calculate the timestamp only once
  67. $parts = explode('/', ltrim($dir, '/'));
  68. $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
  69. }
  70. $originalPath = '';
  71. if (isset($originalLocations[$id][$timestamp])) {
  72. $originalPath = $originalLocations[$id][$timestamp];
  73. if (substr($originalPath, -1) === '/') {
  74. $originalPath = substr($originalPath, 0, -1);
  75. }
  76. }
  77. $i = array(
  78. 'name' => $id,
  79. 'mtime' => $timestamp,
  80. 'mimetype' => \OC_Helper::getFileNameMimeType($id),
  81. 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
  82. 'directory' => ($dir === '/') ? '' : $dir,
  83. );
  84. if ($originalPath) {
  85. $i['extraData'] = $originalPath.'/'.$id;
  86. }
  87. $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
  88. }
  89. }
  90. closedir($dirContent);
  91. }
  92. if ($sortAttribute !== '') {
  93. return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
  94. }
  95. return $result;
  96. }
  97. /**
  98. * Format file infos for JSON
  99. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  100. */
  101. public static function formatFileInfos($fileInfos) {
  102. $files = array();
  103. $id = 0;
  104. foreach ($fileInfos as $i) {
  105. $entry = \OCA\Files\Helper::formatFileInfo($i);
  106. $entry['id'] = $id++;
  107. $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
  108. $entry['permissions'] = \OCP\Constants::PERMISSION_READ;
  109. if (\OCP\App::isEnabled('files_encryption')) {
  110. $entry['isPreviewAvailable'] = false;
  111. }
  112. $files[] = $entry;
  113. }
  114. return $files;
  115. }
  116. }