Helper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Trashbin;
  27. use OC\Files\FileInfo;
  28. use OCP\Constants;
  29. use OCP\Files\Cache\ICacheEntry;
  30. class Helper {
  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. $mount = $view->getMount($dir);
  49. $storage = $mount->getStorage();
  50. $absoluteDir = $view->getAbsolutePath($dir);
  51. $internalPath = $mount->getInternalPath($absoluteDir);
  52. $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
  53. $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
  54. foreach ($dirContent as $entry) {
  55. $entryName = $entry->getName();
  56. $id = $entry->getId();
  57. $name = $entryName;
  58. if ($dir === '' || $dir === '/') {
  59. $pathparts = pathinfo($entryName);
  60. $timestamp = substr($pathparts['extension'], 1);
  61. $name = $pathparts['filename'];
  62. } else if ($timestamp === null) {
  63. // for subfolders we need to calculate the timestamp only once
  64. $parts = explode('/', ltrim($dir, '/'));
  65. $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
  66. }
  67. $originalPath = '';
  68. if (isset($originalLocations[$id][$timestamp])) {
  69. $originalPath = $originalLocations[$id][$timestamp];
  70. if (substr($originalPath, -1) === '/') {
  71. $originalPath = substr($originalPath, 0, -1);
  72. }
  73. }
  74. $i = array(
  75. 'name' => $name,
  76. 'mtime' => $timestamp,
  77. 'mimetype' => $entry->getMimeType(),
  78. 'type' => $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file',
  79. 'directory' => ($dir === '/') ? '' : $dir,
  80. 'size' => $entry->getSize(),
  81. 'etag' => '',
  82. 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE
  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. if ($sortAttribute !== '') {
  90. return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Format file infos for JSON
  96. *
  97. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  98. */
  99. public static function formatFileInfos($fileInfos) {
  100. $files = array();
  101. $id = 0;
  102. foreach ($fileInfos as $i) {
  103. $entry = \OCA\Files\Helper::formatFileInfo($i);
  104. $entry['id'] = $id++;
  105. $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
  106. $entry['permissions'] = \OCP\Constants::PERMISSION_READ;
  107. $files[] = $entry;
  108. }
  109. return $files;
  110. }
  111. }