storagemigrator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_external\Migration;
  22. use OCA\Files_External\Service\BackendService;
  23. use OCA\Files_External\Service\DBConfigService;
  24. use OCA\Files_external\Service\GlobalLegacyStoragesService;
  25. use OCA\Files_external\Service\GlobalStoragesService;
  26. use OCA\Files_external\Service\LegacyStoragesService;
  27. use OCA\Files_external\Service\StoragesService;
  28. use OCA\Files_external\Service\UserLegacyStoragesService;
  29. use OCA\Files_external\Service\UserStoragesService;
  30. use OCP\Files\Config\IUserMountCache;
  31. use OCP\IConfig;
  32. use OCP\IDBConnection;
  33. use OCP\ILogger;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. /**
  37. * Migrate mount config from mount.json to the database
  38. */
  39. class StorageMigrator {
  40. /**
  41. * @var BackendService
  42. */
  43. private $backendService;
  44. /**
  45. * @var DBConfigService
  46. */
  47. private $dbConfig;
  48. /**
  49. * @var IConfig
  50. */
  51. private $config;
  52. /**
  53. * @var IDBConnection
  54. */
  55. private $connection;
  56. /**
  57. * @var ILogger
  58. */
  59. private $logger;
  60. /** @var IUserMountCache */
  61. private $userMountCache;
  62. /**
  63. * StorageMigrator constructor.
  64. *
  65. * @param BackendService $backendService
  66. * @param DBConfigService $dbConfig
  67. * @param IConfig $config
  68. * @param IDBConnection $connection
  69. * @param ILogger $logger
  70. * @param IUserMountCache $userMountCache
  71. */
  72. public function __construct(
  73. BackendService $backendService,
  74. DBConfigService $dbConfig,
  75. IConfig $config,
  76. IDBConnection $connection,
  77. ILogger $logger,
  78. IUserMountCache $userMountCache
  79. ) {
  80. $this->backendService = $backendService;
  81. $this->dbConfig = $dbConfig;
  82. $this->config = $config;
  83. $this->connection = $connection;
  84. $this->logger = $logger;
  85. $this->userMountCache = $userMountCache;
  86. }
  87. private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) {
  88. $existingStorage = $legacyService->getAllStorages();
  89. $this->connection->beginTransaction();
  90. try {
  91. foreach ($existingStorage as $storage) {
  92. $storageService->addStorage($storage);
  93. }
  94. $this->connection->commit();
  95. } catch (\Exception $e) {
  96. $this->logger->logException($e);
  97. $this->connection->rollBack();
  98. }
  99. }
  100. /**
  101. * Migrate admin configured storages
  102. */
  103. public function migrateGlobal() {
  104. $legacyService = new GlobalLegacyStoragesService($this->backendService);
  105. $storageService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->userMountCache);
  106. $this->migrate($legacyService, $storageService);
  107. }
  108. /**
  109. * Migrate personal storages configured by the current user
  110. *
  111. * @param IUser $user
  112. */
  113. public function migrateUser(IUser $user) {
  114. $dummySession = new DummyUserSession();
  115. $dummySession->setUser($user);
  116. $userId = $user->getUID();
  117. $userVersion = $this->config->getUserValue($userId, 'files_external', 'config_version', '0.0.0');
  118. if (version_compare($userVersion, '0.5.0', '<')) {
  119. $this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0');
  120. $legacyService = new UserLegacyStoragesService($this->backendService, $dummySession);
  121. $storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession, $this->userMountCache);
  122. $this->migrate($legacyService, $storageService);
  123. }
  124. }
  125. }