feat: use resource identifier

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-09-05 20:10:08 -04:00
parent 41de70da75
commit 26ed0c8aad
2 changed files with 24 additions and 17 deletions
+6 -5
View File
@@ -62,14 +62,14 @@ class TransferController extends ControllerAbstract
/** @var EntityBaseInterface[] $entities */ /** @var EntityBaseInterface[] $entities */
$entities = $this->manager->entityFetchBulk($tenantId, $userId, $target); $entities = $this->manager->entityFetchBulk($tenantId, $userId, $target);
if (empty($entities) || !isset($entities[$identifier])) { if (!isset($entities[(string) $target])) {
return new JsonResponse([ return new JsonResponse([
'status' => 'error', 'status' => 'error',
'error' => ['code' => 404, 'message' => 'File not found'] 'error' => ['code' => 404, 'message' => 'File not found']
], Response::HTTP_NOT_FOUND); ], Response::HTTP_NOT_FOUND);
} }
$entity = $entities[$identifier]; $entity = $entities[(string) $target];
// Get the stream // Get the stream
$stream = $this->manager->entityReadStream($tenantId, $userId, $target); $stream = $this->manager->entityReadStream($tenantId, $userId, $target);
@@ -327,15 +327,16 @@ class TransferController extends ControllerAbstract
foreach ($ids as $id) { foreach ($ids as $id) {
// Try as entity first // Try as entity first
if ($collection !== null) { if ($collection !== null) {
$target = new EntityIdentifier($provider, $service, $collection, (string) $id);
/** @var EntityBaseInterface[] $entities */ /** @var EntityBaseInterface[] $entities */
$entities = $this->manager->entityFetchBulk( $entities = $this->manager->entityFetchBulk(
$tenantId, $tenantId,
$userId, $userId,
new EntityIdentifier($provider, $service, $collection, (string)$id) $target
); );
if (!empty($entities) && isset($entities[$id])) { if (isset($entities[(string) $target])) {
$entity = $entities[$id]; $entity = $entities[(string) $target];
$files[] = [ $files[] = [
'type' => 'file', 'type' => 'file',
'id' => $id, 'id' => $id,
+18 -12
View File
@@ -25,6 +25,7 @@ use KTXF\Resource\Filter\IFilter;
use KTXF\Preview\PreviewSource; use KTXF\Preview\PreviewSource;
use KTXF\Resource\BinaryResource; use KTXF\Resource\BinaryResource;
use KTXF\Resource\Identifier\CollectionIdentifier; use KTXF\Resource\Identifier\CollectionIdentifier;
use KTXF\Resource\Identifier\ServiceIdentifier;
use KTXF\Resource\Identifier\EntityIdentifier; use KTXF\Resource\Identifier\EntityIdentifier;
use KTXF\Resource\Identifier\ResourceIdentifiers; use KTXF\Resource\Identifier\ResourceIdentifiers;
use KTXF\Resource\Provider\ResourceServiceIdentityInterface; use KTXF\Resource\Provider\ResourceServiceIdentityInterface;
@@ -507,18 +508,16 @@ class Manager {
* *
* @param string $tenantId tenant identifier * @param string $tenantId tenant identifier
* @param string $userId user identifier * @param string $userId user identifier
* @param string $provider provider identifier * @param ServiceIdentifier|CollectionIdentifier $target service root or parent collection
* @param string|int $service service identifier
* @param CollectionIdentifier|null $target target parent collection identifier (null for root)
* @param CollectionPropertiesMutableInterface|array $properties properties for the new collection * @param CollectionPropertiesMutableInterface|array $properties properties for the new collection
* @param array $options additional options for creation * @param array $options additional options for creation
* *
* @return CollectionBaseInterface * @return CollectionBaseInterface
* @throws InvalidArgumentException * @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 // retrieve service
$service = $this->serviceFetch($tenantId, $userId, $provider, $service); $service = $this->serviceFetch($tenantId, $userId, $target->provider(), $target->service());
// Check if service supports collection creation // Check if service supports collection creation
if ($service->getEnabled() === false) { if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled"); throw new InvalidArgumentException("Service '{$service->identifier()}' not found or is disabled");
@@ -534,7 +533,7 @@ class Manager {
$properties = $service->collectionFresh()->getProperties()->jsonDeserialize($properties); $properties = $service->collectionFresh()->getProperties()->jsonDeserialize($properties);
} }
// Create collection // 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<string,EntityBaseInterface> * @return array<string,EntityBaseInterface>
*/ */
public function entityFetchBulk(string $tenantId, ?string $userId, EntityIdentifier ...$identifiers): 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 = []; $groupedIdentifiers = [];
foreach ($identifiers as $identifier) { foreach ($identifiers as $identifier) {
$groupedIdentifiers[$identifier->provider()][$identifier->service()][] = $identifier; $groupedIdentifiers[$identifier->provider()][$identifier->service()][$identifier->collection()][] = $identifier;
} }
// retrieve each service and fetch entities // retrieve each service and fetch entities
$list = []; $list = [];
foreach ($groupedIdentifiers as $providerId => $services) { foreach ($groupedIdentifiers as $providerId => $services) {
foreach ($services as $serviceId => $entities) { foreach ($services as $serviceId => $collections) {
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId); $service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
if ($service->getEnabled() === false) { if ($service->getEnabled() === false) {
throw new InvalidArgumentException("Service '{$providerId}:{$serviceId}' not found or is disabled"); throw new InvalidArgumentException("Service '{$providerId}:{$serviceId}' not found or is disabled");
} }
// retrieve entities and merge into list foreach ($collections as $entities) {
$list = array_merge($list, $service->entityFetchBulk(...$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; return $list;
@@ -1354,7 +1360,7 @@ class Manager {
*/ */
public function entityPreview(string $tenantId, string $userId, EntityIdentifier $target, string $variant = 'inline'): ?Preview { public function entityPreview(string $tenantId, string $userId, EntityIdentifier $target, string $variant = 'inline'): ?Preview {
$entities = $this->entityFetchBulk($tenantId, $userId, $target); $entities = $this->entityFetchBulk($tenantId, $userId, $target);
$entity = $entities[$target->entity()] ?? reset($entities); $entity = $entities[(string) $target] ?? null;
if (!$entity instanceof EntityBaseInterface) { if (!$entity instanceof EntityBaseInterface) {
return null; return null;
} }