VersionFile.php 3.5 KB

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