Helper.php 4.6 KB

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