-
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.
- Loading branch information
Showing
8 changed files
with
212 additions
and
30 deletions.
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,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dbp\Relay\CabinetBundle\Blob; | ||
|
||
use Dbp\Relay\BlobBundle\Entity\FileData; | ||
use Dbp\Relay\BlobBundle\Event\AddFileDataByPostSuccessEvent; | ||
use Dbp\Relay\BlobBundle\Event\ChangeFileDataByPatchSuccessEvent; | ||
use Dbp\Relay\BlobBundle\Event\DeleteFileDataByDeleteSuccessEvent; | ||
use Dbp\Relay\CabinetBundle\Service\ConfigurationService; | ||
use Dbp\Relay\CabinetBundle\TypesenseSync\DocumentTranslator; | ||
use Dbp\Relay\CabinetBundle\TypesenseSync\TypesenseSync; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class BlobSubscriber implements EventSubscriberInterface | ||
{ | ||
private ConfigurationService $config; | ||
private DocumentTranslator $translator; | ||
private TypesenseSync $typesenseSync; | ||
|
||
public function __construct(ConfigurationService $config, DocumentTranslator $translator, TypesenseSync $typesenseSync) | ||
{ | ||
$this->config = $config; | ||
$this->translator = $translator; | ||
$this->typesenseSync = $typesenseSync; | ||
} | ||
|
||
private function isForCabinet(FileData $fileData): bool | ||
{ | ||
$bucket = $fileData->getBucket(); | ||
if ($bucket->getBucketID() !== $this->config->getBlobBucketId()) { | ||
return false; | ||
} | ||
if ($fileData->getPrefix() !== $this->config->getBlobBucketPrefix()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
AddFileDataByPostSuccessEvent::class => 'onFileAdded', | ||
ChangeFileDataByPatchSuccessEvent::class => 'onFileChanged', | ||
DeleteFileDataByDeleteSuccessEvent::class => 'onFileRemoved', | ||
]; | ||
} | ||
|
||
private function translateMetadata(FileData $fileData): array | ||
{ | ||
$metadata = json_decode($fileData->getMetadata(), associative: true, flags: JSON_THROW_ON_ERROR); | ||
$objectType = $metadata['objectType']; | ||
$input = [ | ||
'id' => $fileData->getIdentifier(), | ||
'fileSource' => $fileData->getBucket()->getBucketID(), | ||
'fileName' => $fileData->getFileName(), | ||
'metadata' => $metadata, | ||
]; | ||
|
||
return $this->translator->translateDocument($objectType, $input); | ||
} | ||
|
||
public function onFileAdded(AddFileDataByPostSuccessEvent $event) | ||
{ | ||
$fileData = $event->getFileData(); | ||
if (!$this->isForCabinet($fileData)) { | ||
return; | ||
} | ||
|
||
$translated = $this->translateMetadata($fileData); | ||
$this->typesenseSync->upsertPartialFile($translated); | ||
} | ||
|
||
public function onFileChanged(ChangeFileDataByPatchSuccessEvent $event) | ||
{ | ||
$fileData = $event->getFileData(); | ||
if (!$this->isForCabinet($fileData)) { | ||
return; | ||
} | ||
|
||
$translated = $this->translateMetadata($fileData); | ||
$this->typesenseSync->upsertPartialFile($translated); | ||
} | ||
|
||
public function onFileRemoved(DeleteFileDataByDeleteSuccessEvent $event) | ||
{ | ||
$fileData = $event->getFileData(); | ||
if (!$this->isForCabinet($fileData)) { | ||
return; | ||
} | ||
|
||
$translated = $this->translateMetadata($fileData); | ||
$this->typesenseSync->removePartialFile($translated); | ||
} | ||
} |
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,49 @@ | ||
<?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\Output\OutputInterface; | ||
|
||
class DeleteFileCommand extends Command | ||
{ | ||
private BlobService $blobService; | ||
|
||
public function __construct(BlobService $blobService) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->blobService = $blobService; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('dbp:relay:cabinet:delete-file'); | ||
$this->setDescription('Delete a file from the cabinet blob bucket'); | ||
$this->addArgument('id', InputArgument::REQUIRED, 'The ID of the file'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$fileId = $input->getArgument('id'); | ||
|
||
try { | ||
$this->blobService->deleteFile($fileId); | ||
$output->writeln("<info>File deleted successfully: $fileId</info>"); | ||
|
||
return Command::SUCCESS; | ||
} catch (BlobApiError $e) { | ||
$output->writeln('<error>Error deleting 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 was deleted.
Oops, something went wrong.
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
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