Initial commit
This commit is contained in:
720
lib/Controllers/DefaultController.php
Normal file
720
lib/Controllers/DefaultController.php
Normal file
@@ -0,0 +1,720 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileManager\Controllers;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use KTXC\Http\Response\JsonResponse;
|
||||
use KTXC\SessionIdentity;
|
||||
use KTXC\SessionTenant;
|
||||
use KTXF\Controller\ControllerAbstract;
|
||||
use KTXF\Resource\Selector\SourceSelector;
|
||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||
use KTXM\FileManager\Manager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
class DefaultController extends ControllerAbstract {
|
||||
|
||||
public function __construct(
|
||||
private readonly SessionTenant $tenantIdentity,
|
||||
private readonly SessionIdentity $userIdentity,
|
||||
private Manager $fileManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Main API endpoint for file operations
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
#[AuthenticatedRoute('/v1', name: 'filemanager.v1', methods: ['POST'])]
|
||||
|
||||
public function index(int $version, string $transaction, string $operation, array $data = [], string|null $user = null): JsonResponse {
|
||||
|
||||
// authorize request
|
||||
$tenantId = $this->tenantIdentity->identifier();
|
||||
$userId = $this->userIdentity->identifier();
|
||||
|
||||
try {
|
||||
$data = $this->process($tenantId, $userId, $operation, $data);
|
||||
return new JsonResponse([
|
||||
'version' => $version,
|
||||
'transaction' => $transaction,
|
||||
'operation' => $operation,
|
||||
'status' => 'success',
|
||||
'data' => $data
|
||||
], JsonResponse::HTTP_OK);
|
||||
} catch (Throwable $t) {
|
||||
$this->logger->error('Error processing file manager request', ['exception' => $t]);
|
||||
return new JsonResponse([
|
||||
'version' => $version,
|
||||
'transaction' => $transaction,
|
||||
'operation' => $operation,
|
||||
'status' => 'error',
|
||||
'error' => [
|
||||
'code' => $t->getCode(),
|
||||
'message' => $t->getMessage()
|
||||
]
|
||||
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
private function process(string $tenantId, string $userId, string $operation, array $data): mixed {
|
||||
|
||||
return match ($operation) {
|
||||
// Provider operations
|
||||
'provider.list' => $this->providerList($tenantId, $userId, $data),
|
||||
'provider.extant' => $this->providerExtant($tenantId, $userId, $data),
|
||||
// Service operations
|
||||
'service.list' => $this->serviceList($tenantId, $userId, $data),
|
||||
'service.extant' => $this->serviceExtant($tenantId, $userId, $data),
|
||||
'service.fetch' => $this->serviceFetch($tenantId, $userId, $data),
|
||||
// Collection operations
|
||||
'collection.list' => $this->collectionList($tenantId, $userId, $data),
|
||||
'collection.extant' => $this->collectionExtant($tenantId, $userId, $data),
|
||||
'collection.fetch' => $this->collectionFetch($tenantId, $userId, $data),
|
||||
'collection.create' => $this->collectionCreate($tenantId, $userId, $data),
|
||||
'collection.modify' => $this->collectionModify($tenantId, $userId, $data),
|
||||
'collection.destroy' => $this->collectionDestroy($tenantId, $userId, $data),
|
||||
'collection.copy' => $this->collectionCopy($tenantId, $userId, $data),
|
||||
'collection.move' => $this->collectionMove($tenantId, $userId, $data),
|
||||
// Entity operations
|
||||
'entity.list' => $this->entityList($tenantId, $userId, $data),
|
||||
'entity.delta' => $this->entityDelta($tenantId, $userId, $data),
|
||||
'entity.extant' => $this->entityExtant($tenantId, $userId, $data),
|
||||
'entity.fetch' => $this->entityFetch($tenantId, $userId, $data),
|
||||
'entity.read' => $this->entityRead($tenantId, $userId, $data),
|
||||
'entity.create' => $this->entityCreate($tenantId, $userId, $data),
|
||||
'entity.modify' => $this->entityModify($tenantId, $userId, $data),
|
||||
'entity.destroy' => $this->entityDestroy($tenantId, $userId, $data),
|
||||
'entity.copy' => $this->entityCopy($tenantId, $userId, $data),
|
||||
'entity.move' => $this->entityMove($tenantId, $userId, $data),
|
||||
'entity.write' => $this->entityWrite($tenantId, $userId, $data),
|
||||
// Node operations (unified recursive)
|
||||
'node.list' => $this->nodeList($tenantId, $userId, $data),
|
||||
'node.delta' => $this->nodeDelta($tenantId, $userId, $data),
|
||||
default => throw new InvalidArgumentException('Unknown operation: ' . $operation)
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// ==================== Provider Operations ====================
|
||||
|
||||
private function providerList(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
$sources = null;
|
||||
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
}
|
||||
return $this->fileManager->providerList($tenantId, $userId, $sources);
|
||||
|
||||
}
|
||||
|
||||
private function providerExtant(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['sources']) || !is_array($data['sources'])) {
|
||||
throw new InvalidArgumentException('Invalid sources selector provided');
|
||||
}
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
// retrieve providers
|
||||
return $this->fileManager->providerExtant($tenantId, $userId, $sources);
|
||||
|
||||
}
|
||||
|
||||
// ==================== Service Operations ====================
|
||||
|
||||
private function serviceList(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
$sources = null;
|
||||
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
}
|
||||
// retrieve services
|
||||
return $this->fileManager->serviceList($tenantId, $userId, $sources);
|
||||
|
||||
}
|
||||
|
||||
private function serviceExtant(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['sources']) || !is_array($data['sources'])) {
|
||||
throw new InvalidArgumentException('Invalid sources selector provided');
|
||||
}
|
||||
$sources = new SourceSelector();
|
||||
$sources->jsonDeserialize($data['sources']);
|
||||
// retrieve services
|
||||
return $this->fileManager->serviceExtant($tenantId, $userId, $sources);
|
||||
|
||||
}
|
||||
|
||||
private function serviceFetch(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Invalid provider identifier provided');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Invalid service identifier provided');
|
||||
}
|
||||
// retrieve service
|
||||
return $this->fileManager->serviceFetch($tenantId, $userId, $data['provider'], $data['identifier']);
|
||||
|
||||
}
|
||||
|
||||
// ==================== Collection Operations ====================
|
||||
|
||||
private function collectionList(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Invalid provider identifier provided');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Invalid service identifier provided');
|
||||
}
|
||||
|
||||
$provider = $data['provider'];
|
||||
$service = $data['service'];
|
||||
$location = $data['location'] ?? null;
|
||||
$filter = $data['filter'] ?? null;
|
||||
$sort = $data['sort'] ?? null;
|
||||
|
||||
return $this->fileManager->collectionList(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$location,
|
||||
$filter,
|
||||
$sort
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionExtant(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
return [
|
||||
'extant' => $this->fileManager->collectionExtant(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier']
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
private function collectionFetch(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
return $this->fileManager->collectionFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier']
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionCreate(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['data'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: data');
|
||||
}
|
||||
|
||||
$location = $data['location'] ?? null;
|
||||
$options = $data['options'] ?? [];
|
||||
|
||||
return $this->fileManager->collectionCreate(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$location,
|
||||
$data['data'],
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionModify(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
if (!isset($data['data'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: data');
|
||||
}
|
||||
|
||||
return $this->fileManager->collectionModify(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier'],
|
||||
$data['data']
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionDestroy(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => $this->fileManager->collectionDestroy(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier']
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
private function collectionCopy(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$location = $data['location'] ?? null;
|
||||
|
||||
return $this->fileManager->collectionCopy(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier'],
|
||||
$location
|
||||
);
|
||||
}
|
||||
|
||||
private function collectionMove(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$location = $data['location'] ?? null;
|
||||
|
||||
return $this->fileManager->collectionMove(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['identifier'],
|
||||
$location
|
||||
);
|
||||
}
|
||||
|
||||
// ==================== Entity Operations ====================
|
||||
|
||||
private function entityList(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Invalid provider identifier provided');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Invalid service identifier provided');
|
||||
}
|
||||
if (!isset($data['collection'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: collection');
|
||||
}
|
||||
|
||||
$provider = $data['provider'];
|
||||
$service = $data['service'];
|
||||
$collection = $data['collection'];
|
||||
$filter = $data['filter'] ?? null;
|
||||
$sort = $data['sort'] ?? null;
|
||||
$range = $data['range'] ?? null;
|
||||
|
||||
return $this->fileManager->entityList(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collection,
|
||||
$filter,
|
||||
$sort,
|
||||
$range
|
||||
);
|
||||
}
|
||||
|
||||
private function entityDelta(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['collection'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: collection');
|
||||
}
|
||||
if (!isset($data['signature']) || !is_string($data['signature'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: signature');
|
||||
}
|
||||
|
||||
$detail = $data['detail'] ?? 'ids';
|
||||
|
||||
return $this->fileManager->entityDelta(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'],
|
||||
$data['signature'],
|
||||
$detail
|
||||
);
|
||||
}
|
||||
|
||||
private function entityExtant(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['collection'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: collection');
|
||||
}
|
||||
if (!isset($data['identifiers']) || !is_array($data['identifiers'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifiers');
|
||||
}
|
||||
|
||||
return $this->fileManager->entityExtant(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'],
|
||||
$data['identifiers']
|
||||
);
|
||||
}
|
||||
|
||||
private function entityFetch(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['collection'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: collection');
|
||||
}
|
||||
if (!isset($data['identifiers']) || !is_array($data['identifiers'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifiers');
|
||||
}
|
||||
|
||||
return $this->fileManager->entityFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'],
|
||||
$data['identifiers']
|
||||
);
|
||||
}
|
||||
|
||||
private function entityRead(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['collection'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: collection');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$content = $this->fileManager->entityRead(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'],
|
||||
$data['identifier']
|
||||
);
|
||||
|
||||
return [
|
||||
'content' => $content !== null ? base64_encode($content) : null,
|
||||
'encoding' => 'base64'
|
||||
];
|
||||
}
|
||||
|
||||
private function entityCreate(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['data'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: data');
|
||||
}
|
||||
|
||||
$collection = $data['collection'] ?? null;
|
||||
$options = $data['options'] ?? [];
|
||||
|
||||
return $this->fileManager->entityCreate(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$collection,
|
||||
$data['data'],
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
private function entityModify(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
if (!isset($data['data'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: data');
|
||||
}
|
||||
|
||||
$collection = $data['collection'] ?? null;
|
||||
|
||||
return $this->fileManager->entityModify(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$collection,
|
||||
$data['identifier'],
|
||||
$data['data']
|
||||
);
|
||||
}
|
||||
|
||||
private function entityDestroy(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$collection = $data['collection'] ?? null;
|
||||
|
||||
return [
|
||||
'success' => $this->fileManager->entityDestroy(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$collection,
|
||||
$data['identifier']
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
private function entityCopy(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$collection = $data['collection'] ?? null;
|
||||
$destination = $data['destination'] ?? null;
|
||||
|
||||
return $this->fileManager->entityCopy(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$collection,
|
||||
$data['identifier'],
|
||||
$destination
|
||||
);
|
||||
}
|
||||
|
||||
private function entityMove(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
|
||||
$collection = $data['collection'] ?? null;
|
||||
$destination = $data['destination'] ?? null;
|
||||
|
||||
return $this->fileManager->entityMove(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$collection,
|
||||
$data['identifier'],
|
||||
$destination
|
||||
);
|
||||
}
|
||||
|
||||
private function entityWrite(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['identifier'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: identifier');
|
||||
}
|
||||
if (!isset($data['content'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: content');
|
||||
}
|
||||
|
||||
// Decode content if base64 encoded
|
||||
$content = $data['content'];
|
||||
if (isset($data['encoding']) && $data['encoding'] === 'base64') {
|
||||
$content = base64_decode($content);
|
||||
if ($content === false) {
|
||||
throw new InvalidArgumentException('Invalid base64 encoded content');
|
||||
}
|
||||
}
|
||||
|
||||
$bytesWritten = $this->fileManager->entityWrite(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$data['collection'],
|
||||
$data['identifier'],
|
||||
$content
|
||||
);
|
||||
|
||||
return [
|
||||
'bytesWritten' => $bytesWritten
|
||||
];
|
||||
}
|
||||
|
||||
// ==================== Node Operations (Unified/Recursive) ====================
|
||||
|
||||
private function nodeList(string $tenantId, string $userId, array $data = []): mixed {
|
||||
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Invalid provider identifier provided');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Invalid service identifier provided');
|
||||
}
|
||||
|
||||
$provider = $data['provider'];
|
||||
$service = $data['service'];
|
||||
$location = $data['location'] ?? null;
|
||||
$recursive = $data['recursive'] ?? false;
|
||||
$filter = $data['filter'] ?? null;
|
||||
$sort = $data['sort'] ?? null;
|
||||
$range = $data['range'] ?? null;
|
||||
|
||||
return $this->fileManager->nodeList(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$location,
|
||||
$recursive,
|
||||
$filter,
|
||||
$sort,
|
||||
$range
|
||||
);
|
||||
}
|
||||
|
||||
private function nodeDelta(string $tenantId, string $userId, array $data = []): mixed {
|
||||
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: provider');
|
||||
}
|
||||
if (!isset($data['service'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: service');
|
||||
}
|
||||
if (!isset($data['signature']) || !is_string($data['signature'])) {
|
||||
throw new InvalidArgumentException('Missing required parameter: signature');
|
||||
}
|
||||
|
||||
$location = $data['location'] ?? null;
|
||||
$recursive = $data['recursive'] ?? false;
|
||||
$detail = $data['detail'] ?? 'ids';
|
||||
|
||||
return $this->fileManager->nodeDelta(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$data['provider'],
|
||||
$data['service'],
|
||||
$location,
|
||||
$data['signature'],
|
||||
$recursive,
|
||||
$detail
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
488
lib/Controllers/TransferController.php
Normal file
488
lib/Controllers/TransferController.php
Normal file
@@ -0,0 +1,488 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileManager\Controllers;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use KTXC\Http\Response\JsonResponse;
|
||||
use KTXC\Http\Response\Response;
|
||||
use KTXC\Http\Response\StreamedResponse;
|
||||
use KTXC\SessionIdentity;
|
||||
use KTXC\SessionTenant;
|
||||
use KTXF\Controller\ControllerAbstract;
|
||||
use KTXF\Files\Node\INodeCollectionBase;
|
||||
use KTXF\Files\Node\INodeEntityBase;
|
||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||
use KTXM\FileManager\Manager;
|
||||
use KTXM\FileManager\Transfer\StreamingZip;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Controller for file transfers (downloads and uploads)
|
||||
*
|
||||
* Handles binary file transfers that don't fit the JSON API pattern:
|
||||
* - Single file downloads (streamed)
|
||||
* - Multi-file downloads as ZIP (streamed)
|
||||
* - Folder downloads as ZIP with structure preserved (streamed)
|
||||
* - Large file uploads (future)
|
||||
* - ZIP file uploads with extraction (future)
|
||||
*/
|
||||
class TransferController extends ControllerAbstract
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SessionTenant $tenantIdentity,
|
||||
private readonly SessionIdentity $userIdentity,
|
||||
private Manager $fileManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Download a single file
|
||||
*
|
||||
* GET /download/entity/{provider}/{service}/{collection}/{identifier}
|
||||
*/
|
||||
#[AuthenticatedRoute(
|
||||
'/download/entity/{provider}/{service}/{collection}/{identifier}',
|
||||
name: 'filemanager.download.entity',
|
||||
methods: ['GET']
|
||||
)]
|
||||
public function downloadEntity(
|
||||
string $provider,
|
||||
string $service,
|
||||
string $collection,
|
||||
string $identifier
|
||||
): Response {
|
||||
$tenantId = $this->tenantIdentity->identifier();
|
||||
$userId = $this->userIdentity->identifier();
|
||||
|
||||
try {
|
||||
// Fetch entity metadata
|
||||
$entities = $this->fileManager->entityFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collection,
|
||||
[$identifier]
|
||||
);
|
||||
|
||||
if (empty($entities) || !isset($entities[$identifier])) {
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => 404, 'message' => 'File not found']
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
/** @var INodeEntityBase $entity */
|
||||
$entity = $entities[$identifier];
|
||||
|
||||
// Get the stream
|
||||
$stream = $this->fileManager->entityReadStream(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collection,
|
||||
$identifier
|
||||
);
|
||||
|
||||
if ($stream === null) {
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => 404, 'message' => 'File content not available']
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$filename = $entity->getLabel() ?? 'download';
|
||||
$mime = $entity->getMime() ?? 'application/octet-stream';
|
||||
$size = $entity->size();
|
||||
|
||||
// Create streamed response
|
||||
$response = new StreamedResponse(function () use ($stream) {
|
||||
while (!feof($stream)) {
|
||||
echo fread($stream, 65536);
|
||||
@ob_flush();
|
||||
flush();
|
||||
}
|
||||
fclose($stream);
|
||||
});
|
||||
|
||||
$response->headers->set('Content-Type', $mime);
|
||||
$response->headers->set('Content-Length', (string) $size);
|
||||
$response->headers->set('Content-Disposition',
|
||||
$response->headers->makeDisposition('attachment', $filename, $this->asciiFallback($filename))
|
||||
);
|
||||
$response->headers->set('Cache-Control', 'private, no-cache');
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (Throwable $t) {
|
||||
$this->logger->error('Download failed', ['exception' => $t]);
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => $t->getCode(), 'message' => $t->getMessage()]
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download multiple files as a ZIP archive
|
||||
*
|
||||
* GET /download/archive?provider=...&service=...&ids[]=...&ids[]=...
|
||||
*/
|
||||
#[AuthenticatedRoute(
|
||||
'/download/archive',
|
||||
name: 'filemanager.download.archive',
|
||||
methods: ['GET']
|
||||
)]
|
||||
public function downloadArchive(
|
||||
string $provider,
|
||||
string $service,
|
||||
array $ids = [],
|
||||
string $collection = null,
|
||||
string $name = 'download'
|
||||
): Response {
|
||||
$tenantId = $this->tenantIdentity->identifier();
|
||||
$userId = $this->userIdentity->identifier();
|
||||
|
||||
if (empty($ids)) {
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => 400, 'message' => 'No file IDs provided']
|
||||
], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
// Build list of files to include
|
||||
$files = $this->resolveFilesForArchive(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collection,
|
||||
$ids
|
||||
);
|
||||
|
||||
if (empty($files)) {
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => 404, 'message' => 'No files found']
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$archiveName = $this->sanitizeFilename($name) . '.zip';
|
||||
|
||||
// Create streamed ZIP response
|
||||
$response = new StreamedResponse(function () use ($files, $tenantId, $userId, $provider, $service) {
|
||||
$zip = new StreamingZip(null, false); // No compression for speed
|
||||
|
||||
foreach ($files as $file) {
|
||||
$stream = $this->fileManager->entityReadStream(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$file['collection'],
|
||||
$file['id']
|
||||
);
|
||||
|
||||
if ($stream !== null) {
|
||||
$zip->addFileFromStream(
|
||||
$file['path'],
|
||||
$stream,
|
||||
$file['modTime'] ?? null
|
||||
);
|
||||
fclose($stream);
|
||||
}
|
||||
}
|
||||
|
||||
$zip->finish();
|
||||
});
|
||||
|
||||
$response->headers->set('Content-Type', 'application/zip');
|
||||
$response->headers->set('Content-Disposition',
|
||||
$response->headers->makeDisposition('attachment', $archiveName, $this->asciiFallback($archiveName))
|
||||
);
|
||||
$response->headers->set('Cache-Control', 'private, no-cache');
|
||||
// No Content-Length since we're streaming
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (Throwable $t) {
|
||||
$this->logger->error('Archive download failed', ['exception' => $t]);
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => $t->getCode(), 'message' => $t->getMessage()]
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a collection (folder) as a ZIP archive with structure preserved
|
||||
*
|
||||
* GET /download/collection/{provider}/{service}/{identifier}
|
||||
*/
|
||||
#[AuthenticatedRoute(
|
||||
'/download/collection/{provider}/{service}/{identifier}',
|
||||
name: 'filemanager.download.collection',
|
||||
methods: ['GET']
|
||||
)]
|
||||
public function downloadCollection(
|
||||
string $provider,
|
||||
string $service,
|
||||
string $identifier
|
||||
): Response {
|
||||
$tenantId = $this->tenantIdentity->identifier();
|
||||
$userId = $this->userIdentity->identifier();
|
||||
|
||||
try {
|
||||
// Fetch collection metadata
|
||||
$collection = $this->fileManager->collectionFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$identifier
|
||||
);
|
||||
|
||||
if ($collection === null) {
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => 404, 'message' => 'Folder not found']
|
||||
], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$folderName = $collection->getLabel() ?? 'folder';
|
||||
$archiveName = $this->sanitizeFilename($folderName) . '.zip';
|
||||
|
||||
// Build recursive file list
|
||||
$files = $this->resolveCollectionContents(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$identifier,
|
||||
'' // Base path (root of archive)
|
||||
);
|
||||
|
||||
// Create streamed ZIP response
|
||||
$response = new StreamedResponse(function () use ($files, $tenantId, $userId, $provider, $service) {
|
||||
$zip = new StreamingZip(null, false);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file['type'] === 'directory') {
|
||||
$zip->addDirectory($file['path'], $file['modTime'] ?? null);
|
||||
} else {
|
||||
$stream = $this->fileManager->entityReadStream(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$file['collection'],
|
||||
$file['id']
|
||||
);
|
||||
|
||||
if ($stream !== null) {
|
||||
$zip->addFileFromStream(
|
||||
$file['path'],
|
||||
$stream,
|
||||
$file['modTime'] ?? null
|
||||
);
|
||||
fclose($stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$zip->finish();
|
||||
});
|
||||
|
||||
$response->headers->set('Content-Type', 'application/zip');
|
||||
$response->headers->set('Content-Disposition',
|
||||
$response->headers->makeDisposition('attachment', $archiveName, $this->asciiFallback($archiveName))
|
||||
);
|
||||
$response->headers->set('Cache-Control', 'private, no-cache');
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (Throwable $t) {
|
||||
$this->logger->error('Collection download failed', ['exception' => $t]);
|
||||
return new JsonResponse([
|
||||
'status' => 'error',
|
||||
'error' => ['code' => $t->getCode(), 'message' => $t->getMessage()]
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a list of entity/collection IDs into a flat file list for archiving
|
||||
*/
|
||||
private function resolveFilesForArchive(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $provider,
|
||||
string $service,
|
||||
?string $collection,
|
||||
array $ids
|
||||
): array {
|
||||
$files = [];
|
||||
|
||||
foreach ($ids as $id) {
|
||||
// Try as entity first
|
||||
if ($collection !== null) {
|
||||
$entities = $this->fileManager->entityFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collection,
|
||||
[$id]
|
||||
);
|
||||
|
||||
if (!empty($entities) && isset($entities[$id])) {
|
||||
/** @var INodeEntityBase $entity */
|
||||
$entity = $entities[$id];
|
||||
$files[] = [
|
||||
'type' => 'file',
|
||||
'id' => $id,
|
||||
'collection' => $collection,
|
||||
'path' => $entity->getLabel() ?? $id,
|
||||
'modTime' => $entity->modifiedOn()?->getTimestamp(),
|
||||
];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Try as collection (folder)
|
||||
$collectionNode = $this->fileManager->collectionFetch(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$id
|
||||
);
|
||||
|
||||
if ($collectionNode !== null) {
|
||||
$folderName = $collectionNode->getLabel() ?? $id;
|
||||
$subFiles = $this->resolveCollectionContents(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$id,
|
||||
$folderName
|
||||
);
|
||||
$files = array_merge($files, $subFiles);
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively resolve all contents of a collection into a flat file list
|
||||
*/
|
||||
private function resolveCollectionContents(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $provider,
|
||||
string $service,
|
||||
string $collectionId,
|
||||
string $basePath
|
||||
): array {
|
||||
$files = [];
|
||||
|
||||
// Add directory entry if we have a path
|
||||
if ($basePath !== '') {
|
||||
$files[] = [
|
||||
'type' => 'directory',
|
||||
'path' => $basePath,
|
||||
'modTime' => null,
|
||||
];
|
||||
}
|
||||
|
||||
// Get all nodes in this collection using nodeList with recursive=false
|
||||
// We handle recursion ourselves to build proper paths
|
||||
try {
|
||||
$nodes = $this->fileManager->nodeList(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
$collectionId,
|
||||
false // Not recursive - we handle it ourselves
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->warning('Failed to list collection contents', [
|
||||
'collection' => $collectionId,
|
||||
'exception' => $e
|
||||
]);
|
||||
return $files;
|
||||
}
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
$nodeName = $node->getLabel() ?? (string) $node->id();
|
||||
$nodePath = $basePath !== '' ? $basePath . '/' . $nodeName : $nodeName;
|
||||
|
||||
if ($node->isCollection()) {
|
||||
// Recursively get contents of sub-collection
|
||||
$subFiles = $this->resolveCollectionContents(
|
||||
$tenantId,
|
||||
$userId,
|
||||
$provider,
|
||||
$service,
|
||||
(string) $node->id(),
|
||||
$nodePath
|
||||
);
|
||||
$files = array_merge($files, $subFiles);
|
||||
} else {
|
||||
// It's an entity (file)
|
||||
/** @var INodeEntityBase $node */
|
||||
$files[] = [
|
||||
'type' => 'file',
|
||||
'id' => (string) $node->id(),
|
||||
'collection' => $collectionId,
|
||||
'path' => $nodePath,
|
||||
'modTime' => $node->modifiedOn()?->getTimestamp(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a filename for use in Content-Disposition header
|
||||
*/
|
||||
private function sanitizeFilename(string $filename): string
|
||||
{
|
||||
// Remove or replace problematic characters
|
||||
$filename = preg_replace('/[<>:"\/\\|?*\x00-\x1F]/', '_', $filename);
|
||||
$filename = trim($filename, '. ');
|
||||
|
||||
if ($filename === '') {
|
||||
$filename = 'download';
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ASCII-safe fallback filename
|
||||
*/
|
||||
private function asciiFallback(string $filename): string
|
||||
{
|
||||
// Transliterate to ASCII
|
||||
$fallback = transliterator_transliterate('Any-Latin; Latin-ASCII', $filename);
|
||||
if ($fallback === false) {
|
||||
$fallback = preg_replace('/[^\x20-\x7E]/', '_', $filename);
|
||||
}
|
||||
return $this->sanitizeFilename($fallback);
|
||||
}
|
||||
}
|
||||
748
lib/Manager.php
Normal file
748
lib/Manager.php
Normal file
@@ -0,0 +1,748 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXM\FileManager;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use KTXC\Resource\ProviderManager;
|
||||
use KTXF\Files\Node\INodeBase;
|
||||
use KTXF\Files\Node\INodeCollectionBase;
|
||||
use KTXF\Files\Node\INodeCollectionMutable;
|
||||
use KTXF\Files\Node\INodeEntityBase;
|
||||
use KTXF\Files\Node\INodeEntityMutable;
|
||||
use KTXF\Files\Provider\IProviderBase;
|
||||
use KTXF\Files\Service\IServiceBase;
|
||||
use KTXF\Files\Service\IServiceCollectionMutable;
|
||||
use KTXF\Files\Service\IServiceEntityMutable;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXF\Resource\Range\IRangeTally;
|
||||
use KTXF\Resource\Range\RangeAnchorType;
|
||||
use KTXF\Resource\Range\RangeType;
|
||||
use KTXF\Resource\Selector\ServiceSelector;
|
||||
use KTXF\Resource\Selector\SourceSelector;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Manager {
|
||||
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private ProviderManager $providerManager,
|
||||
) { }
|
||||
|
||||
// ==================== Provider Operations ====================
|
||||
|
||||
/**
|
||||
* Retrieve available providers
|
||||
*
|
||||
* @param SourceSelector|null $sources collection of provider identifiers
|
||||
*
|
||||
* @return array<string,IProviderBase> collection of available providers
|
||||
*/
|
||||
public function providerList(string $tenantId, string $userId, ?SourceSelector $sources = null): array {
|
||||
// determine filter from sources
|
||||
$filter = ($sources !== null && $sources->identifiers() !== []) ? $sources->identifiers() : null;
|
||||
// retrieve providers from provider manager
|
||||
return $this->providerManager->providers(ProviderInterface::TYPE_FILES, $filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm which providers are available
|
||||
*
|
||||
* @param SourceSelector $sources collection of provider identifiers to confirm
|
||||
*
|
||||
* @return array<string,bool> collection of providers and their availability status
|
||||
*/
|
||||
public function providerExtant(string $tenantId, string $userId, SourceSelector $sources): array {
|
||||
$providerFilter = $sources?->identifiers() ?? [];
|
||||
$providersResolved = $this->providerManager->providers(ProviderInterface::TYPE_FILES, $providerFilter);
|
||||
$providersAvailable = array_keys($providersResolved);
|
||||
$providersUnavailable = array_diff($providerFilter, $providersAvailable);
|
||||
$responseData = array_merge(
|
||||
array_fill_keys($providersAvailable, true),
|
||||
array_fill_keys($providersUnavailable, false)
|
||||
);
|
||||
return $responseData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve specific provider for specific user
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param string $provider provider identifier
|
||||
*
|
||||
* @return IProviderBase
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function providerFetch(string $tenantId, string $userId, string $provider): IProviderBase {
|
||||
// retrieve provider
|
||||
$providers = $this->providerList($tenantId, $userId, new SourceSelector([$provider => true]));
|
||||
if (!isset($providers[$provider])) {
|
||||
throw new InvalidArgumentException("Provider '$provider' not found");
|
||||
}
|
||||
return $providers[$provider];
|
||||
}
|
||||
|
||||
// ==================== Service Operations ====================
|
||||
|
||||
/**
|
||||
* Retrieve available services for specific user
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param SourceSelector|null $sources list of provider and service identifiers
|
||||
*
|
||||
* @return array<string,array<string,IServiceBase>> collections of available services
|
||||
*/
|
||||
public function serviceList(string $tenantId, string $userId, ?SourceSelector $sources = null): array {
|
||||
// retrieve providers
|
||||
$providers = $this->providerList($tenantId, $userId, $sources);
|
||||
// retrieve services for each provider
|
||||
$responseData = [];
|
||||
foreach ($providers as $provider) {
|
||||
$serviceFilter = $sources[$provider->id()] instanceof ServiceSelector ? $sources[$provider->id()]->identifiers() : [];
|
||||
$services = $provider->serviceList($tenantId, $userId, $serviceFilter);
|
||||
$responseData[$provider->id()] = $services;
|
||||
}
|
||||
return $responseData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm which services are available
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param SourceSelector|null $sources collection of provider and service identifiers to confirm
|
||||
*
|
||||
* @return array<string,bool> collection of providers and their availability status
|
||||
*/
|
||||
public function serviceExtant(string $tenantId, string $userId, SourceSelector $sources): array {
|
||||
// retrieve providers
|
||||
$providers = $this->providerList($tenantId, $userId, $sources);
|
||||
$providersRequested = $sources->identifiers();
|
||||
$providersUnavailable = array_diff($providersRequested, array_keys($providers));
|
||||
|
||||
// initialize response with unavailable providers
|
||||
$responseData = array_fill_keys($providersUnavailable, false);
|
||||
|
||||
// retrieve services for each available provider
|
||||
foreach ($providers as $provider) {
|
||||
$serviceSelector = $sources[$provider->id()];
|
||||
$serviceAvailability = $provider->serviceExtant($tenantId, $userId, ...$serviceSelector->identifiers());
|
||||
$responseData[$provider->id()] = $serviceAvailability;
|
||||
}
|
||||
return $responseData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve service for specific user
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param string $providerId provider identifier
|
||||
* @param string|int $serviceId service identifier
|
||||
*
|
||||
* @return IServiceBase
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function serviceFetch(string $tenantId, string $userId, string $providerId, string|int $serviceId): IServiceBase {
|
||||
// retrieve provider and service
|
||||
$service = $this->providerFetch($tenantId, $userId, $providerId)->serviceFetch($tenantId, $userId, $serviceId);
|
||||
if ($service === null) {
|
||||
throw new InvalidArgumentException("Service '$serviceId' not found for provider '$providerId'");
|
||||
}
|
||||
return $service;
|
||||
}
|
||||
|
||||
// ==================== Collection Operations ====================
|
||||
|
||||
/**
|
||||
* List collections at a location
|
||||
*
|
||||
* @return array<string|int,INodeCollectionBase>
|
||||
*/
|
||||
public function collectionList(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $location = null,
|
||||
?array $filter = null,
|
||||
?array $sort = null
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
// construct filter
|
||||
$collectionFilter = null;
|
||||
if ($filter !== null && $filter !== []) {
|
||||
$collectionFilter = $service->collectionListFilter();
|
||||
foreach ($filter as $attribute => $value) {
|
||||
$collectionFilter->condition($attribute, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// construct sort
|
||||
$collectionSort = null;
|
||||
if ($sort !== null && $sort !== []) {
|
||||
$collectionSort = $service->collectionListSort();
|
||||
foreach ($sort as $attribute => $direction) {
|
||||
$collectionSort->condition($attribute, $direction);
|
||||
}
|
||||
}
|
||||
|
||||
return $service->collectionList($location, $collectionFilter, $collectionSort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if collection exists
|
||||
*/
|
||||
public function collectionExtant(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier
|
||||
): bool {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->collectionExtant($identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific collection
|
||||
*/
|
||||
public function collectionFetch(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier
|
||||
): ?INodeCollectionBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->collectionFetch($identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection
|
||||
*/
|
||||
public function collectionCreate(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $location,
|
||||
INodeCollectionMutable|array $collection,
|
||||
array $options = []
|
||||
): INodeCollectionBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceCollectionMutable) {
|
||||
throw new InvalidArgumentException('Service does not support collection creation');
|
||||
}
|
||||
|
||||
if (is_array($collection)) {
|
||||
$collectionObject = $service->collectionFresh();
|
||||
$collectionObject->jsonDeserialize($collection);
|
||||
$collection = $collectionObject;
|
||||
}
|
||||
|
||||
return $service->collectionCreate($location, $collection, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an existing collection
|
||||
*/
|
||||
public function collectionModify(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier,
|
||||
INodeCollectionMutable|array $collection
|
||||
): INodeCollectionBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceCollectionMutable) {
|
||||
throw new InvalidArgumentException('Service does not support collection modification');
|
||||
}
|
||||
|
||||
if (is_array($collection)) {
|
||||
$collectionObject = $service->collectionFresh();
|
||||
$collectionObject->jsonDeserialize($collection);
|
||||
$collection = $collectionObject;
|
||||
}
|
||||
|
||||
return $service->collectionModify($identifier, $collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy a collection
|
||||
*/
|
||||
public function collectionDestroy(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier
|
||||
): bool {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceCollectionMutable) {
|
||||
throw new InvalidArgumentException('Service does not support collection deletion');
|
||||
}
|
||||
|
||||
return $service->collectionDestroy($identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a collection
|
||||
*/
|
||||
public function collectionCopy(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier,
|
||||
string|int|null $location
|
||||
): INodeCollectionBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceCollectionMutable) {
|
||||
throw new InvalidArgumentException('Service does not support collection copy');
|
||||
}
|
||||
|
||||
return $service->collectionCopy($identifier, $location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a collection
|
||||
*/
|
||||
public function collectionMove(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $identifier,
|
||||
string|int|null $location
|
||||
): INodeCollectionBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceCollectionMutable) {
|
||||
throw new InvalidArgumentException('Service does not support collection move');
|
||||
}
|
||||
|
||||
return $service->collectionMove($identifier, $location);
|
||||
}
|
||||
|
||||
// ==================== Entity Operations ====================
|
||||
|
||||
/**
|
||||
* List entities in a collection
|
||||
*
|
||||
* @return array<string|int,INodeEntityBase>
|
||||
*/
|
||||
public function entityList(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
?array $filter = null,
|
||||
?array $sort = null,
|
||||
?array $range = null
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
// construct filter
|
||||
$entityFilter = null;
|
||||
if ($filter !== null && $filter !== []) {
|
||||
$entityFilter = $service->entityListFilter();
|
||||
foreach ($filter as $attribute => $value) {
|
||||
$entityFilter->condition($attribute, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// construct sort
|
||||
$entitySort = null;
|
||||
if ($sort !== null && $sort !== []) {
|
||||
$entitySort = $service->entityListSort();
|
||||
foreach ($sort as $attribute => $direction) {
|
||||
$entitySort->condition($attribute, $direction);
|
||||
}
|
||||
}
|
||||
|
||||
// construct range
|
||||
$entityRange = null;
|
||||
if ($range !== null && $range !== [] && isset($range['type'])) {
|
||||
$entityRange = $service->entityListRange(RangeType::from($range['type']));
|
||||
if ($entityRange instanceof IRangeTally) {
|
||||
if (isset($range['anchor'])) {
|
||||
$entityRange->setAnchor(RangeAnchorType::from($range['anchor']));
|
||||
}
|
||||
if (isset($range['position'])) {
|
||||
$entityRange->setPosition($range['position']);
|
||||
}
|
||||
if (isset($range['tally'])) {
|
||||
$entityRange->setTally($range['tally']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $service->entityList($collection, $entityFilter, $entitySort, $entityRange);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity delta/changes since a signature
|
||||
*/
|
||||
public function entityDelta(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string $signature,
|
||||
string $detail = 'ids'
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityDelta($collection, $signature, $detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if entities exist
|
||||
*
|
||||
* @return array<string|int,bool>
|
||||
*/
|
||||
public function entityExtant(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
array $identifiers
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityExtant($collection, ...$identifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch specific entities
|
||||
*
|
||||
* @return array<string|int,INodeEntityBase>
|
||||
*/
|
||||
public function entityFetch(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
array $identifiers
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityFetch($collection, ...$identifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read entity content
|
||||
*/
|
||||
public function entityRead(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string|int $identifier
|
||||
): ?string {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityRead($collection, $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read entity content as stream
|
||||
*
|
||||
* @return resource|null
|
||||
*/
|
||||
public function entityReadStream(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string|int $identifier
|
||||
) {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityReadStream($collection, $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read entity content chunk
|
||||
*/
|
||||
public function entityReadChunk(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string|int $identifier,
|
||||
int $offset,
|
||||
int $length
|
||||
): ?string {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->entityReadChunk($collection, $identifier, $offset, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new entity
|
||||
*/
|
||||
public function entityCreate(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
INodeEntityMutable|array $entity,
|
||||
array $options = []
|
||||
): INodeEntityBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity creation');
|
||||
}
|
||||
|
||||
if (is_array($entity)) {
|
||||
$entityObject = $service->entityFresh();
|
||||
$entityObject->jsonDeserialize($entity);
|
||||
$entity = $entityObject;
|
||||
}
|
||||
|
||||
return $service->entityCreate($collection, $entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an existing entity
|
||||
*/
|
||||
public function entityModify(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
string|int $identifier,
|
||||
INodeEntityMutable|array $entity
|
||||
): INodeEntityBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity modification');
|
||||
}
|
||||
|
||||
if (is_array($entity)) {
|
||||
$entityObject = $service->entityFresh();
|
||||
$entityObject->jsonDeserialize($entity);
|
||||
$entity = $entityObject;
|
||||
}
|
||||
|
||||
return $service->entityModify($collection, $identifier, $entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy an entity
|
||||
*/
|
||||
public function entityDestroy(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
string|int $identifier
|
||||
): bool {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity deletion');
|
||||
}
|
||||
|
||||
return $service->entityDestroy($collection, $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy an entity
|
||||
*/
|
||||
public function entityCopy(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
string|int $identifier,
|
||||
string|int|null $destination
|
||||
): INodeEntityBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity copy');
|
||||
}
|
||||
|
||||
return $service->entityCopy($collection, $identifier, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move an entity
|
||||
*/
|
||||
public function entityMove(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
string|int $identifier,
|
||||
string|int|null $destination
|
||||
): INodeEntityBase {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity move');
|
||||
}
|
||||
|
||||
return $service->entityMove($collection, $identifier, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write entity content
|
||||
*/
|
||||
public function entityWrite(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $collection,
|
||||
string|int $identifier,
|
||||
string $data
|
||||
): int {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity write');
|
||||
}
|
||||
|
||||
return $service->entityWrite($collection, $identifier, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write entity content from stream
|
||||
*
|
||||
* @return resource|null
|
||||
*/
|
||||
public function entityWriteStream(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string|int $identifier
|
||||
) {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity write stream');
|
||||
}
|
||||
|
||||
return $service->entityWriteStream($collection, $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write entity content chunk
|
||||
*/
|
||||
public function entityWriteChunk(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int $collection,
|
||||
string|int $identifier,
|
||||
int $offset,
|
||||
string $data
|
||||
): int {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
if (!$service instanceof IServiceEntityMutable) {
|
||||
throw new InvalidArgumentException('Service does not support entity write chunk');
|
||||
}
|
||||
|
||||
return $service->entityWriteChunk($collection, $identifier, $offset, $data);
|
||||
}
|
||||
|
||||
// ==================== Node Operations (Unified/Recursive) ====================
|
||||
|
||||
/**
|
||||
* List nodes (collections and entities) at a location
|
||||
*
|
||||
* @return array<string|int,INodeBase>
|
||||
*/
|
||||
public function nodeList(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $location = null,
|
||||
bool $recursive = false,
|
||||
?array $filter = null,
|
||||
?array $sort = null,
|
||||
?array $range = null
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
|
||||
// construct filter
|
||||
$nodeFilter = null;
|
||||
if ($filter !== null && $filter !== []) {
|
||||
$nodeFilter = $service->nodeListFilter();
|
||||
foreach ($filter as $attribute => $value) {
|
||||
$nodeFilter->condition($attribute, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// construct sort
|
||||
$nodeSort = null;
|
||||
if ($sort !== null && $sort !== []) {
|
||||
$nodeSort = $service->nodeListSort();
|
||||
foreach ($sort as $attribute => $direction) {
|
||||
$nodeSort->condition($attribute, $direction);
|
||||
}
|
||||
}
|
||||
|
||||
// construct range
|
||||
$nodeRange = null;
|
||||
if ($range !== null && $range !== [] && isset($range['type'])) {
|
||||
$nodeRange = $service->nodeListRange(RangeType::from($range['type']));
|
||||
if ($nodeRange instanceof IRangeTally) {
|
||||
if (isset($range['anchor'])) {
|
||||
$nodeRange->setAnchor(RangeAnchorType::from($range['anchor']));
|
||||
}
|
||||
if (isset($range['position'])) {
|
||||
$nodeRange->setPosition($range['position']);
|
||||
}
|
||||
if (isset($range['tally'])) {
|
||||
$nodeRange->setTally($range['tally']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $service->nodeList($location, $recursive, $nodeFilter, $nodeSort, $nodeRange);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get node delta/changes since a signature
|
||||
*/
|
||||
public function nodeDelta(
|
||||
string $tenantId,
|
||||
string $userId,
|
||||
string $providerId,
|
||||
string|int $serviceId,
|
||||
string|int|null $location,
|
||||
string $signature,
|
||||
bool $recursive = false,
|
||||
string $detail = 'ids'
|
||||
): array {
|
||||
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
|
||||
return $service->nodeDelta($location, $signature, $recursive, $detail);
|
||||
}
|
||||
}
|
||||
65
lib/Module.php
Normal file
65
lib/Module.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace KTXM\FileManager;
|
||||
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
|
||||
/**
|
||||
* File Manager Module
|
||||
*/
|
||||
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{ }
|
||||
|
||||
public function handle(): string
|
||||
{
|
||||
return 'file_manager';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'File Manager';
|
||||
}
|
||||
|
||||
public function author(): string
|
||||
{
|
||||
return 'Ktrix';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'File management module for Ktrix - provides file and folder management functionalities';
|
||||
}
|
||||
|
||||
public function version(): string
|
||||
{
|
||||
return '0.0.1';
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'file_manager' => [
|
||||
'label' => 'Access File Manager',
|
||||
'description' => 'View and access the file manager module',
|
||||
'group' => 'File Management'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function registerBI(): array {
|
||||
return [
|
||||
'handle' => $this->handle(),
|
||||
'namespace' => 'FileManager',
|
||||
'version' => $this->version(),
|
||||
'label' => $this->label(),
|
||||
'author' => $this->author(),
|
||||
'description' => $this->description(),
|
||||
'boot' => 'static/module.mjs',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
285
lib/Transfer/StreamingZip.php
Normal file
285
lib/Transfer/StreamingZip.php
Normal file
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileManager\Transfer;
|
||||
|
||||
/**
|
||||
* Native PHP streaming ZIP archive generator
|
||||
*
|
||||
* Creates ZIP files on-the-fly without requiring external libraries.
|
||||
* Streams directly to output, minimizing memory usage.
|
||||
*
|
||||
* Supports:
|
||||
* - Store method (no compression) for maximum streaming efficiency
|
||||
* - Deflate compression for smaller archives
|
||||
* - Data descriptors for streaming without knowing file size upfront
|
||||
* - Files up to 4GB (ZIP64 not implemented)
|
||||
*/
|
||||
class StreamingZip
|
||||
{
|
||||
/** @var resource Output stream */
|
||||
private $output;
|
||||
|
||||
/** @var array Central directory entries */
|
||||
private array $centralDirectory = [];
|
||||
|
||||
/** @var int Current byte offset in the archive */
|
||||
private int $offset = 0;
|
||||
|
||||
/** @var bool Whether to use deflate compression */
|
||||
private bool $compress;
|
||||
|
||||
/** @var int Compression level (1-9) */
|
||||
private int $compressionLevel;
|
||||
|
||||
/**
|
||||
* @param resource|null $output Output stream (defaults to php://output)
|
||||
* @param bool $compress Whether to compress files (deflate)
|
||||
* @param int $compressionLevel Compression level 1-9 (only used if compress=true)
|
||||
*/
|
||||
public function __construct($output = null, bool $compress = false, int $compressionLevel = 6)
|
||||
{
|
||||
$this->output = $output ?? fopen('php://output', 'wb');
|
||||
$this->compress = $compress;
|
||||
$this->compressionLevel = $compressionLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file from a stream resource
|
||||
*
|
||||
* @param string $path Path/name within the ZIP archive
|
||||
* @param resource $stream File content stream
|
||||
* @param int|null $modTime Unix timestamp for modification time (null = now)
|
||||
*/
|
||||
public function addFileFromStream(string $path, $stream, ?int $modTime = null): void
|
||||
{
|
||||
$modTime = $modTime ?? time();
|
||||
$dosTime = $this->unixToDosTime($modTime);
|
||||
|
||||
// Read entire file content first (needed for accurate CRC)
|
||||
$content = '';
|
||||
while (!feof($stream)) {
|
||||
$chunk = fread($stream, 65536);
|
||||
if ($chunk === false || $chunk === '') {
|
||||
break;
|
||||
}
|
||||
$content .= $chunk;
|
||||
}
|
||||
|
||||
$uncompressedSize = strlen($content);
|
||||
$crcValue = crc32($content);
|
||||
|
||||
// Compress if needed
|
||||
if ($this->compress && $uncompressedSize > 0) {
|
||||
$compressed = gzdeflate($content, $this->compressionLevel);
|
||||
$compressedSize = strlen($compressed);
|
||||
$method = 0x0008; // Deflate
|
||||
} else {
|
||||
$compressed = $content;
|
||||
$compressedSize = $uncompressedSize;
|
||||
$method = 0x0000; // Store
|
||||
}
|
||||
|
||||
// General purpose flags
|
||||
$gpFlags = 0x0000;
|
||||
|
||||
// UTF-8 filename flag
|
||||
if (preg_match('//u', $path)) {
|
||||
$gpFlags |= 0x0800; // Bit 11: UTF-8 filename
|
||||
}
|
||||
|
||||
$pathBytes = $path;
|
||||
|
||||
// Local file header
|
||||
$localHeader = pack('V', 0x04034b50); // Local file header signature
|
||||
$localHeader .= pack('v', 20); // Version needed to extract (2.0)
|
||||
$localHeader .= pack('v', $gpFlags); // General purpose bit flag
|
||||
$localHeader .= pack('v', $method); // Compression method
|
||||
$localHeader .= pack('V', $dosTime); // Last mod time & date
|
||||
$localHeader .= pack('V', $crcValue); // CRC-32
|
||||
$localHeader .= pack('V', $compressedSize); // Compressed size
|
||||
$localHeader .= pack('V', $uncompressedSize); // Uncompressed size
|
||||
$localHeader .= pack('v', strlen($pathBytes)); // File name length
|
||||
$localHeader .= pack('v', 0); // Extra field length
|
||||
$localHeader .= $pathBytes; // File name
|
||||
|
||||
fwrite($this->output, $localHeader);
|
||||
fwrite($this->output, $compressed);
|
||||
|
||||
$headerLen = strlen($localHeader);
|
||||
|
||||
// Store entry for central directory
|
||||
$this->centralDirectory[] = [
|
||||
'path' => $pathBytes,
|
||||
'crc' => $crcValue,
|
||||
'compressedSize' => $compressedSize,
|
||||
'uncompressedSize' => $uncompressedSize,
|
||||
'method' => $method,
|
||||
'dosTime' => $dosTime,
|
||||
'gpFlags' => $gpFlags,
|
||||
'offset' => $this->offset,
|
||||
];
|
||||
|
||||
$this->offset += $headerLen + $compressedSize;
|
||||
|
||||
// Free memory
|
||||
unset($content, $compressed);
|
||||
|
||||
@flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file from a string
|
||||
*
|
||||
* @param string $path Path/name within the ZIP archive
|
||||
* @param string $content File content
|
||||
* @param int|null $modTime Unix timestamp for modification time
|
||||
*/
|
||||
public function addFileFromString(string $path, string $content, ?int $modTime = null): void
|
||||
{
|
||||
$stream = fopen('php://temp', 'r+b');
|
||||
fwrite($stream, $content);
|
||||
rewind($stream);
|
||||
|
||||
$this->addFileFromStream($path, $stream, $modTime);
|
||||
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an empty directory entry
|
||||
*
|
||||
* @param string $path Directory path (will have / appended if missing)
|
||||
* @param int|null $modTime Unix timestamp
|
||||
*/
|
||||
public function addDirectory(string $path, ?int $modTime = null): void
|
||||
{
|
||||
if (!str_ends_with($path, '/')) {
|
||||
$path .= '/';
|
||||
}
|
||||
|
||||
$modTime = $modTime ?? time();
|
||||
$dosTime = $this->unixToDosTime($modTime);
|
||||
|
||||
$gpFlags = 0x0000;
|
||||
if (preg_match('//u', $path)) {
|
||||
$gpFlags |= 0x0800;
|
||||
}
|
||||
|
||||
$pathBytes = $path;
|
||||
|
||||
// Local file header for directory
|
||||
$localHeader = pack('V', 0x04034b50);
|
||||
$localHeader .= pack('v', 20);
|
||||
$localHeader .= pack('v', $gpFlags);
|
||||
$localHeader .= pack('v', 0); // Store method
|
||||
$localHeader .= pack('V', $dosTime);
|
||||
$localHeader .= pack('V', 0); // CRC
|
||||
$localHeader .= pack('V', 0); // Compressed size
|
||||
$localHeader .= pack('V', 0); // Uncompressed size
|
||||
$localHeader .= pack('v', strlen($pathBytes));
|
||||
$localHeader .= pack('v', 0);
|
||||
$localHeader .= $pathBytes;
|
||||
|
||||
fwrite($this->output, $localHeader);
|
||||
$headerLen = strlen($localHeader);
|
||||
|
||||
$this->centralDirectory[] = [
|
||||
'path' => $pathBytes,
|
||||
'crc' => 0,
|
||||
'compressedSize' => 0,
|
||||
'uncompressedSize' => 0,
|
||||
'method' => 0,
|
||||
'dosTime' => $dosTime,
|
||||
'gpFlags' => $gpFlags,
|
||||
'offset' => $this->offset,
|
||||
'externalAttr' => 0x10, // Directory attribute
|
||||
];
|
||||
|
||||
$this->offset += $headerLen;
|
||||
|
||||
@flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalize and close the ZIP archive
|
||||
*/
|
||||
public function finish(): void
|
||||
{
|
||||
$cdOffset = $this->offset;
|
||||
$cdSize = 0;
|
||||
|
||||
// Write central directory
|
||||
foreach ($this->centralDirectory as $entry) {
|
||||
$record = $this->buildCentralDirectoryEntry($entry);
|
||||
fwrite($this->output, $record);
|
||||
$cdSize += strlen($record);
|
||||
}
|
||||
|
||||
// End of central directory record
|
||||
$eocd = pack('V', 0x06054b50); // EOCD signature
|
||||
$eocd .= pack('v', 0); // Disk number
|
||||
$eocd .= pack('v', 0); // Disk with CD
|
||||
$eocd .= pack('v', count($this->centralDirectory)); // Entries on this disk
|
||||
$eocd .= pack('v', count($this->centralDirectory)); // Total entries
|
||||
$eocd .= pack('V', $cdSize); // Central directory size
|
||||
$eocd .= pack('V', $cdOffset); // CD offset
|
||||
$eocd .= pack('v', 0); // Comment length
|
||||
|
||||
fwrite($this->output, $eocd);
|
||||
|
||||
@flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a central directory file header entry
|
||||
*/
|
||||
private function buildCentralDirectoryEntry(array $entry): string
|
||||
{
|
||||
$externalAttr = $entry['externalAttr'] ?? 0;
|
||||
|
||||
$record = pack('V', 0x02014b50); // Central directory signature
|
||||
$record .= pack('v', 0x033F); // Version made by (Unix, 6.3)
|
||||
$record .= pack('v', 20); // Version needed
|
||||
$record .= pack('v', $entry['gpFlags']); // General purpose flags
|
||||
$record .= pack('v', $entry['method']); // Compression method
|
||||
$record .= pack('V', $entry['dosTime']); // Last mod time & date
|
||||
$record .= pack('V', $entry['crc']); // CRC-32
|
||||
$record .= pack('V', $entry['compressedSize']); // Compressed size
|
||||
$record .= pack('V', $entry['uncompressedSize']); // Uncompressed size
|
||||
$record .= pack('v', strlen($entry['path'])); // File name length
|
||||
$record .= pack('v', 0); // Extra field length
|
||||
$record .= pack('v', 0); // Comment length
|
||||
$record .= pack('v', 0); // Disk number start
|
||||
$record .= pack('v', 0); // Internal file attributes
|
||||
$record .= pack('V', $externalAttr); // External file attributes
|
||||
$record .= pack('V', $entry['offset']); // Relative offset of local header
|
||||
$record .= $entry['path']; // File name
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Unix timestamp to DOS date/time format
|
||||
*/
|
||||
private function unixToDosTime(int $timestamp): int
|
||||
{
|
||||
$date = getdate($timestamp);
|
||||
|
||||
$year = max(0, $date['year'] - 1980);
|
||||
if ($year > 127) {
|
||||
$year = 127;
|
||||
}
|
||||
|
||||
$dosDate = ($year << 9) | ($date['mon'] << 5) | $date['mday'];
|
||||
$dosTime = ($date['hours'] << 11) | ($date['minutes'] << 5) | (int)($date['seconds'] / 2);
|
||||
|
||||
return ($dosDate << 16) | $dosTime;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user