Skip to content

Commit

Permalink
Small updates (#89)
Browse files Browse the repository at this point in the history
* Default Author order by reviewed + score
* PNG resizes and crop served as png
* KW changed collation
* Crop cache updates.
* Add filesystem provider method
* Disable vips cache (memory problems fix)
  • Loading branch information
TomasHermanek authored Nov 4, 2024
1 parent 96318cf commit 674bb8d
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 37 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
7 changes: 3 additions & 4 deletions src/Domain/Image/Crop/CropCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace AnzuSystems\CoreDamBundle\Domain\Image\Crop;

use AnzuSystems\CoreDamBundle\Entity\ExtSystem;
use AnzuSystems\CoreDamBundle\Entity\ImageFile;
use AnzuSystems\CoreDamBundle\FileSystem\FileSystemProvider;
use AnzuSystems\CoreDamBundle\FileSystem\NameGenerator\NameGenerator;
Expand Down Expand Up @@ -60,18 +59,18 @@ public function get(ImageFile $image, ImageCropDto $imageCrop): string
public function removeCache(ImageFile $image): void
{
$this->removeCacheByOriginFilePath(
$image->getExtSystem(),
$image->getExtSystem()->getSlug(),
$image->getAssetAttributes()->getFilePath()
);
}

/**
* @throws FilesystemException
*/
public function removeCacheByOriginFilePath(ExtSystem $extSystem, string $path): void
public function removeCacheByOriginFilePath(string $extSystemSlug, string $path): void
{
$this->fileSystemProvider
->getCropFilesystemByExtSystemSlug($extSystem->getSlug())
->getCropFilesystemByExtSystemSlug($extSystemSlug)
->deleteDirectory(
$this->getCacheDir($path)
);
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
4 changes: 3 additions & 1 deletion src/Domain/Image/ImageStatusFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ protected function processAssetFile(AssetFile $assetFile, AdapterFile $file): As
$imageFile = $this->getImage($assetFile);
$this->imageManipulator->loadFile($file->getRealPath());

// TODo most dominant color memory problems
$imageFile->getImageAttributes()
->setAnimated($this->imageManipulator->isAnimated())
->setMostDominantColor($this->imageManipulator->getMostDominantColor())
// ->setMostDominantColor($this->imageManipulator->getMostDominantColor())
;

$this->optimalCropsProcessor->process($imageFile, $file);
$this->defaultRoiProcessor->process($imageFile, $file);
$this->imageManipulator->clean();

return $imageFile;
}
Expand Down
13 changes: 10 additions & 3 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,12 +64,14 @@ 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
$this->imageManipulator->writeToFile($tmpFilesystem->extendPath($tmpPath), false);
// Write file to target storage
// // Write file to target storage
$assetFileSystem = $this->fileSystemProvider->getFilesystemByStorable($imageFile);
$storagePath = $this->createOptimalCropPath($imageFile, $size, $imageFile->getImageAttributes()->getRotation());
$assetFileSystem->writeStream($storagePath, $tmpFilesystem->readStream($tmpPath));
Expand All @@ -80,6 +85,8 @@ public function createCrop(ImageFile $imageFile, AdapterFile $file, int $size):
$optimalResize->setImage($imageFile);
$imageFile->getResizes()->add($optimalResize);

$this->imageManipulator->clean();

return $this->optimalResizeManager->create($optimalResize, false);
}
}
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
2 changes: 1 addition & 1 deletion src/Event/Subscriber/AssetFileDeleteEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function deleteAssetFile(AssetFileDeleteEvent $event): void
if ($event->getType()->is(AssetType::Image)) {
if (false === empty($event->getAssetFile()->getFilePath())) {
$this->cropCache->removeCacheByOriginFilePath(
$event->getAssetFile()->getExtSystem(),
$event->getAssetFile()->getExtSystem()->getSlug(),
$event->getAssetFile()->getFilePath()
);
}
Expand Down
7 changes: 7 additions & 0 deletions src/FileSystem/FileSystemProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public function getFilesystemByStorable(FileSystemStorableInterface $storable):
return $filesystem;
}

public function getStorageNameBySlugAndType(string $slug, AssetType $type): string
{
$extSystemConfig = $this->extSystemConfigurationProvider->getAssetConfiguration($slug, $type);

return $extSystemConfig->getStorageName();
}

public function getStorageNameByStorable(FileSystemStorableInterface $storable): string
{
$extSystemConfig = $this->extSystemConfigurationProvider->getAssetConfiguration(
Expand Down
2 changes: 2 additions & 0 deletions src/Image/ImageManipulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public function getStream(string $extension);
* @throws ImageManipulatorException
*/
public function applyFilterStack(FilterStack $filterStack): void;

public function clean(bool $clean = true): void;
}
Loading

0 comments on commit 674bb8d

Please sign in to comment.