Skip to content

Commit

Permalink
Merge branch 'b-7.1.x-allow-svg-upload-OEVE-211' into b-7.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Sieg committed Mar 8, 2024
2 parents 31a58df + 0513b6a commit 785ef93
Show file tree
Hide file tree
Showing 30 changed files with 879 additions and 327 deletions.
1 change: 1 addition & 0 deletions assets/out/src/img/default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imports:
- { resource: src/Breadcrumb/services.yaml }
- { resource: src/Image/Service/services.yaml }
- { resource: src/Image/ThumbnailGenerator/services.yaml }
- { resource: src/Language/Core/services.yaml }
- { resource: src/Media/services.yaml }
- { resource: src/Transput/services.yaml }
Expand Down
14 changes: 14 additions & 0 deletions src/Image/Exception/AggregatorInputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\Exception;

class AggregatorInputType extends \Exception
{
}
14 changes: 14 additions & 0 deletions src/Image/Exception/NoSupportedDriversForSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\Exception;

class NoSupportedDriversForSource extends \Exception
{
}
43 changes: 43 additions & 0 deletions src/Image/Service/ThumbnailGeneratorAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\Service;

use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSizeInterface;
use OxidEsales\MediaLibrary\Image\Exception\AggregatorInputType;
use OxidEsales\MediaLibrary\Image\Exception\NoSupportedDriversForSource;
use OxidEsales\MediaLibrary\Image\ThumbnailGenerator\ThumbnailGeneratorInterface;

class ThumbnailGeneratorAggregate implements ThumbnailGeneratorAggregateInterface
{
/**
* @param iterable<ThumbnailGeneratorInterface> $thumbnailGenerators
* @throws AggregatorInputType
*/
public function __construct(
protected iterable $thumbnailGenerators
) {
foreach ($this->thumbnailGenerators as $oneGenerator) {
if (!$oneGenerator instanceof ThumbnailGeneratorInterface) {
throw new AggregatorInputType();
}
}
}

public function getSupportedGenerator(string $sourcePath): ThumbnailGeneratorInterface
{
foreach ($this->thumbnailGenerators as $oneDriver) {
if ($oneDriver->isOriginSupported($sourcePath)) {
return $oneDriver;
}
}

throw new NoSupportedDriversForSource();
}
}
17 changes: 17 additions & 0 deletions src/Image/Service/ThumbnailGeneratorAggregateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\Service;

use OxidEsales\MediaLibrary\Image\ThumbnailGenerator\ThumbnailGeneratorInterface;

interface ThumbnailGeneratorAggregateInterface
{
public function getSupportedGenerator(string $sourcePath): ThumbnailGeneratorInterface;
}
22 changes: 0 additions & 22 deletions src/Image/Service/ThumbnailGeneratorInterface.php

This file was deleted.

44 changes: 0 additions & 44 deletions src/Image/Service/ThumbnailGeneratorIntervention.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Image/Service/ThumbnailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ public function __construct(
) {
}

private function getThumbnailFileHash(string $originalFilename): string
{
return md5($originalFilename);
}

public function getThumbnailFileName(
string $originalFileName,
ImageSizeInterface $thumbnailSize,
bool $crop
): string {
return sprintf(
'%s_thumb_%d*%d%s%s',
$this->getThumbnailFileHash($originalFileName),
$thumbnailSize->getWidth(),
$thumbnailSize->getHeight(),
$crop ? '' : '_nocrop',
'.jpg'
);
}

public function getDefaultThumbnailSize(): ImageSizeInterface
{
return new ImageSize(width: self::THUMBNAIL_DEFAULT_SIZE, height: self::THUMBNAIL_DEFAULT_SIZE);
Expand All @@ -62,9 +42,4 @@ public function getUrlToThumbnailFiles(string $folderName = ''): string
self::THUMBNAIL_DIRECTORY
);
}

public function getThumbnailsGlob(string $originalFilename): string
{
return $this->getThumbnailFileHash($originalFilename) . '*.*';
}
}
8 changes: 0 additions & 8 deletions src/Image/Service/ThumbnailResourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@

interface ThumbnailResourceInterface
{
public function getThumbnailFileName(
string $originalFileName,
ImageSizeInterface $thumbnailSize,
bool $crop
): string;

public function getDefaultThumbnailSize(): ImageSizeInterface;

public function getPathToThumbnailFiles(string $folderName): string;

public function getUrlToThumbnailFiles(string $folderName): string;

public function getThumbnailsGlob(string $originalFilename): string;
}
18 changes: 11 additions & 7 deletions src/Image/Service/ThumbnailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ class ThumbnailService implements ThumbnailServiceInterface
public function __construct(
protected ThumbnailResourceInterface $thumbnailResource,
protected FileSystemServiceInterface $fileSystemService,
protected ThumbnailGeneratorInterface $thumbnailGenerator,
protected ThumbnailGeneratorAggregateInterface $thumbnailGeneratorAggregate,
protected MediaResourceInterface $mediaResource,
) {
}

public function deleteMediaThumbnails(MediaInterface $media): void
{
$thumbnailGenerator = $this->thumbnailGeneratorAggregate->getSupportedGenerator($media->getFileName());

$this->fileSystemService->deleteByGlob(
inPath: $this->thumbnailResource->getPathToThumbnailFiles($media->getFolderName()),
globTargetToDelete: $this->thumbnailResource->getThumbnailsGlob($media->getFileName())
globTargetToDelete: $thumbnailGenerator->getThumbnailsGlob($media->getFileName())
);
}

Expand All @@ -39,21 +41,23 @@ public function ensureAndGetThumbnailUrl(
ImageSizeInterface $imageSize = null,
bool $crop = true
): string {
$thumbnailFileName = $this->thumbnailResource->getThumbnailFileName(
$thumbnailGenerator = $this->thumbnailGeneratorAggregate->getSupportedGenerator($fileName);

$thumbnailFileName = $thumbnailGenerator->getThumbnailFileName(
originalFileName: $fileName,
thumbnailSize: $imageSize ?? $this->thumbnailResource->getDefaultThumbnailSize(),
crop: $crop
isCropRequired: $crop
);

$thumbnailDirectoryPath = $this->thumbnailResource->getPathToThumbnailFiles($folderName);
$thumbnailPath = Path::join($thumbnailDirectoryPath, $thumbnailFileName);
if (!is_file($thumbnailPath)) {
$this->fileSystemService->ensureDirectory($thumbnailDirectoryPath);
$this->thumbnailGenerator->generateThumbnail(
$thumbnailGenerator->generateThumbnail(
sourcePath: Path::join($this->mediaResource->getPathToMediaFiles($folderName), $fileName),
thumbnailPath: $thumbnailPath,
size: $imageSize ?? $this->thumbnailResource->getDefaultThumbnailSize(),
blCrop: $crop
thumbnailSize: $imageSize ?? $this->thumbnailResource->getDefaultThumbnailSize(),
isCropRequired: $crop
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Image/Service/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ services:
autowire: true
public: false

Intervention\Image\ImageManager:
class: Intervention\Image\ImageManager
arguments:
$driver: 'Intervention\Image\Drivers\Gd\Driver'

OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorInterface:
class: OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorIntervention
OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorAggregateInterface:
class: OxidEsales\MediaLibrary\Image\Service\ThumbnailGeneratorAggregate
public: true
arguments:
$thumbnailGenerators:
- '@OxidEsales\MediaLibrary\Image\ThumbnailGenerator\SvgDriver'
- '@OxidEsales\MediaLibrary\Image\ThumbnailGenerator\InterventionDriver'
- '@OxidEsales\MediaLibrary\Image\ThumbnailGenerator\DefaultDriver'

OxidEsales\MediaLibrary\Image\Service\ThumbnailResourceInterface:
class: OxidEsales\MediaLibrary\Image\Service\ThumbnailResource
Expand Down
42 changes: 42 additions & 0 deletions src/Image/ThumbnailGenerator/DefaultDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\ThumbnailGenerator;

use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSizeInterface;

class DefaultDriver implements ThumbnailGeneratorInterface
{
public function isOriginSupported(string $sourcePath): bool
{
return true;
}

public function generateThumbnail(
string $sourcePath,
string $thumbnailPath,
ImageSizeInterface $thumbnailSize,
bool $isCropRequired,
): void {
copy(__DIR__ . '/../../../assets/out/src/img/default.svg', $thumbnailPath);
}

public function getThumbnailFileName(
string $originalFileName,
ImageSizeInterface $thumbnailSize,
bool $isCropRequired
): string {
return 'default.svg';
}

public function getThumbnailsGlob(string $originalFilename): string
{
return 'default.svg';
}
}
Loading

0 comments on commit 785ef93

Please sign in to comment.