NewSimpleFile.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Files\SimpleFS;
  8. use Icewind\Streams\CallbackWrapper;
  9. use OCP\Files\File;
  10. use OCP\Files\Folder;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. use OCP\Files\SimpleFS\ISimpleFile;
  14. class NewSimpleFile implements ISimpleFile {
  15. private Folder $parentFolder;
  16. private string $name;
  17. private ?File $file = null;
  18. /**
  19. * File constructor.
  20. */
  21. public function __construct(Folder $parentFolder, string $name) {
  22. $this->parentFolder = $parentFolder;
  23. $this->name = $name;
  24. }
  25. /**
  26. * Get the name
  27. */
  28. public function getName(): string {
  29. return $this->name;
  30. }
  31. /**
  32. * Get the size in bytes
  33. */
  34. public function getSize(): int|float {
  35. if ($this->file) {
  36. return $this->file->getSize();
  37. } else {
  38. return 0;
  39. }
  40. }
  41. /**
  42. * Get the ETag
  43. */
  44. public function getETag(): string {
  45. if ($this->file) {
  46. return $this->file->getEtag();
  47. } else {
  48. return '';
  49. }
  50. }
  51. /**
  52. * Get the last modification time
  53. */
  54. public function getMTime(): int {
  55. if ($this->file) {
  56. return $this->file->getMTime();
  57. } else {
  58. return time();
  59. }
  60. }
  61. /**
  62. * Get the content
  63. *
  64. * @throws NotFoundException
  65. * @throws NotPermittedException
  66. */
  67. public function getContent(): string {
  68. if ($this->file) {
  69. $result = $this->file->getContent();
  70. if ($result === false) {
  71. $this->checkFile();
  72. }
  73. return $result;
  74. } else {
  75. return '';
  76. }
  77. }
  78. /**
  79. * Overwrite the file
  80. *
  81. * @param string|resource $data
  82. * @throws NotPermittedException
  83. * @throws NotFoundException
  84. */
  85. public function putContent($data): void {
  86. try {
  87. if ($this->file) {
  88. $this->file->putContent($data);
  89. } else {
  90. $this->file = $this->parentFolder->newFile($this->name, $data);
  91. }
  92. } catch (NotFoundException $e) {
  93. $this->checkFile();
  94. }
  95. }
  96. /**
  97. * Sometimes there are some issues with the AppData. Most of them are from
  98. * user error. But we should handle them gracefully anyway.
  99. *
  100. * If for some reason the current file can't be found. We remove it.
  101. * Then traverse up and check all folders if they exists. This so that the
  102. * next request will have a valid appdata structure again.
  103. *
  104. * @throws NotFoundException
  105. */
  106. private function checkFile(): void {
  107. if (!$this->file) {
  108. throw new NotFoundException('File not set');
  109. }
  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(): void {
  130. if ($this->file) {
  131. $this->file->delete();
  132. }
  133. }
  134. /**
  135. * Get the MimeType
  136. *
  137. * @return string
  138. */
  139. public function getMimeType(): string {
  140. if ($this->file) {
  141. return $this->file->getMimeType();
  142. } else {
  143. return 'text/plain';
  144. }
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getExtension(): string {
  150. if ($this->file) {
  151. return $this->file->getExtension();
  152. } else {
  153. return \pathinfo($this->name, PATHINFO_EXTENSION);
  154. }
  155. }
  156. /**
  157. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  158. *
  159. * @return resource|false
  160. * @throws \OCP\Files\NotPermittedException
  161. * @since 14.0.0
  162. */
  163. public function read() {
  164. if ($this->file) {
  165. return $this->file->fopen('r');
  166. } else {
  167. return fopen('php://temp', 'r');
  168. }
  169. }
  170. /**
  171. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  172. *
  173. * @return resource|bool
  174. * @throws \OCP\Files\NotPermittedException
  175. * @since 14.0.0
  176. */
  177. public function write() {
  178. if ($this->file) {
  179. return $this->file->fopen('w');
  180. } else {
  181. $source = fopen('php://temp', 'w+');
  182. return CallbackWrapper::wrap($source, null, null, null, null, function () use ($source) {
  183. rewind($source);
  184. $this->putContent($source);
  185. });
  186. }
  187. }
  188. }