From 63ea49425859d4283b4601750002aafa1e603948 Mon Sep 17 00:00:00 2001 From: Tomas Hermanek Date: Wed, 30 Oct 2024 13:49:54 +0100 Subject: [PATCH] * Default Author order by reviewed + score * PNG resizes and crop served as png * KW changed collation --- src/Controller/AbstractImageController.php | 23 +++++++------------ .../Modules/Youtube/YoutubeApiClient.php | 4 +++- .../Modules/YoutubeDistributionModule.php | 1 + src/Domain/Image/Crop/CropProcessor.php | 14 ++++++++--- src/Domain/Image/ImageRotator.php | 10 ++++++-- .../OptimalResizeFactory.php | 9 ++++++-- .../SearchDto/AuthorAdmSearchDto.php | 8 +++++++ src/Entity/Keyword.php | 2 +- 8 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/Controller/AbstractImageController.php b/src/Controller/AbstractImageController.php index 0baf0991..4b961562 100644 --- a/src/Controller/AbstractImageController.php +++ b/src/Controller/AbstractImageController.php @@ -7,6 +7,7 @@ use AnzuSystems\CoreDamBundle\Domain\Configuration\ConfigurationProvider; use AnzuSystems\CoreDamBundle\Domain\Configuration\DomainProvider; use AnzuSystems\CoreDamBundle\Domain\Image\Crop\CropFacade; +use AnzuSystems\CoreDamBundle\Domain\Image\Crop\CropProcessor; use AnzuSystems\CoreDamBundle\Entity\AssetFile; use AnzuSystems\CoreDamBundle\Entity\ImageFile; use AnzuSystems\CoreDamBundle\Entity\RegionOfInterest; @@ -16,6 +17,7 @@ use AnzuSystems\CoreDamBundle\Helper\FileNameHelper; use AnzuSystems\CoreDamBundle\Model\Dto\Image\Crop\RequestedCropDto; use AnzuSystems\CoreDamBundle\Repository\ImageFileRepository; +use AnzuSystems\CoreDamBundle\Traits\FileHelperTrait; use Doctrine\ORM\NonUniqueResultException; use League\Flysystem\FilesystemException; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -26,8 +28,7 @@ abstract class AbstractImageController extends AbstractPublicController { - public const string CROP_EXTENSION = 'jpeg'; - public const string DEFAULT_CROP_MIME_TYPE = 'image/jpeg'; + use FileHelperTrait; protected DomainProvider $domainProvider; protected ImageFileRepository $imageFileRepository; @@ -84,14 +85,6 @@ protected function okImageResponse( return $response; } - protected function okResponse(string $content, AssetFile $asset): Response - { - $response = $this->getImageResponse($content, $asset)->setStatusCode(Response::HTTP_OK); - $this->assetFileCacheManager->setCache($response, $asset); - - return $response; - } - protected function notFoundResponse(): Response { $response = new Response('', Response::HTTP_NOT_FOUND); @@ -160,20 +153,20 @@ function () use ($fileStream) { return $response; } - private function getImageResponse(string $content, AssetFile $assetFile): Response + private function getImageResponse(string $content, ImageFile $assetFile): Response { return new Response($content, Response::HTTP_OK, [ - 'Content-Type' => self::DEFAULT_CROP_MIME_TYPE, - 'Content-Disposition' => $this->makeDisposition($assetFile), + 'Content-Type' => CropProcessor::getCropMimeType($assetFile), + 'Content-Disposition' => $this->mageImageCropDisposition($assetFile), 'Content-Length' => strlen($content), ]); } - private function makeDisposition(AssetFile $assetFile): string + private function mageImageCropDisposition(ImageFile $assetFile): string { $fileName = FileNameHelper::changeFileExtension( (string) $assetFile->getId(), - self::CROP_EXTENSION + $this->fileHelper->guessExtension(CropProcessor::getCropMimeType($assetFile)), ); return HeaderUtils::makeDisposition( diff --git a/src/Distribution/Modules/Youtube/YoutubeApiClient.php b/src/Distribution/Modules/Youtube/YoutubeApiClient.php index 40e5932b..d993438a 100644 --- a/src/Distribution/Modules/Youtube/YoutubeApiClient.php +++ b/src/Distribution/Modules/Youtube/YoutubeApiClient.php @@ -6,6 +6,7 @@ use AnzuSystems\CoreDamBundle\Domain\Image\Crop\CropProcessor; use AnzuSystems\CoreDamBundle\Entity\AssetFile; +use AnzuSystems\CoreDamBundle\Entity\ImageFile; use AnzuSystems\CoreDamBundle\Entity\YoutubeDistribution; use AnzuSystems\CoreDamBundle\Exception\DistributionFailedException; use AnzuSystems\CoreDamBundle\Exception\RuntimeException; @@ -140,6 +141,7 @@ public function setPlaylist( public function setThumbnail( string $distributionService, string $distributionId, + ImageFile $imageFile, string $imageData, ): void { $client = $this->clientProvider->getClient($distributionService); @@ -149,7 +151,7 @@ public function setThumbnail( $youtubeService = new Google_Service_YouTube($client); $response = $youtubeService->thumbnails->set($distributionId, [ 'data' => $imageData, - 'mimeType' => CropProcessor::DEFAULT_MIME_TYPE, + 'mimeType' => CropProcessor::getCropMimeType($imageFile), ]); } catch (Throwable $exception) { $this->damLogger->error( diff --git a/src/Distribution/Modules/YoutubeDistributionModule.php b/src/Distribution/Modules/YoutubeDistributionModule.php index 4d26e2ab..7a1ee0e3 100644 --- a/src/Distribution/Modules/YoutubeDistributionModule.php +++ b/src/Distribution/Modules/YoutubeDistributionModule.php @@ -187,6 +187,7 @@ private function setThumbnail(YoutubeDistribution $distribution): void $this->client->setThumbnail( distributionService: $distribution->getDistributionService(), distributionId: $distribution->getExtId(), + imageFile: $imageFile, imageData: $this->cropFacade->applyCropPayloadToDefaultRoi( image: $imageFile, cropPayload: (new RequestedCropDto()) diff --git a/src/Domain/Image/Crop/CropProcessor.php b/src/Domain/Image/Crop/CropProcessor.php index 5bd97939..0c774435 100644 --- a/src/Domain/Image/Crop/CropProcessor.php +++ b/src/Domain/Image/Crop/CropProcessor.php @@ -16,6 +16,7 @@ use AnzuSystems\CoreDamBundle\Image\Filter\ResizeFilter; use AnzuSystems\CoreDamBundle\Image\ImageManipulatorInterface; use AnzuSystems\CoreDamBundle\Model\Dto\Image\ImageCropDto; +use AnzuSystems\CoreDamBundle\Model\Enum\ImageMimeTypes; use AnzuSystems\CoreDamBundle\Traits\FileHelperTrait; use League\Flysystem\FilesystemException; @@ -23,8 +24,6 @@ final class CropProcessor { use FileHelperTrait; - public const string DEFAULT_MIME_TYPE = 'image/jpeg'; - public function __construct( private readonly FileSystemProvider $fileSystemProvider, private readonly ImageManipulatorInterface $imageManipulator, @@ -70,12 +69,21 @@ public function applyCrop(ImageFile $image, ImageCropDto $imageCrop): string ]) ); - $content = $this->imageManipulator->getContent($this->fileHelper->guessExtension(self::DEFAULT_MIME_TYPE)); + $content = $this->imageManipulator->getContent($this->fileHelper->guessExtension($this->getCropMimeType($image))); $this->cropCache->store($image, $imageCrop, $content); return $content; } + public static function getCropMimeType(ImageFile $image): string + { + if ($image->getAssetAttributes()->getMimeType() === ImageMimeTypes::MimePng->value) { + return ImageMimeTypes::MimePng->value; + } + + return ImageMimeTypes::MimeJpeg->value; + } + /** * @throws DomainException */ diff --git a/src/Domain/Image/ImageRotator.php b/src/Domain/Image/ImageRotator.php index 321ccb0b..af2c082f 100644 --- a/src/Domain/Image/ImageRotator.php +++ b/src/Domain/Image/ImageRotator.php @@ -4,8 +4,8 @@ namespace AnzuSystems\CoreDamBundle\Domain\Image; -use AnzuSystems\CoreDamBundle\Controller\AbstractImageController; use AnzuSystems\CoreDamBundle\Domain\AssetFile\FileStash; +use AnzuSystems\CoreDamBundle\Domain\Image\Crop\CropProcessor; use AnzuSystems\CoreDamBundle\Domain\Image\FileProcessor\OptimalCropsProcessor; use AnzuSystems\CoreDamBundle\Domain\ImageFileOptimalResize\OptimalResizeFactory; use AnzuSystems\CoreDamBundle\Domain\RegionOfInterest\DefaultRegionOfInterestFactory; @@ -17,10 +17,13 @@ use AnzuSystems\CoreDamBundle\Image\Filter\FilterStack; use AnzuSystems\CoreDamBundle\Image\Filter\RotateFilter; use AnzuSystems\CoreDamBundle\Image\ImageManipulatorInterface; +use AnzuSystems\CoreDamBundle\Traits\FileHelperTrait; use League\Flysystem\FilesystemException; final class ImageRotator { + use FileHelperTrait; + public const float CLOCK_ANGLE = 360.0; public const float MIRROR_ANGLE = 180.0; @@ -99,7 +102,10 @@ private function rotateResize(ImageFileOptimalResize $resize, float $angle, floa ->setWidth($this->imageManipulator->getWidth()) ->setHeight($this->imageManipulator->getHeight()); - $path = $tmpFilesystem->getTmpFileName(AbstractImageController::CROP_EXTENSION); + $path = $tmpFilesystem->getTmpFileName( + $this->fileHelper->guessExtension(CropProcessor::getCropMimeType($resize->getImage())) + ); + $this->imageManipulator->writeToFile($tmpFilesystem->extendPath($path)); // move old file to stash and write new file $this->fileStash->add($resize); diff --git a/src/Domain/ImageFileOptimalResize/OptimalResizeFactory.php b/src/Domain/ImageFileOptimalResize/OptimalResizeFactory.php index cac14c74..e2c593d1 100644 --- a/src/Domain/ImageFileOptimalResize/OptimalResizeFactory.php +++ b/src/Domain/ImageFileOptimalResize/OptimalResizeFactory.php @@ -5,7 +5,7 @@ namespace AnzuSystems\CoreDamBundle\Domain\ImageFileOptimalResize; use AnzuSystems\CommonBundle\Domain\AbstractManager; -use AnzuSystems\CoreDamBundle\Controller\AbstractImageController; +use AnzuSystems\CoreDamBundle\Domain\Image\Crop\CropProcessor; use AnzuSystems\CoreDamBundle\Entity\ImageFile; use AnzuSystems\CoreDamBundle\Entity\ImageFileOptimalResize; use AnzuSystems\CoreDamBundle\Exception\ImageManipulatorException; @@ -13,10 +13,13 @@ use AnzuSystems\CoreDamBundle\FileSystem\NameGenerator\NameGenerator; use AnzuSystems\CoreDamBundle\Image\ImageManipulatorInterface; use AnzuSystems\CoreDamBundle\Model\Dto\File\AdapterFile; +use AnzuSystems\CoreDamBundle\Traits\FileHelperTrait; use League\Flysystem\FilesystemException as FilesystemExceptionAlias; final class OptimalResizeFactory extends AbstractManager { + use FileHelperTrait; + public function __construct( private readonly FileSystemProvider $fileSystemProvider, private readonly NameGenerator $nameGenerator, @@ -61,7 +64,9 @@ public function createCrop(ImageFile $imageFile, AdapterFile $file, int $size): // Prepare path and folder for Visp to write crop file $tmpFilesystem = $this->fileSystemProvider->getTmpFileSystem(); - $tmpPath = $tmpFilesystem->getTmpFileName(AbstractImageController::CROP_EXTENSION); + $tmpPath = $tmpFilesystem->getTmpFileName( + $this->fileHelper->guessExtension(CropProcessor::getCropMimeType($imageFile)) + ); $tmpFilesystem->ensureDirectory($tmpPath); // Write rotated crop file diff --git a/src/Elasticsearch/SearchDto/AuthorAdmSearchDto.php b/src/Elasticsearch/SearchDto/AuthorAdmSearchDto.php index 8d41f9d6..b074311b 100644 --- a/src/Elasticsearch/SearchDto/AuthorAdmSearchDto.php +++ b/src/Elasticsearch/SearchDto/AuthorAdmSearchDto.php @@ -21,6 +21,14 @@ final class AuthorAdmSearchDto extends AbstractSearchDto #[Serialize] protected ?string $type = null; + public function __construct() + { + $this->setOrder([ + 'reviewed' => 'desc', + '_score' => 'desc', + ]); + } + public function getIndexName(): string { return Author::getResourceName(); diff --git a/src/Entity/Keyword.php b/src/Entity/Keyword.php index cc9b5fc9..b0b5aa87 100644 --- a/src/Entity/Keyword.php +++ b/src/Entity/Keyword.php @@ -32,7 +32,7 @@ class Keyword implements UuidIdentifiableInterface, UserTrackingInterface, TimeT public const int NAME_MAX_LENGTH = 255; - #[ORM\Column(type: Types::STRING, length: self::NAME_MAX_LENGTH)] + #[ORM\Column(type: Types::STRING, length: self::NAME_MAX_LENGTH, options: ['collation' => 'utf8mb4_bin'])] #[Serialize] #[Assert\Length( min: 2,