StorageMigrator.php 3.9 KB

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