-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new command for uploading files (dbp:relay:cabinet:upload-file)
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters