Helper.php 4.4 KB

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