Helper.php 3.6 KB

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