|
18 | 18 | use OCA\Files_External\Service\UserStoragesService; |
19 | 19 | use OCP\AppFramework\QueryException; |
20 | 20 | use OCP\Files\Config\IMountProvider; |
| 21 | +use OCP\Files\Config\IPartialMountProvider; |
21 | 22 | use OCP\Files\Mount\IMountPoint; |
22 | 23 | use OCP\Files\ObjectStore\IObjectStore; |
23 | 24 | use OCP\Files\Storage\IConstructableStorage; |
|
26 | 27 | use OCP\Files\StorageNotAvailableException; |
27 | 28 | use OCP\IUser; |
28 | 29 | use OCP\Server; |
| 30 | +use Override; |
29 | 31 | use Psr\Clock\ClockInterface; |
30 | 32 | use Psr\Log\LoggerInterface; |
31 | 33 |
|
32 | 34 | /** |
33 | 35 | * Make the old files_external config work with the new public mount config api |
34 | 36 | */ |
35 | | -class ConfigAdapter implements IMountProvider { |
| 37 | +class ConfigAdapter implements IMountProvider, IPartialMountProvider { |
36 | 38 | public function __construct( |
37 | 39 | private UserStoragesService $userStoragesService, |
38 | 40 | private UserGlobalStoragesService $userGlobalStoragesService, |
@@ -168,4 +170,98 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
168 | 170 |
|
169 | 171 | return $mounts; |
170 | 172 | } |
| 173 | + |
| 174 | + #[Override] |
| 175 | + public function getMountsForPath(string $path, bool $forChildren, array $mountProviderArgs, IStorageFactory $loader): array { |
| 176 | + if (empty($mountProviderArgs)) { |
| 177 | + return []; |
| 178 | + } |
| 179 | + |
| 180 | + $userId = null; |
| 181 | + $user = null; |
| 182 | + foreach ($mountProviderArgs as $mountProviderArg) { |
| 183 | + if ($userId === null) { |
| 184 | + $user = $mountProviderArg->mountInfo->getUser(); |
| 185 | + $userId = $user->getUID(); |
| 186 | + } elseif ($userId !== $mountProviderArg->mountInfo->getUser()->getUID()) { |
| 187 | + throw new \LogicException('Mounts must belong to the same user!'); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + $this->userStoragesService->setUser($user); |
| 192 | + $this->userGlobalStoragesService->setUser($user); |
| 193 | + |
| 194 | + $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUserWithPath($path, $forChildren); |
| 195 | + |
| 196 | + $storages = array_map(function (StorageConfig $storageConfig) use ($user) { |
| 197 | + try { |
| 198 | + $this->prepareStorageConfig($storageConfig, $user); |
| 199 | + return $this->constructStorage($storageConfig); |
| 200 | + } catch (\Exception $e) { |
| 201 | + // propagate exception into filesystem |
| 202 | + return new FailedStorage(['exception' => $e]); |
| 203 | + } |
| 204 | + }, $storageConfigs); |
| 205 | + |
| 206 | + |
| 207 | + Storage::getGlobalCache()->loadForStorageIds(array_map(function (IStorage $storage) { |
| 208 | + return $storage->getId(); |
| 209 | + }, $storages)); |
| 210 | + |
| 211 | + $availableStorages = array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage { |
| 212 | + try { |
| 213 | + $availability = $storage->getAvailability(); |
| 214 | + if (!$availability['available'] && !Availability::shouldRecheck($availability)) { |
| 215 | + $storage = new FailedStorage([ |
| 216 | + 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available') |
| 217 | + ]); |
| 218 | + } |
| 219 | + } catch (\Exception $e) { |
| 220 | + // propagate exception into filesystem |
| 221 | + $storage = new FailedStorage(['exception' => $e]); |
| 222 | + } |
| 223 | + return $storage; |
| 224 | + }, $storages, $storageConfigs); |
| 225 | + |
| 226 | + $mounts = []; |
| 227 | + |
| 228 | + $i = 0; |
| 229 | + foreach ($storageConfigs as $storageConfig) { |
| 230 | + $storage = $availableStorages[$i]; |
| 231 | + $i++; |
| 232 | + $storage->setOwner($user->getUID()); |
| 233 | + $mountPoint = '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(); |
| 234 | + if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) { |
| 235 | + $mounts[$mountPoint] = new PersonalMount( |
| 236 | + $this->userStoragesService, |
| 237 | + $storageConfig, |
| 238 | + $storageConfig->getId(), |
| 239 | + new KnownMtime([ |
| 240 | + 'storage' => $storage, |
| 241 | + 'clock' => $this->clock, |
| 242 | + ]), |
| 243 | + $mountPoint, |
| 244 | + null, |
| 245 | + $loader, |
| 246 | + $storageConfig->getMountOptions(), |
| 247 | + $storageConfig->getId() |
| 248 | + ); |
| 249 | + } else { |
| 250 | + $mounts[$mountPoint] = new SystemMountPoint( |
| 251 | + $storageConfig, |
| 252 | + $storage, |
| 253 | + $mountPoint, |
| 254 | + null, |
| 255 | + $loader, |
| 256 | + $storageConfig->getMountOptions(), |
| 257 | + $storageConfig->getId() |
| 258 | + ); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + $this->userStoragesService->resetUser(); |
| 263 | + $this->userGlobalStoragesService->resetUser(); |
| 264 | + |
| 265 | + return $mounts; |
| 266 | + } |
171 | 267 | } |
0 commit comments