InMemoryFile.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Files\SimpleFS;
  8. use OCP\Files\NotPermittedException;
  9. /**
  10. * This class represents a file that is only hold in memory.
  11. *
  12. * @since 16.0.0
  13. */
  14. class InMemoryFile implements ISimpleFile {
  15. /**
  16. * Holds the file name.
  17. */
  18. private string $name;
  19. /**
  20. * Holds the file contents.
  21. */
  22. private string $contents;
  23. /**
  24. * InMemoryFile constructor.
  25. *
  26. * @param string $name The file name
  27. * @param string $contents The file contents
  28. * @since 16.0.0
  29. */
  30. public function __construct(string $name, string $contents) {
  31. $this->name = $name;
  32. $this->contents = $contents;
  33. }
  34. /**
  35. * @inheritdoc
  36. * @since 16.0.0
  37. */
  38. public function getName(): string {
  39. return $this->name;
  40. }
  41. /**
  42. * @inheritdoc
  43. * @since 16.0.0
  44. */
  45. public function getSize(): int|float {
  46. return strlen($this->contents);
  47. }
  48. /**
  49. * @inheritdoc
  50. * @since 16.0.0
  51. */
  52. public function getETag(): string {
  53. return '';
  54. }
  55. /**
  56. * @inheritdoc
  57. * @since 16.0.0
  58. */
  59. public function getMTime(): int {
  60. return time();
  61. }
  62. /**
  63. * @inheritdoc
  64. * @since 16.0.0
  65. */
  66. public function getContent(): string {
  67. return $this->contents;
  68. }
  69. /**
  70. * @inheritdoc
  71. * @since 16.0.0
  72. */
  73. public function putContent($data): void {
  74. $this->contents = $data;
  75. }
  76. /**
  77. * In memory files can't be deleted.
  78. *
  79. * @since 16.0.0
  80. */
  81. public function delete(): void {
  82. // unimplemented for in memory files
  83. }
  84. /**
  85. * @inheritdoc
  86. * @since 16.0.0
  87. */
  88. public function getMimeType(): string {
  89. $fileInfo = new \finfo(FILEINFO_MIME_TYPE);
  90. return $fileInfo->buffer($this->contents);
  91. }
  92. /**
  93. * {@inheritDoc}
  94. * @since 24.0.0
  95. */
  96. public function getExtension(): string {
  97. return \pathinfo($this->name, PATHINFO_EXTENSION);
  98. }
  99. /**
  100. * Stream reading is unsupported for in memory files.
  101. *
  102. * @throws NotPermittedException
  103. * @since 16.0.0
  104. */
  105. public function read() {
  106. throw new NotPermittedException(
  107. 'Stream reading is unsupported for in memory files'
  108. );
  109. }
  110. /**
  111. * Stream writing isn't available for in memory files.
  112. *
  113. * @throws NotPermittedException
  114. * @since 16.0.0
  115. */
  116. public function write() {
  117. throw new NotPermittedException(
  118. 'Stream writing is unsupported for in memory files'
  119. );
  120. }
  121. }