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.

NewSimpleFile.php 4.7KB

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