StorageObjectStore.php 2.5 KB

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