helper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace OCA\Files_Trashbin;
  3. use OC\Files\FileInfo;
  4. class Helper
  5. {
  6. /**
  7. * Retrieves the contents of a trash bin directory.
  8. *
  9. * @param string $dir path to the directory inside the trashbin
  10. * or empty to retrieve the root of the trashbin
  11. * @param string $user
  12. * @param string $sortAttribute attribute to sort on or empty to disable sorting
  13. * @param bool $sortDescending true for descending sort, false otherwise
  14. * @return \OCP\Files\FileInfo[]
  15. */
  16. public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false){
  17. $result = array();
  18. $timestamp = null;
  19. $view = new \OC\Files\View('/' . $user . '/files_trashbin/files');
  20. if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) {
  21. throw new \Exception('Directory does not exists');
  22. }
  23. $dirContent = $view->opendir($dir);
  24. if ($dirContent === false) {
  25. return $result;
  26. }
  27. list($storage, $internalPath) = $view->resolvePath($dir);
  28. $absoluteDir = $view->getAbsolutePath($dir);
  29. if (is_resource($dirContent)) {
  30. $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user);
  31. while (($entryName = readdir($dirContent)) !== false) {
  32. if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) {
  33. $id = $entryName;
  34. if ($dir === '' || $dir === '/') {
  35. $pathparts = pathinfo($entryName);
  36. $timestamp = substr($pathparts['extension'], 1);
  37. $id = $pathparts['filename'];
  38. } else if ($timestamp === null) {
  39. // for subfolders we need to calculate the timestamp only once
  40. $parts = explode('/', ltrim($dir, '/'));
  41. $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1);
  42. }
  43. $originalPath = '';
  44. if (isset($originalLocations[$id][$timestamp])) {
  45. $originalPath = $originalLocations[$id][$timestamp];
  46. if (substr($originalPath, -1) === '/') {
  47. $originalPath = substr($originalPath, 0, -1);
  48. }
  49. }
  50. $i = array(
  51. 'name' => $id,
  52. 'mtime' => $timestamp,
  53. 'mimetype' => \OC_Helper::getFileNameMimeType($id),
  54. 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file',
  55. 'directory' => ($dir === '/') ? '' : $dir,
  56. );
  57. if ($originalPath) {
  58. $i['extraData'] = $originalPath.'/'.$id;
  59. }
  60. $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i);
  61. }
  62. }
  63. closedir($dirContent);
  64. }
  65. if ($sortAttribute !== '') {
  66. return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending);
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Format file infos for JSON
  72. * @param \OCP\Files\FileInfo[] $fileInfos file infos
  73. */
  74. public static function formatFileInfos($fileInfos) {
  75. $files = array();
  76. $id = 0;
  77. foreach ($fileInfos as $i) {
  78. $entry = \OCA\Files\Helper::formatFileInfo($i);
  79. $entry['id'] = $id++;
  80. $entry['etag'] = $entry['mtime']; // add fake etag, it is only needed to identify the preview image
  81. $entry['permissions'] = \OCP\PERMISSION_READ;
  82. if (\OCP\App::isEnabled('files_encryption')) {
  83. $entry['isPreviewAvailable'] = false;
  84. }
  85. $files[] = $entry;
  86. }
  87. return $files;
  88. }
  89. }