VersionFile.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Files_Versions\Sabre;
  26. use OCA\Files_Versions\Versions\IDeletableVersionBackend;
  27. use OCA\Files_Versions\Versions\INameableVersion;
  28. use OCA\Files_Versions\Versions\INameableVersionBackend;
  29. use OCA\Files_Versions\Versions\IVersion;
  30. use OCA\Files_Versions\Versions\IVersionManager;
  31. use OCP\Files\FileInfo;
  32. use OCP\Files\NotFoundException;
  33. use Sabre\DAV\Exception\Forbidden;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\IFile;
  36. class VersionFile implements IFile {
  37. /** @var IVersion */
  38. private $version;
  39. /** @var IVersionManager */
  40. private $versionManager;
  41. public function __construct(IVersion $version, IVersionManager $versionManager) {
  42. $this->version = $version;
  43. $this->versionManager = $versionManager;
  44. }
  45. public function put($data) {
  46. throw new Forbidden();
  47. }
  48. public function get() {
  49. try {
  50. return $this->versionManager->read($this->version);
  51. } catch (NotFoundException $e) {
  52. throw new NotFound();
  53. }
  54. }
  55. public function getContentType(): string {
  56. return $this->version->getMimeType();
  57. }
  58. public function getETag(): string {
  59. return (string)$this->version->getRevisionId();
  60. }
  61. /**
  62. * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
  63. * @return int|float
  64. */
  65. public function getSize(): int|float {
  66. return $this->version->getSize();
  67. }
  68. public function delete() {
  69. if ($this->versionManager instanceof IDeletableVersionBackend) {
  70. $this->versionManager->deleteVersion($this->version);
  71. } else {
  72. throw new Forbidden();
  73. }
  74. }
  75. public function getName(): string {
  76. return (string)$this->version->getRevisionId();
  77. }
  78. public function setName($name) {
  79. throw new Forbidden();
  80. }
  81. public function getLabel(): ?string {
  82. if ($this->version instanceof INameableVersion && $this->version->getSourceFile()->getSize() > 0) {
  83. return $this->version->getLabel();
  84. } else {
  85. return null;
  86. }
  87. }
  88. public function setLabel($label): bool {
  89. if ($this->versionManager instanceof INameableVersionBackend) {
  90. $this->versionManager->setVersionLabel($this->version, $label);
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. public function getLastModified(): int {
  97. return $this->version->getTimestamp();
  98. }
  99. public function rollBack() {
  100. $this->versionManager->rollback($this->version);
  101. }
  102. public function getVersion(): IVersion {
  103. return $this->version;
  104. }
  105. }