Skip to content

Commit

Permalink
Add a new command for uploading files (dbp:relay:cabinet:upload-file)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazka committed Aug 28, 2024
1 parent 4aa7ef6 commit eceddb1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
73 changes: 73 additions & 0 deletions src/Command/UploadFileCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Dbp\Relay\CabinetBundle\Command;

use Dbp\Relay\BlobLibrary\Api\BlobApiError;
use Dbp\Relay\CabinetBundle\Service\BlobService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;

class UploadFileCommand extends Command
{
private BlobService $blobService;

public function __construct(BlobService $blobService)
{
parent::__construct();

$this->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("<error>File not found: $filepath</error>");

return Command::FAILURE;
}

$filename = basename($filepath);
$payload = file_get_contents($filepath);

if ($payload === false) {
$output->writeln("<error>Unable to read file: $filepath</error>");

return Command::FAILURE;
}

try {
$fileId = $this->blobService->uploadFile($filename, $payload, $type, $metadata);
$output->writeln("<info>File uploaded successfully: $fileId</info>");

return Command::SUCCESS;
} catch (BlobApiError $e) {
$output->writeln('<error>Error uploading file: '.$e->getMessage().' </error>');
$output->writeln(print_r($e->getErrorId(), true));
$output->writeln(print_r($e->getErrorDetails(), true));

return Command::FAILURE;
}
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/Service/BlobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Service/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public function getBlobBucketKey(): string
{
return $this->config['blob']['bucket_key'];
}

public function getBlobBucketPrefix(): string
{
return 'document-';
}
}

0 comments on commit eceddb1

Please sign in to comment.