1
0

SimpleFile.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Files\SimpleFS;
  24. use OCP\Files\File;
  25. use OCP\Files\NotFoundException;
  26. use OCP\Files\NotPermittedException;
  27. use OCP\Files\SimpleFS\ISimpleFile;
  28. class SimpleFile implements ISimpleFile {
  29. /** @var File $file */
  30. private $file;
  31. /**
  32. * File constructor.
  33. *
  34. * @param File $file
  35. */
  36. public function __construct(File $file) {
  37. $this->file = $file;
  38. }
  39. /**
  40. * Get the name
  41. *
  42. * @return string
  43. */
  44. public function getName() {
  45. return $this->file->getName();
  46. }
  47. /**
  48. * Get the size in bytes
  49. *
  50. * @return int
  51. */
  52. public function getSize() {
  53. return $this->file->getSize();
  54. }
  55. /**
  56. * Get the ETag
  57. *
  58. * @return string
  59. */
  60. public function getETag() {
  61. return $this->file->getEtag();
  62. }
  63. /**
  64. * Get the last modification time
  65. *
  66. * @return int
  67. */
  68. public function getMTime() {
  69. return $this->file->getMTime();
  70. }
  71. /**
  72. * Get the content
  73. *
  74. * @throws NotPermittedException
  75. * @throws NotFoundException
  76. * @return string
  77. */
  78. public function getContent() {
  79. $result = $this->file->getContent();
  80. if ($result === false) {
  81. $this->checkFile();
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Overwrite the file
  87. *
  88. * @param string|resource $data
  89. * @throws NotPermittedException
  90. */
  91. public function putContent($data) {
  92. $this->file->putContent($data);
  93. }
  94. /**
  95. * Sometimes there are some issues with the AppData. Most of them are from
  96. * user error. But we should handle them gracefull anyway.
  97. *
  98. * If for some reason the current file can't be found. We remove it.
  99. * Then traverse up and check all folders if they exists. This so that the
  100. * next request will have a valid appdata structure again.
  101. *
  102. * @throws NotFoundException
  103. */
  104. private function checkFile() {
  105. $cur = $this->file;
  106. while ($cur->stat() === false) {
  107. $parent = $cur->getParent();
  108. $cur->delete();
  109. $cur = $parent;
  110. }
  111. if ($cur !== $this->file) {
  112. throw new NotFoundException('File does not exist');
  113. }
  114. }
  115. /**
  116. * Delete the file
  117. *
  118. * @throws NotPermittedException
  119. */
  120. public function delete() {
  121. $this->file->delete();
  122. }
  123. /**
  124. * Get the MimeType
  125. *
  126. * @return string
  127. */
  128. public function getMimeType() {
  129. return $this->file->getMimeType();
  130. }
  131. /**
  132. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  133. *
  134. * @return resource
  135. * @throws \OCP\Files\NotPermittedException
  136. * @since 14.0.0
  137. */
  138. public function read() {
  139. return $this->file->fopen('r');
  140. }
  141. /**
  142. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  143. *
  144. * @return resource
  145. * @throws \OCP\Files\NotPermittedException
  146. * @since 14.0.0
  147. */
  148. public function write() {
  149. return $this->file->fopen('w');
  150. }
  151. }