diff --git a/lib/Controllers/TransferController.php b/lib/Controllers/TransferController.php index a21f050..8388e47 100644 --- a/lib/Controllers/TransferController.php +++ b/lib/Controllers/TransferController.php @@ -62,14 +62,14 @@ class TransferController extends ControllerAbstract /** @var EntityBaseInterface[] $entities */ $entities = $this->manager->entityFetchBulk($tenantId, $userId, $target); - if (empty($entities) || !isset($entities[$identifier])) { + if (!isset($entities[(string) $target])) { return new JsonResponse([ 'status' => 'error', 'error' => ['code' => 404, 'message' => 'File not found'] ], Response::HTTP_NOT_FOUND); } - $entity = $entities[$identifier]; + $entity = $entities[(string) $target]; // Get the stream $stream = $this->manager->entityReadStream($tenantId, $userId, $target); @@ -327,15 +327,16 @@ class TransferController extends ControllerAbstract foreach ($ids as $id) { // Try as entity first if ($collection !== null) { + $target = new EntityIdentifier($provider, $service, $collection, (string) $id); /** @var EntityBaseInterface[] $entities */ $entities = $this->manager->entityFetchBulk( $tenantId, $userId, - new EntityIdentifier($provider, $service, $collection, (string)$id) + $target ); - if (!empty($entities) && isset($entities[$id])) { - $entity = $entities[$id]; + if (isset($entities[(string) $target])) { + $entity = $entities[(string) $target]; $files[] = [ 'type' => 'file', 'id' => $id, diff --git a/lib/Manager.php b/lib/Manager.php index 914e345..55bac33 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -25,6 +25,7 @@ use KTXF\Resource\Filter\IFilter; use KTXF\Preview\PreviewSource; use KTXF\Resource\BinaryResource; use KTXF\Resource\Identifier\CollectionIdentifier; +use KTXF\Resource\Identifier\ServiceIdentifier; use KTXF\Resource\Identifier\EntityIdentifier; use KTXF\Resource\Identifier\ResourceIdentifiers; use KTXF\Resource\Provider\ResourceServiceIdentityInterface; @@ -507,18 +508,16 @@ class Manager { * * @param string $tenantId tenant identifier * @param string $userId user identifier - * @param string $provider provider identifier - * @param string|int $service service identifier - * @param CollectionIdentifier|null $target target parent collection identifier (null for root) + * @param ServiceIdentifier|CollectionIdentifier $target service root or parent collection * @param CollectionPropertiesMutableInterface|array $properties properties for the new collection * @param array $options additional options for creation * * @return CollectionBaseInterface * @throws InvalidArgumentException */ - public function collectionCreate(string $tenantId, string $userId, string $provider, string|int $service, CollectionIdentifier|null $target, CollectionPropertiesMutableInterface|array $properties, array $options = []): CollectionBaseInterface { + public function collectionCreate(string $tenantId, string $userId, ServiceIdentifier|CollectionIdentifier $target, CollectionPropertiesMutableInterface|array $properties, array $options = []): CollectionBaseInterface { // retrieve service - $service = $this->serviceFetch($tenantId, $userId, $provider, $service); + $service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service()); // Check if service supports collection creation if ($service->getEnabled() === false) { throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled"); @@ -534,7 +533,7 @@ class Manager { $properties = $service->collectionFresh()->getProperties()->jsonDeserialize($properties); } // Create collection - return $service->collectionCreate($target, $properties, $options); + return $service->collectionCreate($target instanceof CollectionIdentifier ? $target : null, $properties, $options); } /** @@ -812,21 +811,28 @@ class Manager { * @return array */ public function entityFetchBulk(string $tenantId, ?string $userId, EntityIdentifier ...$identifiers): array { - // group identifiers by provider/service + // Keep locations separate: providers may return maps keyed by local ID. $groupedIdentifiers = []; foreach ($identifiers as $identifier) { - $groupedIdentifiers[$identifier->provider()][$identifier->service()][] = $identifier; + $groupedIdentifiers[$identifier->provider()][$identifier->service()][$identifier->collection()][] = $identifier; } // retrieve each service and fetch entities $list = []; foreach ($groupedIdentifiers as $providerId => $services) { - foreach ($services as $serviceId => $entities) { + foreach ($services as $serviceId => $collections) { $service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId); if ($service->getEnabled() === false) { throw new InvalidArgumentException("Service '{$providerId}:{$serviceId}' not found or is disabled"); } - // retrieve entities and merge into list - $list = array_merge($list, $service->entityFetchBulk(...$entities)); + foreach ($collections as $entities) { + $fetched = $service->entityFetchBulk(...$entities); + foreach ($entities as $identifier) { + $entity = $fetched[(string) $identifier] ?? $fetched[$identifier->entity()] ?? null; + if ($entity !== null) { + $list[(string) $identifier] = $entity; + } + } + } } } return $list; @@ -1354,7 +1360,7 @@ class Manager { */ public function entityPreview(string $tenantId, string $userId, EntityIdentifier $target, string $variant = 'inline'): ?Preview { $entities = $this->entityFetchBulk($tenantId, $userId, $target); - $entity = $entities[$target->entity()] ?? reset($entities); + $entity = $entities[(string) $target] ?? null; if (!$entity instanceof EntityBaseInterface) { return null; }