StorageObjectStore.php 2.4 KB

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