File.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrew Brown <andrew@casabrown.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Search\Result;
  28. use OCP\Files\FileInfo;
  29. use OCP\Files\Folder;
  30. use OCP\IPreview;
  31. use OCP\IUserSession;
  32. /**
  33. * A found file
  34. * @deprecated 20.0.0
  35. */
  36. class File extends \OCP\Search\Result {
  37. /**
  38. * Type name; translated in templates
  39. * @var string
  40. * @deprecated 20.0.0
  41. */
  42. public $type = 'file';
  43. /**
  44. * Path to file
  45. * @var string
  46. * @deprecated 20.0.0
  47. */
  48. public $path;
  49. /**
  50. * Size, in bytes
  51. * @var int
  52. * @deprecated 20.0.0
  53. */
  54. public $size;
  55. /**
  56. * Date modified, in human readable form
  57. * @var string
  58. * @deprecated 20.0.0
  59. */
  60. public $modified;
  61. /**
  62. * File mime type
  63. * @var string
  64. * @deprecated 20.0.0
  65. */
  66. public $mime_type;
  67. /**
  68. * File permissions:
  69. *
  70. * @var string
  71. * @deprecated 20.0.0
  72. */
  73. public $permissions;
  74. /**
  75. * Has a preview
  76. *
  77. * @var string
  78. * @deprecated 20.0.0
  79. */
  80. public $has_preview;
  81. /**
  82. * Create a new file search result
  83. * @param FileInfo $data file data given by provider
  84. * @deprecated 20.0.0
  85. */
  86. public function __construct(FileInfo $data) {
  87. $path = $this->getRelativePath($data->getPath());
  88. $this->id = $data->getId();
  89. $this->name = $data->getName();
  90. $this->link = \OC::$server->getURLGenerator()->linkToRoute(
  91. 'files.view.index',
  92. [
  93. 'dir' => dirname($path),
  94. 'scrollto' => $data->getName(),
  95. ]
  96. );
  97. $this->permissions = $data->getPermissions();
  98. $this->path = $path;
  99. $this->size = $data->getSize();
  100. $this->modified = $data->getMtime();
  101. $this->mime_type = $data->getMimetype();
  102. $this->has_preview = $this->hasPreview($data);
  103. }
  104. /**
  105. * @var Folder $userFolderCache
  106. * @deprecated 20.0.0
  107. */
  108. protected static $userFolderCache = null;
  109. /**
  110. * converts a path relative to the users files folder
  111. * eg /user/files/foo.txt -> /foo.txt
  112. * @param string $path
  113. * @return string relative path
  114. * @deprecated 20.0.0
  115. */
  116. protected function getRelativePath($path) {
  117. if (!isset(self::$userFolderCache)) {
  118. $userSession = \OC::$server->get(IUserSession::class);
  119. $userID = $userSession->getUser()->getUID();
  120. self::$userFolderCache = \OC::$server->getUserFolder($userID);
  121. }
  122. $relativePath = self::$userFolderCache->getRelativePath($path);
  123. if ($relativePath === null) {
  124. throw new \Exception("Search result not in user folder");
  125. }
  126. return $relativePath;
  127. }
  128. /**
  129. * Is the preview available
  130. * @param FileInfo $data
  131. * @return bool
  132. * @deprecated 20.0.0
  133. */
  134. protected function hasPreview($data) {
  135. $previewManager = \OC::$server->get(IPreview::class);
  136. return $previewManager->isAvailable($data);
  137. }
  138. }