1
0

VersionFile.php 3.2 KB

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