StorageObjectStore.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\ObjectStore;
  7. use OCP\Files\ObjectStore\IObjectStore;
  8. use OCP\Files\Storage\IStorage;
  9. use function is_resource;
  10. /**
  11. * Object store that wraps a storage backend, mostly for testing purposes
  12. */
  13. class StorageObjectStore implements IObjectStore {
  14. /** @var IStorage */
  15. private $storage;
  16. /**
  17. * @param IStorage $storage
  18. */
  19. public function __construct(IStorage $storage) {
  20. $this->storage = $storage;
  21. }
  22. /**
  23. * @return string the container or bucket name where objects are stored
  24. * @since 7.0.0
  25. */
  26. public function getStorageId(): string {
  27. return $this->storage->getId();
  28. }
  29. /**
  30. * @param string $urn the unified resource name used to identify the object
  31. * @return resource stream with the read data
  32. * @throws \Exception when something goes wrong, message will be logged
  33. * @since 7.0.0
  34. */
  35. public function readObject($urn) {
  36. $handle = $this->storage->fopen($urn, 'r');
  37. if (is_resource($handle)) {
  38. return $handle;
  39. }
  40. throw new \Exception();
  41. }
  42. public function writeObject($urn, $stream, ?string $mimetype = null) {
  43. $handle = $this->storage->fopen($urn, 'w');
  44. if ($handle) {
  45. stream_copy_to_stream($stream, $handle);
  46. fclose($handle);
  47. } else {
  48. throw new \Exception();
  49. }
  50. }
  51. /**
  52. * @param string $urn the unified resource name used to identify the object
  53. * @return void
  54. * @throws \Exception when something goes wrong, message will be logged
  55. * @since 7.0.0
  56. */
  57. public function deleteObject($urn) {
  58. $this->storage->unlink($urn);
  59. }
  60. public function objectExists($urn) {
  61. return $this->storage->file_exists($urn);
  62. }
  63. public function copyObject($from, $to) {
  64. $this->storage->copy($from, $to);
  65. }
  66. }