File.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrew Brown <andrew@casabrown.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Search\Result;
  26. use OCP\Files\FileInfo;
  27. use OCP\Files\Folder;
  28. /**
  29. * A found file
  30. */
  31. class File extends \OCP\Search\Result {
  32. /**
  33. * Type name; translated in templates
  34. * @var string
  35. */
  36. public $type = 'file';
  37. /**
  38. * Path to file
  39. * @var string
  40. */
  41. public $path;
  42. /**
  43. * Size, in bytes
  44. * @var int
  45. */
  46. public $size;
  47. /**
  48. * Date modified, in human readable form
  49. * @var string
  50. */
  51. public $modified;
  52. /**
  53. * File mime type
  54. * @var string
  55. */
  56. public $mime_type;
  57. /**
  58. * File permissions:
  59. *
  60. * @var string
  61. */
  62. public $permissions;
  63. /**
  64. * Create a new file search result
  65. * @param FileInfo $data file data given by provider
  66. */
  67. public function __construct(FileInfo $data) {
  68. $path = $this->getRelativePath($data->getPath());
  69. $info = pathinfo($path);
  70. $this->id = $data->getId();
  71. $this->name = $info['basename'];
  72. $this->link = \OC::$server->getURLGenerator()->linkToRoute(
  73. 'files.view.index',
  74. [
  75. 'dir' => $info['dirname'],
  76. 'scrollto' => $info['basename'],
  77. ]
  78. );
  79. $this->permissions = $data->getPermissions();
  80. $this->path = $path;
  81. $this->size = $data->getSize();
  82. $this->modified = $data->getMtime();
  83. $this->mime = $data->getMimetype();
  84. }
  85. /**
  86. * @var Folder $userFolderCache
  87. */
  88. static protected $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. */
  95. protected function getRelativePath ($path) {
  96. if (!isset(self::$userFolderCache)) {
  97. $user = \OC::$server->getUserSession()->getUser()->getUID();
  98. self::$userFolderCache = \OC::$server->getUserFolder($user);
  99. }
  100. return self::$userFolderCache->getRelativePath($path);
  101. }
  102. }