1
0

File.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Node;
  8. use OCP\Files\GenericFileException;
  9. use OCP\Files\NotPermittedException;
  10. use OCP\Lock\LockedException;
  11. class File extends Node implements \OCP\Files\File {
  12. /**
  13. * Creates a Folder that represents a non-existing path
  14. *
  15. * @param string $path path
  16. * @return NonExistingFile non-existing node
  17. */
  18. protected function createNonExistingNode($path) {
  19. return new NonExistingFile($this->root, $this->view, $path);
  20. }
  21. /**
  22. * @return string
  23. * @throws NotPermittedException
  24. * @throws GenericFileException
  25. * @throws LockedException
  26. */
  27. public function getContent() {
  28. if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
  29. $content = $this->view->file_get_contents($this->path);
  30. if ($content === false) {
  31. throw new GenericFileException();
  32. }
  33. return $content;
  34. } else {
  35. throw new NotPermittedException();
  36. }
  37. }
  38. /**
  39. * @param string|resource $data
  40. * @throws NotPermittedException
  41. * @throws GenericFileException
  42. * @throws LockedException
  43. */
  44. public function putContent($data) {
  45. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  46. $this->sendHooks(['preWrite']);
  47. if ($this->view->file_put_contents($this->path, $data) === false) {
  48. throw new GenericFileException('file_put_contents failed');
  49. }
  50. $this->fileInfo = null;
  51. $this->sendHooks(['postWrite']);
  52. } else {
  53. throw new NotPermittedException();
  54. }
  55. }
  56. /**
  57. * @param string $mode
  58. * @return resource|false
  59. * @throws NotPermittedException
  60. * @throws LockedException
  61. */
  62. public function fopen($mode) {
  63. $preHooks = [];
  64. $postHooks = [];
  65. $requiredPermissions = \OCP\Constants::PERMISSION_READ;
  66. switch ($mode) {
  67. case 'r+':
  68. case 'rb+':
  69. case 'w+':
  70. case 'wb+':
  71. case 'x+':
  72. case 'xb+':
  73. case 'a+':
  74. case 'ab+':
  75. case 'w':
  76. case 'wb':
  77. case 'x':
  78. case 'xb':
  79. case 'a':
  80. case 'ab':
  81. $preHooks[] = 'preWrite';
  82. $postHooks[] = 'postWrite';
  83. $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
  84. break;
  85. }
  86. if ($this->checkPermissions($requiredPermissions)) {
  87. $this->sendHooks($preHooks);
  88. $result = $this->view->fopen($this->path, $mode);
  89. $this->sendHooks($postHooks);
  90. return $result;
  91. } else {
  92. throw new NotPermittedException();
  93. }
  94. }
  95. /**
  96. * @throws NotPermittedException
  97. * @throws \OCP\Files\InvalidPathException
  98. * @throws \OCP\Files\NotFoundException
  99. */
  100. public function delete() {
  101. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  102. $this->sendHooks(['preDelete']);
  103. $fileInfo = $this->getFileInfo();
  104. $this->view->unlink($this->path);
  105. $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
  106. $this->sendHooks(['postDelete'], [$nonExisting]);
  107. $this->fileInfo = null;
  108. } else {
  109. throw new NotPermittedException();
  110. }
  111. }
  112. /**
  113. * @param string $type
  114. * @param bool $raw
  115. * @return string
  116. */
  117. public function hash($type, $raw = false) {
  118. return $this->view->hash($type, $this->path, $raw);
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public function getChecksum() {
  124. return $this->getFileInfo()->getChecksum();
  125. }
  126. public function getExtension(): string {
  127. return $this->getFileInfo()->getExtension();
  128. }
  129. }