DirectFile.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Direct;
  26. use OCA\DAV\Db\Direct;
  27. use OCA\DAV\Events\BeforeFileDirectDownloadedEvent;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\Files\File;
  30. use OCP\Files\IRootFolder;
  31. use Sabre\DAV\Exception\Forbidden;
  32. use Sabre\DAV\Exception\NotFound;
  33. use Sabre\DAV\IFile;
  34. class DirectFile implements IFile {
  35. /** @var Direct */
  36. private $direct;
  37. /** @var IRootFolder */
  38. private $rootFolder;
  39. /** @var File */
  40. private $file;
  41. private $eventDispatcher;
  42. public function __construct(Direct $direct, IRootFolder $rootFolder, IEventDispatcher $eventDispatcher) {
  43. $this->direct = $direct;
  44. $this->rootFolder = $rootFolder;
  45. $this->eventDispatcher = $eventDispatcher;
  46. }
  47. public function put($data) {
  48. throw new Forbidden();
  49. }
  50. public function get() {
  51. $this->getFile();
  52. $this->eventDispatcher->dispatchTyped(new BeforeFileDirectDownloadedEvent($this->file));
  53. return $this->file->fopen('rb');
  54. }
  55. public function getContentType() {
  56. $this->getFile();
  57. return $this->file->getMimeType();
  58. }
  59. public function getETag() {
  60. $this->getFile();
  61. return $this->file->getEtag();
  62. }
  63. /**
  64. * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
  65. * @return int|float
  66. */
  67. public function getSize() {
  68. $this->getFile();
  69. return $this->file->getSize();
  70. }
  71. public function delete() {
  72. throw new Forbidden();
  73. }
  74. public function getName() {
  75. return $this->direct->getToken();
  76. }
  77. public function setName($name) {
  78. throw new Forbidden();
  79. }
  80. public function getLastModified() {
  81. $this->getFile();
  82. return $this->file->getMTime();
  83. }
  84. private function getFile() {
  85. if ($this->file === null) {
  86. $userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
  87. $file = $userFolder->getFirstNodeById($this->direct->getFileId());
  88. if (!$file) {
  89. throw new NotFound();
  90. }
  91. if (!$file instanceof File) {
  92. throw new Forbidden("direct download not allowed on directories");
  93. }
  94. $this->file = $file;
  95. }
  96. return $this->file;
  97. }
  98. }