From d0697b4889419bac7f6e2f365f1904891b8c6d5b Mon Sep 17 00:00:00 2001 From: bartlomiej Date: Tue, 10 May 2022 10:10:45 +0200 Subject: [PATCH] chore: taomedia link handling --- .../ImageToPropertiesHelper.php | 81 +++++++++++++++++++ .../Task/UpgradeTextReaderInteractionTask.php | 58 ++++++------- .../UpgradeProcessServiceProvider.php | 13 +++ 3 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 model/LegacyPciHelper/ImageToPropertiesHelper.php diff --git a/model/LegacyPciHelper/ImageToPropertiesHelper.php b/model/LegacyPciHelper/ImageToPropertiesHelper.php new file mode 100644 index 0000000..ed8e206 --- /dev/null +++ b/model/LegacyPciHelper/ImageToPropertiesHelper.php @@ -0,0 +1,81 @@ +mediaSource = $mediaSource; + $this->fileManagement = $fileManagement; + } + + public function addImagesToProperties(array $images, array $properties, Directory $itemDirectory): array + { + foreach ($images as $image) { + if ($this->isImageMediaManager($image['fileName'])) { + $fileInfo = $this->mediaSource->getFileInfo($image['fileName']); + $properties = $this->addBase64Image( + $properties, + $image['fileName'], + $this->fileManagement->getFileStream( + $fileInfo['link'] + )->read(1024 * 8) + ); + continue; + } + $properties = $this->addBase64Image( + $properties, + $image['fileName'], + $itemDirectory->getFile($image['fileName'])->read() + ); + } + + return $properties; + } + + private function addBase64Image(array $properties, string $fileName, string $image): array + { + $properties['content-' . $fileName] = sprintf( + "data:image/png;base64,%s", + base64_encode($image) + ); + + return $properties; + } + + private function isImageMediaManager(string $fileName): bool + { + return false !== strpos($fileName, 'taomedia://'); + } +} diff --git a/model/LegacyPciHelper/Task/UpgradeTextReaderInteractionTask.php b/model/LegacyPciHelper/Task/UpgradeTextReaderInteractionTask.php index 89d3dec..5a47dbf 100644 --- a/model/LegacyPciHelper/Task/UpgradeTextReaderInteractionTask.php +++ b/model/LegacyPciHelper/Task/UpgradeTextReaderInteractionTask.php @@ -29,8 +29,10 @@ use oat\oatbox\filesystem\Directory; use oat\oatbox\reporting\Report; use oat\oatbox\service\ServiceManagerAwareTrait; +use oat\pciSamples\model\LegacyPciHelper\ImageToPropertiesHelper; use oat\pciSamples\model\LegacyPciHelper\TextReaderLegacyDetection; use oat\taoQtiItem\helpers\QtiFile; +use oat\taoQtiItem\model\qti\interaction\CustomInteraction; use oat\taoQtiItem\model\qti\interaction\PortableCustomInteraction; use oat\taoQtiItem\model\qti\Item; use oat\taoQtiItem\model\qti\Parser; @@ -55,7 +57,7 @@ public function __invoke($params) } $this->itemDirectory = $this->getItemService()->getItemDirectory($itemResource); - $itemXmlFile = $this->getItemService()->getItemDirectory($itemResource)->getFile(QtiFile::FILE); + $itemXmlFile = $this->itemDirectory->getFile(QtiFile::FILE); $parser = new Parser($itemXmlFile->read()); $xmlItem = $parser->load(); @@ -63,24 +65,16 @@ public function __invoke($params) return Report::createSuccess("Item does not contain Legacy PCI Text Reader"); } - /** @var PortableCustomInteraction $pciInteraction */ - foreach ($xmlItem->getBody()->getElements(PortableCustomInteraction::class) as $pciInteraction) { - if ($this->getTextReaderLegacyDetection()->isTextReaderWithImage($pciInteraction)) { - $properties = $pciInteraction->getProperties(); - foreach ($properties['pages'] as $page) { - $images = $this->extractImages($page['content']); - $properties = $this->addImagesToProperties($images, $properties); - } - - $pciInteraction->setProperties($properties); - } - } - try { + $this->addImagesToProperties($xmlItem); $this->getQtiService()->saveDataItemToRdfItem($xmlItem, $itemResource); } catch (Exception $e) { return Report::createError( - sprintf('Task failed with item %s', $itemResource->getUri()) + sprintf( + 'Task failed with item %s with error: %s', + $itemResource->getUri(), + $e->getMessage() + ) ); } return Report::createSuccess( @@ -108,21 +102,9 @@ private function extractImages(array $content): array return $images; } - private function addImagesToProperties(array $images, array $properties): array - { - foreach ($images as $image) { - $properties['content-' . $image['fileName']] = sprintf( - "data:image/png;base64,%s", - base64_encode($this->itemDirectory->getFile($image['fileName'])->read()) - ); - } - - return $properties; - } - private function getPciInteractions(Item $xmlItem): array { - return $xmlItem->getBody()->getElements(PortableCustomInteraction::class); + return $xmlItem->getBody()->getElements(CustomInteraction::class); } private function isLegacyTextReader(Item $item): bool @@ -160,4 +142,24 @@ private function getQtiService(): QtiService { return $this->getServiceManager()->getContainer()->get(QtiService::class); } + + private function getImageToPropertyHelper(): ImageToPropertiesHelper + { + return $this->getServiceManager()->getContainer()->get(ImageToPropertiesHelper::class); + } + + private function addImagesToProperties(Item $xmlItem): void + { + foreach ($xmlItem->getBody()->getElements(PortableCustomInteraction::class) as $pciInteraction) { + if ($this->getTextReaderLegacyDetection()->isTextReaderWithImage($pciInteraction)) { + $properties = $pciInteraction->getProperties(); + foreach ($properties['pages'] as $page) { + $images = $this->extractImages($page['content']); + $properties = $this->getImageToPropertyHelper()->addImagesToProperties($images, $properties, $this->itemDirectory); + } + + $pciInteraction->setProperties($properties); + } + } + } } diff --git a/model/ServiceProvider/UpgradeProcessServiceProvider.php b/model/ServiceProvider/UpgradeProcessServiceProvider.php index 764ec0c..81aba94 100644 --- a/model/ServiceProvider/UpgradeProcessServiceProvider.php +++ b/model/ServiceProvider/UpgradeProcessServiceProvider.php @@ -23,9 +23,12 @@ namespace oat\pciSamples\model\ServiceProvider; use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface; +use oat\pciSamples\model\LegacyPciHelper\ImageToPropertiesHelper; use oat\pciSamples\model\LegacyPciHelper\LegacyTextReaderItemUpdate; use oat\pciSamples\model\LegacyPciHelper\TextReaderLegacyDetection; use oat\tao\model\taskQueue\QueueDispatcherInterface; +use oat\taoMediaManager\model\fileManagement\FileManagement; +use oat\taoMediaManager\model\MediaSource; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use taoItems_models_classes_ItemsService; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; @@ -37,6 +40,16 @@ public function __invoke(ContainerConfigurator $configurator): void { $services = $configurator->services(); + $services->set(MediaSource::class, MediaSource::class) + ->private(); + + $services->set(ImageToPropertiesHelper::class, ImageToPropertiesHelper::class) + ->public() + ->args([ + service(MediaSource::class), + service(FileManagement::SERVICE_ID) + ]); + $services ->set(LegacyTextReaderItemUpdate::class, LegacyTextReaderItemUpdate::class) ->public()