VersionFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. public function getSize(): int {
  61. return $this->version->getSize();
  62. }
  63. public function delete() {
  64. if ($this->versionManager instanceof IDeletableVersionBackend) {
  65. $this->versionManager->deleteVersion($this->version);
  66. } else {
  67. throw new Forbidden();
  68. }
  69. }
  70. public function getName(): string {
  71. return (string)$this->version->getRevisionId();
  72. }
  73. public function setName($name) {
  74. throw new Forbidden();
  75. }
  76. public function getLabel(): ?string {
  77. if ($this->version instanceof INameableVersion && $this->version->getSourceFile()->getSize() > 0) {
  78. return $this->version->getLabel();
  79. } else {
  80. return null;
  81. }
  82. }
  83. public function setLabel($label): bool {
  84. if ($this->versionManager instanceof INameableVersionBackend) {
  85. $this->versionManager->setVersionLabel($this->version, $label);
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function getLastModified(): int {
  92. return $this->version->getTimestamp();
  93. }
  94. public function rollBack() {
  95. $this->versionManager->rollback($this->version);
  96. }
  97. public function getVersion(): IVersion {
  98. return $this->version;
  99. }
  100. }