NewSimpleFile.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Robin Appelman <robin@icewind.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 Icewind\Streams\CallbackWrapper;
  27. use OCP\Files\File;
  28. use OCP\Files\Folder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\Files\SimpleFS\ISimpleFile;
  32. class NewSimpleFile implements ISimpleFile {
  33. private Folder $parentFolder;
  34. private string $name;
  35. private ?File $file = null;
  36. /**
  37. * File constructor.
  38. */
  39. public function __construct(Folder $parentFolder, string $name) {
  40. $this->parentFolder = $parentFolder;
  41. $this->name = $name;
  42. }
  43. /**
  44. * Get the name
  45. */
  46. public function getName(): string {
  47. return $this->name;
  48. }
  49. /**
  50. * Get the size in bytes
  51. */
  52. public function getSize(): int|float {
  53. if ($this->file) {
  54. return $this->file->getSize();
  55. } else {
  56. return 0;
  57. }
  58. }
  59. /**
  60. * Get the ETag
  61. */
  62. public function getETag(): string {
  63. if ($this->file) {
  64. return $this->file->getEtag();
  65. } else {
  66. return '';
  67. }
  68. }
  69. /**
  70. * Get the last modification time
  71. */
  72. public function getMTime(): int {
  73. if ($this->file) {
  74. return $this->file->getMTime();
  75. } else {
  76. return time();
  77. }
  78. }
  79. /**
  80. * Get the content
  81. *
  82. * @throws NotFoundException
  83. * @throws NotPermittedException
  84. */
  85. public function getContent(): string {
  86. if ($this->file) {
  87. $result = $this->file->getContent();
  88. if ($result === false) {
  89. $this->checkFile();
  90. }
  91. return $result;
  92. } else {
  93. return '';
  94. }
  95. }
  96. /**
  97. * Overwrite the file
  98. *
  99. * @param string|resource $data
  100. * @throws NotPermittedException
  101. * @throws NotFoundException
  102. */
  103. public function putContent($data): void {
  104. try {
  105. if ($this->file) {
  106. $this->file->putContent($data);
  107. } else {
  108. $this->file = $this->parentFolder->newFile($this->name, $data);
  109. }
  110. } catch (NotFoundException $e) {
  111. $this->checkFile();
  112. }
  113. }
  114. /**
  115. * Sometimes there are some issues with the AppData. Most of them are from
  116. * user error. But we should handle them gracefully anyway.
  117. *
  118. * If for some reason the current file can't be found. We remove it.
  119. * Then traverse up and check all folders if they exists. This so that the
  120. * next request will have a valid appdata structure again.
  121. *
  122. * @throws NotFoundException
  123. */
  124. private function checkFile(): void {
  125. if (!$this->file) {
  126. throw new NotFoundException('File not set');
  127. }
  128. $cur = $this->file;
  129. while ($cur->stat() === false) {
  130. $parent = $cur->getParent();
  131. try {
  132. $cur->delete();
  133. } catch (NotFoundException $e) {
  134. // Just continue then
  135. }
  136. $cur = $parent;
  137. }
  138. if ($cur !== $this->file) {
  139. throw new NotFoundException('File does not exist');
  140. }
  141. }
  142. /**
  143. * Delete the file
  144. *
  145. * @throws NotPermittedException
  146. */
  147. public function delete(): void {
  148. if ($this->file) {
  149. $this->file->delete();
  150. }
  151. }
  152. /**
  153. * Get the MimeType
  154. *
  155. * @return string
  156. */
  157. public function getMimeType(): string {
  158. if ($this->file) {
  159. return $this->file->getMimeType();
  160. } else {
  161. return 'text/plain';
  162. }
  163. }
  164. /**
  165. * {@inheritDoc}
  166. */
  167. public function getExtension(): string {
  168. if ($this->file) {
  169. return $this->file->getExtension();
  170. } else {
  171. return \pathinfo($this->name, PATHINFO_EXTENSION);
  172. }
  173. }
  174. /**
  175. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  176. *
  177. * @return resource|false
  178. * @throws \OCP\Files\NotPermittedException
  179. * @since 14.0.0
  180. */
  181. public function read() {
  182. if ($this->file) {
  183. return $this->file->fopen('r');
  184. } else {
  185. return fopen('php://temp', 'r');
  186. }
  187. }
  188. /**
  189. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  190. *
  191. * @return resource|bool
  192. * @throws \OCP\Files\NotPermittedException
  193. * @since 14.0.0
  194. */
  195. public function write() {
  196. if ($this->file) {
  197. return $this->file->fopen('w');
  198. } else {
  199. $source = fopen('php://temp', 'w+');
  200. return CallbackWrapper::wrap($source, null, null, null, null, function () use ($source) {
  201. rewind($source);
  202. $this->putContent($source);
  203. });
  204. }
  205. }
  206. }