File.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <vincent@nextcloud.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->fileInfo = null;
  128. } else {
  129. throw new NotPermittedException();
  130. }
  131. }
  132. /**
  133. * @param string $type
  134. * @param bool $raw
  135. * @return string
  136. */
  137. public function hash($type, $raw = false) {
  138. return $this->view->hash($type, $this->path, $raw);
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function getChecksum() {
  144. return $this->getFileInfo()->getChecksum();
  145. }
  146. public function getExtension(): string {
  147. return $this->getFileInfo()->getExtension();
  148. }
  149. }