File.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 NonExistingFile non-existing node
  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 GenericFileException
  47. * @throws LockedException
  48. */
  49. public function getContent() {
  50. if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
  51. $content = $this->view->file_get_contents($this->path);
  52. if ($content === false) {
  53. throw new GenericFileException();
  54. }
  55. return $content;
  56. } else {
  57. throw new NotPermittedException();
  58. }
  59. }
  60. /**
  61. * @param string|resource $data
  62. * @throws NotPermittedException
  63. * @throws GenericFileException
  64. * @throws LockedException
  65. */
  66. public function putContent($data) {
  67. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  68. $this->sendHooks(['preWrite']);
  69. if ($this->view->file_put_contents($this->path, $data) === false) {
  70. throw new GenericFileException('file_put_contents failed');
  71. }
  72. $this->fileInfo = null;
  73. $this->sendHooks(['postWrite']);
  74. } else {
  75. throw new NotPermittedException();
  76. }
  77. }
  78. /**
  79. * @param string $mode
  80. * @return resource|false
  81. * @throws NotPermittedException
  82. * @throws LockedException
  83. */
  84. public function fopen($mode) {
  85. $preHooks = [];
  86. $postHooks = [];
  87. $requiredPermissions = \OCP\Constants::PERMISSION_READ;
  88. switch ($mode) {
  89. case 'r+':
  90. case 'rb+':
  91. case 'w+':
  92. case 'wb+':
  93. case 'x+':
  94. case 'xb+':
  95. case 'a+':
  96. case 'ab+':
  97. case 'w':
  98. case 'wb':
  99. case 'x':
  100. case 'xb':
  101. case 'a':
  102. case 'ab':
  103. $preHooks[] = 'preWrite';
  104. $postHooks[] = 'postWrite';
  105. $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
  106. break;
  107. }
  108. if ($this->checkPermissions($requiredPermissions)) {
  109. $this->sendHooks($preHooks);
  110. $result = $this->view->fopen($this->path, $mode);
  111. $this->sendHooks($postHooks);
  112. return $result;
  113. } else {
  114. throw new NotPermittedException();
  115. }
  116. }
  117. /**
  118. * @throws NotPermittedException
  119. * @throws \OCP\Files\InvalidPathException
  120. * @throws \OCP\Files\NotFoundException
  121. */
  122. public function delete() {
  123. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  124. $this->sendHooks(['preDelete']);
  125. $fileInfo = $this->getFileInfo();
  126. $this->view->unlink($this->path);
  127. $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
  128. $this->sendHooks(['postDelete'], [$nonExisting]);
  129. $this->fileInfo = null;
  130. } else {
  131. throw new NotPermittedException();
  132. }
  133. }
  134. /**
  135. * @param string $type
  136. * @param bool $raw
  137. * @return string
  138. */
  139. public function hash($type, $raw = false) {
  140. return $this->view->hash($type, $this->path, $raw);
  141. }
  142. /**
  143. * @inheritdoc
  144. */
  145. public function getChecksum() {
  146. return $this->getFileInfo()->getChecksum();
  147. }
  148. public function getExtension(): string {
  149. return $this->getFileInfo()->getExtension();
  150. }
  151. }