PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/files/mount/mount.php

https://github.com/sezuan/core
PHP | 141 lines | 116 code | 6 blank | 19 comment | 6 complexity | b09672cefd2193cfc6a4211d8d3600e2 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Mount;
  9. use \OC\Files\Filesystem;
  10. use OC\Files\Storage\Loader;
  11. use OC\Files\Storage\Storage;
  12. class Mount {
  13. /**
  14. * @var \OC\Files\Storage\Storage $storage
  15. */
  16. private $storage = null;
  17. private $class;
  18. private $storageId;
  19. private $arguments = array();
  20. private $mountPoint;
  21. /**
  22. * @var \OC\Files\Storage\Loader $loader
  23. */
  24. private $loader;
  25. /**
  26. * @param string | \OC\Files\Storage\Storage $storage
  27. * @param string $mountpoint
  28. * @param array $arguments (optional)\
  29. * @param \OC\Files\Storage\Loader $loader
  30. */
  31. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  32. if (is_null($arguments)) {
  33. $arguments = array();
  34. }
  35. if (is_null($loader)) {
  36. $this->loader = new Loader();
  37. } else {
  38. $this->loader = $loader;
  39. }
  40. $mountpoint = $this->formatPath($mountpoint);
  41. if ($storage instanceof Storage) {
  42. $this->class = get_class($storage);
  43. $this->storage = $this->loader->wrap($mountpoint, $storage);
  44. } else {
  45. // Update old classes to new namespace
  46. if (strpos($storage, 'OC_Filestorage_') !== false) {
  47. $storage = '\OC\Files\Storage\\' . substr($storage, 15);
  48. }
  49. $this->class = $storage;
  50. $this->arguments = $arguments;
  51. }
  52. $this->mountPoint = $mountpoint;
  53. }
  54. /**
  55. * @return string
  56. */
  57. public function getMountPoint() {
  58. return $this->mountPoint;
  59. }
  60. /**
  61. * create the storage that is mounted
  62. *
  63. * @return \OC\Files\Storage\Storage
  64. */
  65. private function createStorage() {
  66. if (class_exists($this->class)) {
  67. try {
  68. return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
  69. } catch (\Exception $exception) {
  70. \OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
  71. return null;
  72. }
  73. } else {
  74. \OC_Log::write('core', 'storage backend ' . $this->class . ' not found', \OC_Log::ERROR);
  75. return null;
  76. }
  77. }
  78. /**
  79. * @return \OC\Files\Storage\Storage
  80. */
  81. public function getStorage() {
  82. if (is_null($this->storage)) {
  83. $this->storage = $this->createStorage();
  84. }
  85. return $this->storage;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getStorageId() {
  91. if (!$this->storageId) {
  92. if (is_null($this->storage)) {
  93. $storage = $this->createStorage(); //FIXME: start using exceptions
  94. if (is_null($storage)) {
  95. return null;
  96. }
  97. $this->storage = $storage;
  98. }
  99. $this->storageId = $this->storage->getId();
  100. if (strlen($this->storageId) > 64) {
  101. $this->storageId = md5($this->storageId);
  102. }
  103. }
  104. return $this->storageId;
  105. }
  106. /**
  107. * @param string $path
  108. * @return string
  109. */
  110. public function getInternalPath($path) {
  111. if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
  112. $internalPath = '';
  113. } else {
  114. $internalPath = substr($path, strlen($this->mountPoint));
  115. }
  116. return $internalPath;
  117. }
  118. /**
  119. * @param string $path
  120. * @return string
  121. */
  122. private function formatPath($path) {
  123. $path = Filesystem::normalizePath($path);
  124. if (strlen($path) > 1) {
  125. $path .= '/';
  126. }
  127. return $path;
  128. }
  129. }