Storage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Cache;
  29. use OCP\Files\Storage\IStorage;
  30. /**
  31. * Handle the mapping between the string and numeric storage ids
  32. *
  33. * Each storage has 2 different ids
  34. * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
  35. * and a numeric storage id which is referenced in the file cache
  36. *
  37. * A mapping between the two storage ids is stored in the database and accessible trough this class
  38. *
  39. * @package OC\Files\Cache
  40. */
  41. class Storage {
  42. /** @var StorageGlobal|null */
  43. private static $globalCache = null;
  44. private $storageId;
  45. private $numericId;
  46. /**
  47. * @return StorageGlobal
  48. */
  49. public static function getGlobalCache() {
  50. if (is_null(self::$globalCache)) {
  51. self::$globalCache = new StorageGlobal(\OC::$server->getDatabaseConnection());
  52. }
  53. return self::$globalCache;
  54. }
  55. /**
  56. * @param \OC\Files\Storage\Storage|string $storage
  57. * @param bool $isAvailable
  58. * @throws \RuntimeException
  59. */
  60. public function __construct($storage, $isAvailable = true) {
  61. if ($storage instanceof IStorage) {
  62. $this->storageId = $storage->getId();
  63. } else {
  64. $this->storageId = $storage;
  65. }
  66. $this->storageId = self::adjustStorageId($this->storageId);
  67. if ($row = self::getStorageById($this->storageId)) {
  68. $this->numericId = (int)$row['numeric_id'];
  69. } else {
  70. $connection = \OC::$server->getDatabaseConnection();
  71. $available = $isAvailable ? 1 : 0;
  72. if ($connection->insertIfNotExist('*PREFIX*storages', ['id' => $this->storageId, 'available' => $available])) {
  73. $this->numericId = (int)$connection->lastInsertId('*PREFIX*storages');
  74. } else {
  75. if ($row = self::getStorageById($this->storageId)) {
  76. $this->numericId = (int)$row['numeric_id'];
  77. } else {
  78. throw new \RuntimeException('Storage could neither be inserted nor be selected from the database');
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * @param string $storageId
  85. * @return array
  86. */
  87. public static function getStorageById($storageId) {
  88. return self::getGlobalCache()->getStorageInfo($storageId);
  89. }
  90. /**
  91. * Adjusts the storage id to use md5 if too long
  92. * @param string $storageId storage id
  93. * @return string unchanged $storageId if its length is less than 64 characters,
  94. * else returns the md5 of $storageId
  95. */
  96. public static function adjustStorageId($storageId) {
  97. if (strlen($storageId) > 64) {
  98. return md5($storageId);
  99. }
  100. return $storageId;
  101. }
  102. /**
  103. * Get the numeric id for the storage
  104. *
  105. * @return int
  106. */
  107. public function getNumericId() {
  108. return $this->numericId;
  109. }
  110. /**
  111. * Get the string id for the storage
  112. *
  113. * @param int $numericId
  114. * @return string|null either the storage id string or null if the numeric id is not known
  115. */
  116. public static function getStorageId($numericId) {
  117. $sql = 'SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?';
  118. $result = \OC_DB::executeAudited($sql, array($numericId));
  119. if ($row = $result->fetchRow()) {
  120. return $row['id'];
  121. } else {
  122. return null;
  123. }
  124. }
  125. /**
  126. * Get the numeric of the storage with the provided string id
  127. *
  128. * @param $storageId
  129. * @return int|null either the numeric storage id or null if the storage id is not knwon
  130. */
  131. public static function getNumericStorageId($storageId) {
  132. $storageId = self::adjustStorageId($storageId);
  133. if ($row = self::getStorageById($storageId)) {
  134. return (int)$row['numeric_id'];
  135. } else {
  136. return null;
  137. }
  138. }
  139. /**
  140. * @return array|null [ available, last_checked ]
  141. */
  142. public function getAvailability() {
  143. if ($row = self::getStorageById($this->storageId)) {
  144. return [
  145. 'available' => (int)$row['available'] === 1,
  146. 'last_checked' => $row['last_checked']
  147. ];
  148. } else {
  149. return null;
  150. }
  151. }
  152. /**
  153. * @param bool $isAvailable
  154. */
  155. public function setAvailability($isAvailable) {
  156. $sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
  157. $available = $isAvailable ? 1 : 0;
  158. \OC_DB::executeAudited($sql, array($available, time(), $this->storageId));
  159. }
  160. /**
  161. * Check if a string storage id is known
  162. *
  163. * @param string $storageId
  164. * @return bool
  165. */
  166. public static function exists($storageId) {
  167. return !is_null(self::getNumericStorageId($storageId));
  168. }
  169. /**
  170. * remove the entry for the storage
  171. *
  172. * @param string $storageId
  173. */
  174. public static function remove($storageId) {
  175. $storageId = self::adjustStorageId($storageId);
  176. $numericId = self::getNumericStorageId($storageId);
  177. $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
  178. \OC_DB::executeAudited($sql, array($storageId));
  179. if (!is_null($numericId)) {
  180. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?';
  181. \OC_DB::executeAudited($sql, array($numericId));
  182. }
  183. }
  184. }