Helper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin;
  8. use OC\Files\FileInfo;
  9. use OC\Files\View;
  10. use OCP\Constants;
  11. use OCP\Files\Cache\ICacheEntry;
  12. class Helper {
  13. /**
  14. * Retrieves the contents of a trash bin directory.
  15. *
  16. * @param string $dir path to the directory inside the trashbin
  17. * or empty to retrieve the root of the trashbin
  18. * @param string $user
  19. * @param string $sortAttribute attribute to sort on or empty to disable sorting
  20. * @param bool $sortDescending true for descending sort, false otherwise
  21. * @return \OCP\Files\FileInfo[]
  22. */
  23. public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) {
  24. $result = [];
  25. $timestamp = null;
  26. $view = new View('/' . $user . '/files_trashbin/files');
  27. if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
  28. throw new \Exception('Directory does not exists');
  29. }
  30. $mount = $view->getMount($dir);
  31. $storage = $mount->getStorage();
  32. $absoluteDir = $view->getAbsolutePath($dir);
  33. $internalPath = $mount->getInternalPath($absoluteDir);
  34. $extraData = Trashbin::getExtraData($user);
  35. $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
  36. foreach ($dirContent as $entry) {
  37. $entryName = $entry->getName();
  38. $name = $entryName;
  39. if ($dir === '' || $dir === '/') {
  40. $pathparts = pathinfo($entryName);
  41. $timestamp = substr($pathparts['extension'], 1);
  42. $name = $pathparts['filename'];
  43. } elseif ($timestamp === null) {
  44. // for subfolders we need to calculate the timestamp only once
  45. $parts = explode('/', ltrim($dir, '/'));
  46. $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
  47. }
  48. $originalPath = '';
  49. $originalName = substr($entryName, 0, -strlen($timestamp) - 2);
  50. if (isset($extraData[$originalName][$timestamp]['location'])) {
  51. $originalPath = $extraData[$originalName][$timestamp]['location'];
  52. if (substr($originalPath, -1) === '/') {
  53. $originalPath = substr($originalPath, 0, -1);
  54. }
  55. }
  56. $type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file';
  57. $i = [
  58. 'name' => $name,
  59. 'mtime' => $timestamp,
  60. 'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name),
  61. 'type' => $type,
  62. 'directory' => ($dir === '/') ? '' : $dir,
  63. 'size' => $entry->getSize(),
  64. 'etag' => '',
  65. 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE,
  66. 'fileid' => $entry->getId(),
  67. ];
  68. if ($originalPath) {
  69. if ($originalPath !== '.') {
  70. $i['extraData'] = $originalPath . '/' . $originalName;
  71. } else {
  72. $i['extraData'] = $originalName;
  73. }
  74. }
  75. $i['deletedBy'] = $extraData[$originalName][$timestamp]['deletedBy'] ?? null;
  76. $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount);
  77. }
  78. if ($sortAttribute !== '') {
  79. return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
  80. }
  81. return $result;
  82. }
  83. /**
  84. * Format file infos for JSON
  85. *
  86. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  87. */
  88. public static function formatFileInfos($fileInfos) {
  89. $files = [];
  90. foreach ($fileInfos as $i) {
  91. $entry = \OCA\Files\Helper::formatFileInfo($i);
  92. $entry['id'] = $i->getId();
  93. $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
  94. $entry['permissions'] = Constants::PERMISSION_READ;
  95. $files[] = $entry;
  96. }
  97. return $files;
  98. }
  99. }