storage.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public static function exists($storageId) {
  54. if (strlen($storageId) > 64) {
  55. $storageId = md5($storageId);
  56. }
  57. $sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
  58. $result = \OC_DB::executeAudited($sql, array($storageId));
  59. if ($row = $result->fetchRow()) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. }