File.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 OC\Search\Result;
  8. use OCP\Files\FileInfo;
  9. use OCP\Files\Folder;
  10. use OCP\IPreview;
  11. use OCP\IUserSession;
  12. /**
  13. * A found file
  14. * @deprecated 20.0.0
  15. */
  16. class File extends \OCP\Search\Result {
  17. /**
  18. * Type name; translated in templates
  19. * @var string
  20. * @deprecated 20.0.0
  21. */
  22. public $type = 'file';
  23. /**
  24. * Path to file
  25. * @var string
  26. * @deprecated 20.0.0
  27. */
  28. public $path;
  29. /**
  30. * Size, in bytes
  31. * @var int
  32. * @deprecated 20.0.0
  33. */
  34. public $size;
  35. /**
  36. * Date modified, in human readable form
  37. * @var string
  38. * @deprecated 20.0.0
  39. */
  40. public $modified;
  41. /**
  42. * File mime type
  43. * @var string
  44. * @deprecated 20.0.0
  45. */
  46. public $mime_type;
  47. /**
  48. * File permissions:
  49. *
  50. * @var string
  51. * @deprecated 20.0.0
  52. */
  53. public $permissions;
  54. /**
  55. * Has a preview
  56. *
  57. * @var string
  58. * @deprecated 20.0.0
  59. */
  60. public $has_preview;
  61. /**
  62. * Create a new file search result
  63. * @param FileInfo $data file data given by provider
  64. * @deprecated 20.0.0
  65. */
  66. public function __construct(FileInfo $data) {
  67. $path = $this->getRelativePath($data->getPath());
  68. $this->id = $data->getId();
  69. $this->name = $data->getName();
  70. $this->link = \OC::$server->getURLGenerator()->linkToRoute(
  71. 'files.view.index',
  72. [
  73. 'dir' => dirname($path),
  74. 'scrollto' => $data->getName(),
  75. ]
  76. );
  77. $this->permissions = $data->getPermissions();
  78. $this->path = $path;
  79. $this->size = $data->getSize();
  80. $this->modified = $data->getMtime();
  81. $this->mime_type = $data->getMimetype();
  82. $this->has_preview = $this->hasPreview($data);
  83. }
  84. /**
  85. * @var Folder $userFolderCache
  86. * @deprecated 20.0.0
  87. */
  88. protected static $userFolderCache = null;
  89. /**
  90. * converts a path relative to the users files folder
  91. * eg /user/files/foo.txt -> /foo.txt
  92. * @param string $path
  93. * @return string relative path
  94. * @deprecated 20.0.0
  95. */
  96. protected function getRelativePath($path) {
  97. if (!isset(self::$userFolderCache)) {
  98. $userSession = \OC::$server->get(IUserSession::class);
  99. $userID = $userSession->getUser()->getUID();
  100. self::$userFolderCache = \OC::$server->getUserFolder($userID);
  101. }
  102. $relativePath = self::$userFolderCache->getRelativePath($path);
  103. if ($relativePath === null) {
  104. throw new \Exception("Search result not in user folder");
  105. }
  106. return $relativePath;
  107. }
  108. /**
  109. * Is the preview available
  110. * @param FileInfo $data
  111. * @return bool
  112. * @deprecated 20.0.0
  113. */
  114. protected function hasPreview($data) {
  115. $previewManager = \OC::$server->get(IPreview::class);
  116. return $previewManager->isAvailable($data);
  117. }
  118. }