VersionFile.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Versions\Sabre;
  8. use OCA\Files_Versions\Versions\IDeletableVersionBackend;
  9. use OCA\Files_Versions\Versions\IMetadataVersion;
  10. use OCA\Files_Versions\Versions\IMetadataVersionBackend;
  11. use OCA\Files_Versions\Versions\INameableVersion;
  12. use OCA\Files_Versions\Versions\INameableVersionBackend;
  13. use OCA\Files_Versions\Versions\IVersion;
  14. use OCA\Files_Versions\Versions\IVersionManager;
  15. use OCP\Files\NotFoundException;
  16. use Sabre\DAV\Exception\Forbidden;
  17. use Sabre\DAV\Exception\NotFound;
  18. use Sabre\DAV\IFile;
  19. class VersionFile implements IFile {
  20. public function __construct(
  21. private IVersion $version,
  22. private IVersionManager $versionManager
  23. ) {
  24. }
  25. public function put($data) {
  26. throw new Forbidden();
  27. }
  28. public function get() {
  29. try {
  30. return $this->versionManager->read($this->version);
  31. } catch (NotFoundException $e) {
  32. throw new NotFound();
  33. }
  34. }
  35. public function getContentType(): string {
  36. return $this->version->getMimeType();
  37. }
  38. public function getETag(): string {
  39. return (string)$this->version->getRevisionId();
  40. }
  41. /**
  42. * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
  43. * @return int|float
  44. */
  45. public function getSize(): int|float {
  46. return $this->version->getSize();
  47. }
  48. public function delete() {
  49. if ($this->versionManager instanceof IDeletableVersionBackend) {
  50. $this->versionManager->deleteVersion($this->version);
  51. } else {
  52. throw new Forbidden();
  53. }
  54. }
  55. public function getName(): string {
  56. return (string)$this->version->getRevisionId();
  57. }
  58. public function setName($name) {
  59. throw new Forbidden();
  60. }
  61. public function setMetadataValue(string $key, string $value): bool {
  62. $backend = $this->version->getBackend();
  63. if ($backend instanceof IMetadataVersionBackend) {
  64. $backend->setMetadataValue($this->version->getSourceFile(), $this->version->getRevisionId(), $key, $value);
  65. return true;
  66. } elseif ($key === 'label' && $backend instanceof INameableVersionBackend) {
  67. $backend->setVersionLabel($this->version, $value);
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. public function getMetadataValue(string $key): ?string {
  74. if ($this->version instanceof IMetadataVersion) {
  75. return $this->version->getMetadataValue($key);
  76. } elseif ($key === 'label' && $this->version instanceof INameableVersion) {
  77. return $this->version->getLabel();
  78. }
  79. return null;
  80. }
  81. public function getLastModified(): int {
  82. return $this->version->getTimestamp();
  83. }
  84. public function rollBack() {
  85. $this->versionManager->rollback($this->version);
  86. }
  87. public function getVersion(): IVersion {
  88. return $this->version;
  89. }
  90. }