StorageObjectStore.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Files\ObjectStore;
  24. use OCP\Files\ObjectStore\IObjectStore;
  25. use OCP\Files\Storage\IStorage;
  26. /**
  27. * Object store that wraps a storage backend, mostly for testing purposes
  28. */
  29. class StorageObjectStore implements IObjectStore {
  30. /** @var IStorage */
  31. private $storage;
  32. /**
  33. * @param IStorage $storage
  34. */
  35. public function __construct(IStorage $storage) {
  36. $this->storage = $storage;
  37. }
  38. /**
  39. * @return string the container or bucket name where objects are stored
  40. * @since 7.0.0
  41. */
  42. function getStorageId() {
  43. $this->storage->getId();
  44. }
  45. /**
  46. * @param string $urn the unified resource name used to identify the object
  47. * @return resource stream with the read data
  48. * @throws \Exception when something goes wrong, message will be logged
  49. * @since 7.0.0
  50. */
  51. function readObject($urn) {
  52. $handle = $this->storage->fopen($urn, 'r');
  53. if ($handle) {
  54. return $handle;
  55. } else {
  56. throw new \Exception();
  57. }
  58. }
  59. /**
  60. * @param string $urn the unified resource name used to identify the object
  61. * @param resource $stream stream with the data to write
  62. * @throws \Exception when something goes wrong, message will be logged
  63. * @since 7.0.0
  64. */
  65. function writeObject($urn, $stream) {
  66. $handle = $this->storage->fopen($urn, 'w');
  67. if ($handle) {
  68. stream_copy_to_stream($stream, $handle);
  69. fclose($handle);
  70. } else {
  71. throw new \Exception();
  72. }
  73. }
  74. /**
  75. * @param string $urn the unified resource name used to identify the object
  76. * @return void
  77. * @throws \Exception when something goes wrong, message will be logged
  78. * @since 7.0.0
  79. */
  80. function deleteObject($urn) {
  81. $this->storage->unlink($urn);
  82. }
  83. }