StorageObjectStore.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\FileInfo;
  26. use OCP\Files\ObjectStore\IObjectStore;
  27. use OCP\Files\Storage\IStorage;
  28. use function is_resource;
  29. /**
  30. * Object store that wraps a storage backend, mostly for testing purposes
  31. */
  32. class StorageObjectStore implements IObjectStore {
  33. /** @var IStorage */
  34. private $storage;
  35. /**
  36. * @param IStorage $storage
  37. */
  38. public function __construct(IStorage $storage) {
  39. $this->storage = $storage;
  40. }
  41. /**
  42. * @return string the container or bucket name where objects are stored
  43. * @since 7.0.0
  44. */
  45. public function getStorageId() {
  46. $this->storage->getId();
  47. }
  48. /**
  49. * @param string $urn the unified resource name used to identify the object
  50. * @return resource stream with the read data
  51. * @throws \Exception when something goes wrong, message will be logged
  52. * @since 7.0.0
  53. */
  54. public function readObject($urn) {
  55. $handle = $this->storage->fopen($urn, 'r');
  56. if (is_resource($handle)) {
  57. return $handle;
  58. }
  59. throw new \Exception();
  60. }
  61. public function writeObject($urn, $stream, string $mimetype = null) {
  62. $handle = $this->storage->fopen($urn, 'w');
  63. if ($handle) {
  64. stream_copy_to_stream($stream, $handle);
  65. fclose($handle);
  66. } else {
  67. throw new \Exception();
  68. }
  69. }
  70. /**
  71. * @param string $urn the unified resource name used to identify the object
  72. * @return void
  73. * @throws \Exception when something goes wrong, message will be logged
  74. * @since 7.0.0
  75. */
  76. public function deleteObject($urn) {
  77. $this->storage->unlink($urn);
  78. }
  79. public function objectExists($urn) {
  80. return $this->storage->file_exists($urn);
  81. }
  82. public function copyObject($from, $to) {
  83. $this->storage->copy($from, $to);
  84. }
  85. public function bytesUsed(): int {
  86. return FileInfo::SPACE_UNKNOWN;
  87. }
  88. public function bytesQuota(): int {
  89. return FileInfo::SPACE_UNLIMITED;
  90. }
  91. }