1
0

DirectFile.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public function getSize() {
  64. $this->getFile();
  65. return $this->file->getSize();
  66. }
  67. public function delete() {
  68. throw new Forbidden();
  69. }
  70. public function getName() {
  71. return $this->direct->getToken();
  72. }
  73. public function setName($name) {
  74. throw new Forbidden();
  75. }
  76. public function getLastModified() {
  77. $this->getFile();
  78. return $this->file->getMTime();
  79. }
  80. private function getFile() {
  81. if ($this->file === null) {
  82. $userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
  83. $files = $userFolder->getById($this->direct->getFileId());
  84. if ($files === []) {
  85. throw new NotFound();
  86. }
  87. $this->file = array_shift($files);
  88. }
  89. return $this->file;
  90. }
  91. }