SimpleFile.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @throws NotFoundException
  91. */
  92. public function putContent($data) {
  93. try {
  94. return $this->file->putContent($data);
  95. } catch (NotFoundException $e) {
  96. $this->checkFile();
  97. }
  98. }
  99. /**
  100. * Sometimes there are some issues with the AppData. Most of them are from
  101. * user error. But we should handle them gracefull anyway.
  102. *
  103. * If for some reason the current file can't be found. We remove it.
  104. * Then traverse up and check all folders if they exists. This so that the
  105. * next request will have a valid appdata structure again.
  106. *
  107. * @throws NotFoundException
  108. */
  109. private function checkFile() {
  110. $cur = $this->file;
  111. while ($cur->stat() === false) {
  112. $parent = $cur->getParent();
  113. try {
  114. $cur->delete();
  115. } catch (NotFoundException $e) {
  116. // Just continue then
  117. }
  118. $cur = $parent;
  119. }
  120. if ($cur !== $this->file) {
  121. throw new NotFoundException('File does not exist');
  122. }
  123. }
  124. /**
  125. * Delete the file
  126. *
  127. * @throws NotPermittedException
  128. */
  129. public function delete() {
  130. $this->file->delete();
  131. }
  132. /**
  133. * Get the MimeType
  134. *
  135. * @return string
  136. */
  137. public function getMimeType() {
  138. return $this->file->getMimeType();
  139. }
  140. /**
  141. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  142. *
  143. * @return resource
  144. * @throws \OCP\Files\NotPermittedException
  145. * @since 14.0.0
  146. */
  147. public function read() {
  148. return $this->file->fopen('r');
  149. }
  150. /**
  151. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  152. *
  153. * @return resource
  154. * @throws \OCP\Files\NotPermittedException
  155. * @since 14.0.0
  156. */
  157. public function write() {
  158. return $this->file->fopen('w');
  159. }
  160. }