SimpleFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\SimpleFS;
  7. use OCP\Files\File;
  8. use OCP\Files\NotFoundException;
  9. use OCP\Files\NotPermittedException;
  10. use OCP\Files\SimpleFS\ISimpleFile;
  11. class SimpleFile implements ISimpleFile {
  12. private File $file;
  13. public function __construct(File $file) {
  14. $this->file = $file;
  15. }
  16. /**
  17. * Get the name
  18. */
  19. public function getName(): string {
  20. return $this->file->getName();
  21. }
  22. /**
  23. * Get the size in bytes
  24. */
  25. public function getSize(): int|float {
  26. return $this->file->getSize();
  27. }
  28. /**
  29. * Get the ETag
  30. */
  31. public function getETag(): string {
  32. return $this->file->getEtag();
  33. }
  34. /**
  35. * Get the last modification time
  36. */
  37. public function getMTime(): int {
  38. return $this->file->getMTime();
  39. }
  40. /**
  41. * Get the content
  42. *
  43. * @throws NotPermittedException
  44. * @throws NotFoundException
  45. */
  46. public function getContent(): string {
  47. $result = $this->file->getContent();
  48. if ($result === false) {
  49. $this->checkFile();
  50. }
  51. return $result;
  52. }
  53. /**
  54. * Overwrite the file
  55. *
  56. * @param string|resource $data
  57. * @throws NotPermittedException
  58. * @throws NotFoundException
  59. */
  60. public function putContent($data): void {
  61. try {
  62. $this->file->putContent($data);
  63. } catch (NotFoundException $e) {
  64. $this->checkFile();
  65. }
  66. }
  67. /**
  68. * Sometimes there are some issues with the AppData. Most of them are from
  69. * user error. But we should handle them gracefully anyway.
  70. *
  71. * If for some reason the current file can't be found. We remove it.
  72. * Then traverse up and check all folders if they exists. This so that the
  73. * next request will have a valid appdata structure again.
  74. *
  75. * @throws NotFoundException
  76. */
  77. private function checkFile(): void {
  78. $cur = $this->file;
  79. while ($cur->stat() === false) {
  80. $parent = $cur->getParent();
  81. try {
  82. $cur->delete();
  83. } catch (NotFoundException $e) {
  84. // Just continue then
  85. }
  86. $cur = $parent;
  87. }
  88. if ($cur !== $this->file) {
  89. throw new NotFoundException('File does not exist');
  90. }
  91. }
  92. /**
  93. * Delete the file
  94. *
  95. * @throws NotPermittedException
  96. */
  97. public function delete(): void {
  98. $this->file->delete();
  99. }
  100. /**
  101. * Get the MimeType
  102. */
  103. public function getMimeType(): string {
  104. return $this->file->getMimeType();
  105. }
  106. /**
  107. * {@inheritDoc}
  108. */
  109. public function getExtension(): string {
  110. return $this->file->getExtension();
  111. }
  112. /**
  113. * Open the file as stream for reading, resulting resource can be operated as stream like the result from php's own fopen
  114. *
  115. * @return resource|false
  116. * @throws \OCP\Files\NotPermittedException
  117. * @since 14.0.0
  118. */
  119. public function read() {
  120. return $this->file->fopen('r');
  121. }
  122. /**
  123. * Open the file as stream for writing, resulting resource can be operated as stream like the result from php's own fopen
  124. *
  125. * @return resource|false
  126. * @throws \OCP\Files\NotPermittedException
  127. * @since 14.0.0
  128. */
  129. public function write() {
  130. return $this->file->fopen('w');
  131. }
  132. public function getId(): int {
  133. return $this->file->getId();
  134. }
  135. }