file.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Node;
  27. use OCP\Files\NotPermittedException;
  28. class File extends Node implements \OCP\Files\File {
  29. /**
  30. * @return string
  31. * @throws \OCP\Files\NotPermittedException
  32. */
  33. public function getContent() {
  34. if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
  35. /**
  36. * @var \OC\Files\Storage\Storage $storage;
  37. */
  38. return $this->view->file_get_contents($this->path);
  39. } else {
  40. throw new NotPermittedException();
  41. }
  42. }
  43. /**
  44. * @param string $data
  45. * @throws \OCP\Files\NotPermittedException
  46. */
  47. public function putContent($data) {
  48. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  49. $this->sendHooks(array('preWrite'));
  50. $this->view->file_put_contents($this->path, $data);
  51. $this->fileInfo = null;
  52. $this->sendHooks(array('postWrite'));
  53. } else {
  54. throw new NotPermittedException();
  55. }
  56. }
  57. /**
  58. * @param string $mode
  59. * @return resource
  60. * @throws \OCP\Files\NotPermittedException
  61. */
  62. public function fopen($mode) {
  63. $preHooks = array();
  64. $postHooks = array();
  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. public function delete() {
  96. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  97. $this->sendHooks(array('preDelete'));
  98. $fileInfo = $this->getFileInfo();
  99. $this->view->unlink($this->path);
  100. $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
  101. $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
  102. $this->exists = false;
  103. $this->fileInfo = null;
  104. } else {
  105. throw new NotPermittedException();
  106. }
  107. }
  108. /**
  109. * @param string $targetPath
  110. * @throws \OCP\Files\NotPermittedException
  111. * @return \OC\Files\Node\Node
  112. */
  113. public function copy($targetPath) {
  114. $targetPath = $this->normalizePath($targetPath);
  115. $parent = $this->root->get(dirname($targetPath));
  116. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  117. $nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
  118. $this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
  119. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  120. $this->view->copy($this->path, $targetPath);
  121. $targetNode = $this->root->get($targetPath);
  122. $this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
  123. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  124. return $targetNode;
  125. } else {
  126. throw new NotPermittedException();
  127. }
  128. }
  129. /**
  130. * @param string $targetPath
  131. * @throws \OCP\Files\NotPermittedException
  132. * @return \OC\Files\Node\Node
  133. */
  134. public function move($targetPath) {
  135. $targetPath = $this->normalizePath($targetPath);
  136. $parent = $this->root->get(dirname($targetPath));
  137. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  138. $nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
  139. $this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
  140. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  141. $this->view->rename($this->path, $targetPath);
  142. $targetNode = $this->root->get($targetPath);
  143. $this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
  144. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  145. $this->path = $targetPath;
  146. $this->fileInfo = null;
  147. return $targetNode;
  148. } else {
  149. throw new NotPermittedException();
  150. }
  151. }
  152. /**
  153. * @param string $type
  154. * @param bool $raw
  155. * @return string
  156. */
  157. public function hash($type, $raw = false) {
  158. return $this->view->hash($type, $this->path, $raw);
  159. }
  160. /**
  161. * @inheritdoc
  162. */
  163. public function getChecksum() {
  164. return $this->getFileInfo()->getChecksum();
  165. }
  166. }