Skip to content

Commit

Permalink
* Default Author order by reviewed + score
Browse files Browse the repository at this point in the history
 * PNG resizes and crop served as png
 * KW changed collation
  • Loading branch information
TomasHermanek committed Oct 30, 2024
1 parent 96318cf commit 63ea494
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 24 deletions.
23 changes: 8 additions & 15 deletions src/Controller/AbstractImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion src/Distribution/Modules/Youtube/YoutubeApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,6 +141,7 @@ public function setPlaylist(
public function setThumbnail(
string $distributionService,
string $distributionId,
ImageFile $imageFile,
string $imageData,
): void {
$client = $this->clientProvider->getClient($distributionService);
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/Distribution/Modules/YoutubeDistributionModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 11 additions & 3 deletions src/Domain/Image/Crop/CropProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
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;

final class CropProcessor
{
use FileHelperTrait;

public const string DEFAULT_MIME_TYPE = 'image/jpeg';

public function __construct(
private readonly FileSystemProvider $fileSystemProvider,
private readonly ImageManipulatorInterface $imageManipulator,
Expand Down Expand Up @@ -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
*/
Expand Down
10 changes: 8 additions & 2 deletions src/Domain/Image/ImageRotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/Domain/ImageFileOptimalResize/OptimalResizeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
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;
use AnzuSystems\CoreDamBundle\FileSystem\FileSystemProvider;
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,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/Elasticsearch/SearchDto/AuthorAdmSearchDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Keyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 63ea494

Please sign in to comment.