InMemoryFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Michael Weimann <mail@michael-weimann.eu>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  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 OCP\Files\SimpleFS;
  26. use OCP\Files\NotPermittedException;
  27. /**
  28. * This class represents a file that is only hold in memory.
  29. *
  30. * @since 16.0.0
  31. */
  32. class InMemoryFile implements ISimpleFile {
  33. /**
  34. * Holds the file name.
  35. */
  36. private string $name;
  37. /**
  38. * Holds the file contents.
  39. */
  40. private string $contents;
  41. /**
  42. * InMemoryFile constructor.
  43. *
  44. * @param string $name The file name
  45. * @param string $contents The file contents
  46. * @since 16.0.0
  47. */
  48. public function __construct(string $name, string $contents) {
  49. $this->name = $name;
  50. $this->contents = $contents;
  51. }
  52. /**
  53. * @inheritdoc
  54. * @since 16.0.0
  55. */
  56. public function getName(): string {
  57. return $this->name;
  58. }
  59. /**
  60. * @inheritdoc
  61. * @since 16.0.0
  62. */
  63. public function getSize(): int|float {
  64. return strlen($this->contents);
  65. }
  66. /**
  67. * @inheritdoc
  68. * @since 16.0.0
  69. */
  70. public function getETag(): string {
  71. return '';
  72. }
  73. /**
  74. * @inheritdoc
  75. * @since 16.0.0
  76. */
  77. public function getMTime(): int {
  78. return time();
  79. }
  80. /**
  81. * @inheritdoc
  82. * @since 16.0.0
  83. */
  84. public function getContent(): string {
  85. return $this->contents;
  86. }
  87. /**
  88. * @inheritdoc
  89. * @since 16.0.0
  90. */
  91. public function putContent($data): void {
  92. $this->contents = $data;
  93. }
  94. /**
  95. * In memory files can't be deleted.
  96. *
  97. * @since 16.0.0
  98. */
  99. public function delete(): void {
  100. // unimplemented for in memory files
  101. }
  102. /**
  103. * @inheritdoc
  104. * @since 16.0.0
  105. */
  106. public function getMimeType(): string {
  107. $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
  108. return $fileInfo->buffer($this->contents);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. * @since 24.0.0
  113. */
  114. public function getExtension(): string {
  115. return \pathinfo($this->name, PATHINFO_EXTENSION);
  116. }
  117. /**
  118. * Stream reading is unsupported for in memory files.
  119. *
  120. * @throws NotPermittedException
  121. * @since 16.0.0
  122. */
  123. public function read() {
  124. throw new NotPermittedException(
  125. 'Stream reading is unsupported for in memory files'
  126. );
  127. }
  128. /**
  129. * Stream writing isn't available for in memory files.
  130. *
  131. * @throws NotPermittedException
  132. * @since 16.0.0
  133. */
  134. public function write() {
  135. throw new NotPermittedException(
  136. 'Stream writing is unsupported for in memory files'
  137. );
  138. }
  139. }