You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
3.8 KiB

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