SimpleFile.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. private File $file;
  32. public function __construct(File $file) {
  33. $this->file = $file;
  34. }
  35. /**
  36. * Get the name
  37. */
  38. public function getName(): string {
  39. return $this->file->getName();
  40. }
  41. /**
  42. * Get the size in bytes
  43. */
  44. public function getSize(): int {
  45. return $this->file->getSize();
  46. }
  47. /**
  48. * Get the ETag
  49. */
  50. public function getETag(): string {
  51. return $this->file->getEtag();
  52. }
  53. /**
  54. * Get the last modification time
  55. */
  56. public function getMTime(): int {
  57. return $this->file->getMTime();
  58. }
  59. /**
  60. * Get the content
  61. *
  62. * @throws NotPermittedException
  63. * @throws NotFoundException
  64. */
  65. public function getContent(): string {
  66. $result = $this->file->getContent();
  67. if ($result === false) {
  68. $this->checkFile();
  69. }
  70. return $result;
  71. }
  72. /**
  73. * Overwrite the file
  74. *
  75. * @param string|resource $data
  76. * @throws NotPermittedException
  77. * @throws NotFoundException
  78. */
  79. public function putContent($data): void {
  80. try {
  81. $this->file->putContent($data);
  82. } catch (NotFoundException $e) {
  83. $this->checkFile();
  84. }
  85. }
  86. /**
  87. * Sometimes there are some issues with the AppData. Most of them are from
  88. * user error. But we should handle them gracefully anyway.
  89. *
  90. * If for some reason the current file can't be found. We remove it.
  91. * Then traverse up and check all folders if they exists. This so that the
  92. * next request will have a valid appdata structure again.
  93. *
  94. * @throws NotFoundException
  95. */
  96. private function checkFile(): void {
  97. $cur = $this->file;
  98. while ($cur->stat() === false) {
  99. $parent = $cur->getParent();
  100. try {
  101. $cur->delete();
  102. } catch (NotFoundException $e) {
  103. // Just continue then
  104. }
  105. $cur = $parent;
  106. }
  107. if ($cur !== $this->file) {
  108. throw new NotFoundException('File does not exist');
  109. }
  110. }
  111. /**
  112. * Delete the file
  113. *
  114. * @throws NotPermittedException
  115. */
  116. public function delete(): void {
  117. $this->file->delete();
  118. }
  119. /**
  120. * Get the MimeType
  121. */
  122. public function getMimeType(): string {
  123. return $this->file->getMimeType();
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function getExtension(): string {
  129. return $this->file->getExtension();
  130. }
  131. /**
  132. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  133. *
  134. * @return resource
  135. * @throws \OCP\Files\NotPermittedException
  136. * @since 14.0.0
  137. */
  138. public function read() {
  139. return $this->file->fopen('r');
  140. }
  141. /**
  142. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  143. *
  144. * @return resource|false
  145. * @throws \OCP\Files\NotPermittedException
  146. * @since 14.0.0
  147. */
  148. public function write() {
  149. return $this->file->fopen('w');
  150. }
  151. public function getId(): int {
  152. return $this->file->getId();
  153. }
  154. }