storage.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright (c) 2013 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\Cache;
  9. /**
  10. * Class Storage
  11. *
  12. * cache storage specific data
  13. *
  14. * @package OC\Files\Cache
  15. */
  16. class Storage {
  17. private $storageId;
  18. private $numericId;
  19. /**
  20. * @param \OC\Files\Storage\Storage|string $storage
  21. */
  22. public function __construct($storage) {
  23. if ($storage instanceof \OC\Files\Storage\Storage) {
  24. $this->storageId = $storage->getId();
  25. } else {
  26. $this->storageId = $storage;
  27. }
  28. if (strlen($this->storageId) > 64) {
  29. $this->storageId = md5($this->storageId);
  30. }
  31. $sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
  32. $result = \OC_DB::executeAudited($sql, array($this->storageId));
  33. if ($row = $result->fetchRow()) {
  34. $this->numericId = $row['numeric_id'];
  35. } else {
  36. $sql = 'INSERT INTO `*PREFIX*storages` (`id`) VALUES(?)';
  37. \OC_DB::executeAudited($sql, array($this->storageId));
  38. $this->numericId = \OC_DB::insertid('*PREFIX*storages');
  39. }
  40. }
  41. public function getNumericId() {
  42. return $this->numericId;
  43. }
  44. public static function getStorageId($numericId) {
  45. $sql = 'SELECT `id` FROM `*PREFIX*storages` WHERE `numeric_id` = ?';
  46. $result = \OC_DB::executeAudited($sql, array($numericId));
  47. if ($row = $result->fetchRow()) {
  48. return $row['id'];
  49. } else {
  50. return null;
  51. }
  52. }
  53. }