You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.2 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Node;
  30. use OCP\Files\GenericFileException;
  31. use OCP\Files\NotPermittedException;
  32. use OCP\Lock\LockedException;
  33. class File extends Node implements \OCP\Files\File {
  34. /**
  35. * Creates a Folder that represents a non-existing path
  36. *
  37. * @param string $path path
  38. * @return string non-existing node class
  39. */
  40. protected function createNonExistingNode($path) {
  41. return new NonExistingFile($this->root, $this->view, $path);
  42. }
  43. /**
  44. * @return string
  45. * @throws NotPermittedException
  46. * @throws LockedException
  47. */
  48. public function getContent() {
  49. if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
  50. /**
  51. * @var \OC\Files\Storage\Storage $storage;
  52. */
  53. return $this->view->file_get_contents($this->path);
  54. } else {
  55. throw new NotPermittedException();
  56. }
  57. }
  58. /**
  59. * @param string|resource $data
  60. * @throws NotPermittedException
  61. * @throws \OCP\Files\GenericFileException
  62. * @throws LockedException
  63. */
  64. public function putContent($data) {
  65. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  66. $this->sendHooks(['preWrite']);
  67. if ($this->view->file_put_contents($this->path, $data) === false) {
  68. throw new GenericFileException('file_put_contents failed');
  69. }
  70. $this->fileInfo = null;
  71. $this->sendHooks(['postWrite']);
  72. } else {
  73. throw new NotPermittedException();
  74. }
  75. }
  76. /**
  77. * @param string $mode
  78. * @return resource
  79. * @throws NotPermittedException
  80. * @throws LockedException
  81. */
  82. public function fopen($mode) {
  83. $preHooks = [];
  84. $postHooks = [];
  85. $requiredPermissions = \OCP\Constants::PERMISSION_READ;
  86. switch ($mode) {
  87. case 'r+':
  88. case 'rb+':
  89. case 'w+':
  90. case 'wb+':
  91. case 'x+':
  92. case 'xb+':
  93. case 'a+':
  94. case 'ab+':
  95. case 'w':
  96. case 'wb':
  97. case 'x':
  98. case 'xb':
  99. case 'a':
  100. case 'ab':
  101. $preHooks[] = 'preWrite';
  102. $postHooks[] = 'postWrite';
  103. $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
  104. break;
  105. }
  106. if ($this->checkPermissions($requiredPermissions)) {
  107. $this->sendHooks($preHooks);
  108. $result = $this->view->fopen($this->path, $mode);
  109. $this->sendHooks($postHooks);
  110. return $result;
  111. } else {
  112. throw new NotPermittedException();
  113. }
  114. }
  115. /**
  116. * @throws NotPermittedException
  117. * @throws \OCP\Files\InvalidPathException
  118. * @throws \OCP\Files\NotFoundException
  119. */
  120. public function delete() {
  121. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  122. $this->sendHooks(['preDelete']);
  123. $fileInfo = $this->getFileInfo();
  124. $this->view->unlink($this->path);
  125. $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
  126. $this->sendHooks(['postDelete'], [$nonExisting]);
  127. $this->exists = false;
  128. $this->fileInfo = null;
  129. } else {
  130. throw new NotPermittedException();
  131. }
  132. }
  133. /**
  134. * @param string $type
  135. * @param bool $raw
  136. * @return string
  137. */
  138. public function hash($type, $raw = false) {
  139. return $this->view->hash($type, $this->path, $raw);
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function getChecksum() {
  145. return $this->getFileInfo()->getChecksum();
  146. }
  147. public function getExtension(): string {
  148. return $this->getFileInfo()->getExtension();
  149. }
  150. }