DirectFile.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Direct;
  25. use OCA\DAV\Db\Direct;
  26. use OCP\Files\File;
  27. use OCP\Files\IRootFolder;
  28. use Sabre\DAV\Exception\Forbidden;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\IFile;
  31. class DirectFile implements IFile {
  32. /** @var Direct */
  33. private $direct;
  34. /** @var IRootFolder */
  35. private $rootFolder;
  36. /** @var File */
  37. private $file;
  38. public function __construct(Direct $direct, IRootFolder $rootFolder) {
  39. $this->direct = $direct;
  40. $this->rootFolder = $rootFolder;
  41. }
  42. public function put($data) {
  43. throw new Forbidden();
  44. }
  45. public function get() {
  46. $this->getFile();
  47. return $this->file->fopen('rb');
  48. }
  49. public function getContentType() {
  50. $this->getFile();
  51. return $this->file->getMimeType();
  52. }
  53. public function getETag() {
  54. $this->getFile();
  55. return $this->file->getEtag();
  56. }
  57. public function getSize() {
  58. $this->getFile();
  59. return $this->file->getSize();
  60. }
  61. public function delete() {
  62. throw new Forbidden();
  63. }
  64. public function getName() {
  65. return $this->direct->getToken();
  66. }
  67. public function setName($name) {
  68. throw new Forbidden();
  69. }
  70. public function getLastModified() {
  71. $this->getFile();
  72. return $this->file->getMTime();
  73. }
  74. private function getFile() {
  75. if ($this->file === null) {
  76. $userFolder = $this->rootFolder->getUserFolder($this->direct->getUserId());
  77. $files = $userFolder->getById($this->direct->getFileId());
  78. if ($files === []) {
  79. throw new NotFound();
  80. }
  81. $this->file = array_shift($files);
  82. }
  83. return $this->file;
  84. }
  85. }