diff --git a/src/Command/UploadFileCommand.php b/src/Command/UploadFileCommand.php new file mode 100644 index 0000000..9bc2dc4 --- /dev/null +++ b/src/Command/UploadFileCommand.php @@ -0,0 +1,73 @@ +blobService = $blobService; + } + + protected function configure(): void + { + $this->setName('dbp:relay:cabinet:upload-file'); + $this->setDescription('Upload a file to the cabinet blob bucket'); + + $this->addArgument('filepath', InputArgument::REQUIRED, 'The path to the file to upload'); + $this->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'The type of the file'); + $this->addOption('metadata', 'm', InputOption::VALUE_OPTIONAL, 'The metadata of the file'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $filepath = $input->getArgument('filepath'); + $type = $input->getOption('type'); + $metadata = $input->getOption('metadata'); + + $filesystem = new Filesystem(); + + if (!$filesystem->exists($filepath)) { + $output->writeln("File not found: $filepath"); + + return Command::FAILURE; + } + + $filename = basename($filepath); + $payload = file_get_contents($filepath); + + if ($payload === false) { + $output->writeln("Unable to read file: $filepath"); + + return Command::FAILURE; + } + + try { + $fileId = $this->blobService->uploadFile($filename, $payload, $type, $metadata); + $output->writeln("File uploaded successfully: $fileId"); + + return Command::SUCCESS; + } catch (BlobApiError $e) { + $output->writeln('Error uploading file: '.$e->getMessage().' '); + $output->writeln(print_r($e->getErrorId(), true)); + $output->writeln(print_r($e->getErrorDetails(), true)); + + return Command::FAILURE; + } + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 1cb56ae..1137086 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -44,6 +44,10 @@ services: autowire: true autoconfigure: true + Dbp\Relay\CabinetBundle\Command\UploadFileCommand: + autowire: true + autoconfigure: true + Dbp\Relay\CabinetBundle\Authorization\AuthorizationService: autowire: true autoconfigure: true diff --git a/src/Service/BlobService.php b/src/Service/BlobService.php index 8e44510..8f654c2 100644 --- a/src/Service/BlobService.php +++ b/src/Service/BlobService.php @@ -27,14 +27,28 @@ public function __construct(AuthorizationService $auth, ConfigurationService $co $this->config = $config; } - public function checkConnection(): void + private function getInternalBlobApi(): BlobApi { $config = $this->config; $blobApi = new BlobApi($config->getBlobApiUrlInternal(), $config->getBlobBucketId(), $config->getBlobBucketKey()); $blobApi->setOAuth2Token($config->getBlobIdpUrl(), $config->getBlobIdpClientId(), $config->getBlobIdpClientSecret()); + + return $blobApi; + } + + public function checkConnection(): void + { + $blobApi = $this->getInternalBlobApi(); $blobApi->getFileDataByPrefix(Uuid::v4()->toRfc4122(), 0); } + public function uploadFile(string $filename, string $payload, ?string $type = null, ?string $metadata = null): string + { + $blobApi = $this->getInternalBlobApi(); + + return $blobApi->uploadFile($this->config->getBlobBucketPrefix(), $filename, $payload, $metadata ?? '', $type ?? ''); + } + public function getSignatureForGivenRequest(Request $request): Response { if (!$this->auth->isAuthenticated()) { diff --git a/src/Service/ConfigurationService.php b/src/Service/ConfigurationService.php index 8820329..625e0d9 100644 --- a/src/Service/ConfigurationService.php +++ b/src/Service/ConfigurationService.php @@ -57,4 +57,9 @@ public function getBlobBucketKey(): string { return $this->config['blob']['bucket_key']; } + + public function getBlobBucketPrefix(): string + { + return 'document-'; + } }