File.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\GenericFileException;
  28. use OCP\Files\NotPermittedException;
  29. class File extends Node implements \OCP\Files\File {
  30. /**
  31. * Creates a Folder that represents a non-existing path
  32. *
  33. * @param string $path path
  34. * @return string non-existing node class
  35. */
  36. protected function createNonExistingNode($path) {
  37. return new NonExistingFile($this->root, $this->view, $path);
  38. }
  39. /**
  40. * @return string
  41. * @throws \OCP\Files\NotPermittedException
  42. */
  43. public function getContent() {
  44. if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
  45. /**
  46. * @var \OC\Files\Storage\Storage $storage;
  47. */
  48. return $this->view->file_get_contents($this->path);
  49. } else {
  50. throw new NotPermittedException();
  51. }
  52. }
  53. /**
  54. * @param string|resource $data
  55. * @throws \OCP\Files\NotPermittedException
  56. * @throws \OCP\Files\GenericFileException
  57. */
  58. public function putContent($data) {
  59. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  60. $this->sendHooks(array('preWrite'));
  61. if ($this->view->file_put_contents($this->path, $data) === false) {
  62. throw new GenericFileException('file_put_contents failed');
  63. }
  64. $this->fileInfo = null;
  65. $this->sendHooks(array('postWrite'));
  66. } else {
  67. throw new NotPermittedException();
  68. }
  69. }
  70. /**
  71. * @param string $mode
  72. * @return resource
  73. * @throws \OCP\Files\NotPermittedException
  74. */
  75. public function fopen($mode) {
  76. $preHooks = array();
  77. $postHooks = array();
  78. $requiredPermissions = \OCP\Constants::PERMISSION_READ;
  79. switch ($mode) {
  80. case 'r+':
  81. case 'rb+':
  82. case 'w+':
  83. case 'wb+':
  84. case 'x+':
  85. case 'xb+':
  86. case 'a+':
  87. case 'ab+':
  88. case 'w':
  89. case 'wb':
  90. case 'x':
  91. case 'xb':
  92. case 'a':
  93. case 'ab':
  94. $preHooks[] = 'preWrite';
  95. $postHooks[] = 'postWrite';
  96. $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
  97. break;
  98. }
  99. if ($this->checkPermissions($requiredPermissions)) {
  100. $this->sendHooks($preHooks);
  101. $result = $this->view->fopen($this->path, $mode);
  102. $this->sendHooks($postHooks);
  103. return $result;
  104. } else {
  105. throw new NotPermittedException();
  106. }
  107. }
  108. public function delete() {
  109. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  110. $this->sendHooks(array('preDelete'));
  111. $fileInfo = $this->getFileInfo();
  112. $this->view->unlink($this->path);
  113. $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
  114. $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
  115. $this->exists = false;
  116. $this->fileInfo = null;
  117. } else {
  118. throw new NotPermittedException();
  119. }
  120. }
  121. /**
  122. * @param string $type
  123. * @param bool $raw
  124. * @return string
  125. */
  126. public function hash($type, $raw = false) {
  127. return $this->view->hash($type, $this->path, $raw);
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function getChecksum() {
  133. return $this->getFileInfo()->getChecksum();
  134. }
  135. public function getExtension(): string {
  136. return $this->getFileInfo()->getExtension();
  137. }
  138. }