Skip to content

Commit

Permalink
OXDEV-7356 Extract the ImageResource from Media
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
RahatHameed authored and Sieg committed Sep 27, 2023
1 parent c91f409 commit 989abc1
Show file tree
Hide file tree
Showing 19 changed files with 1,083 additions and 645 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"squizlabs/php_codesniffer": "3.*",
"phpstan/phpstan": "^1.8.11",
"phpunit/phpunit": "^9.6",
"oxid-esales/oxideshop-ce": "dev-b-7.1.x"
"oxid-esales/oxideshop-ce": "dev-b-7.1.x",
"mikey179/vfsstream": "~1.6.8"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
imports:
- { resource: src/Thumbnail/Service/services.yaml }
- { resource: src/Image/Service/services.yaml }

services:

Expand Down
31 changes: 17 additions & 14 deletions src/Application/Controller/Admin/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface;
use OxidEsales\MediaLibrary\Service\Media;
use Symfony\Component\Filesystem\Path;

Expand All @@ -18,6 +19,7 @@
class MediaController extends AdminDetailsController
{
protected ?Media $mediaService = null;
protected ?ImageResourceInterface $imageResource = null;

/**
* Current class template name.
Expand All @@ -43,13 +45,14 @@ public function init()
$this->_sFolderId = Registry::getRequest()->getRequestEscapedParameter('folderid');
}

$this->mediaService = $this->getService(Media::class);
$this->mediaService = $this->getService('OxidEsales\MediaLibrary\Service\Media');
$this->imageResource = $this->getService('OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface');

$this->mediaService->setFolder($this->_sFolderId);
$this->imageResource->setFolder($this->_sFolderId);

$this->_sUploadDir = $this->mediaService->getMediaPath();
$this->_sThumbDir = $this->mediaService->getThumbnailPath();
$this->_iDefaultThumbnailSize = $this->mediaService->getDefaultThumbnailSize();
$this->_sUploadDir = $this->imageResource->getMediaPath();
$this->_sThumbDir = $this->imageResource->getThumbnailPath();
$this->_iDefaultThumbnailSize = $this->imageResource->getDefaultThumbnailSize();
}

/**
Expand All @@ -64,10 +67,10 @@ public function render()

$this->_aViewData['aFiles'] = $this->mediaService->getFiles(0, $iShopId);
$this->_aViewData['iFileCount'] = $this->mediaService->getFileCount($iShopId);
$this->_aViewData['sResourceUrl'] = $this->mediaService->getMediaUrl();
$this->_aViewData['sThumbsUrl'] = $this->mediaService->getThumbnailUrl();
$this->_aViewData['sFoldername'] = $this->mediaService->getFolderName();
$this->_aViewData['sFolderId'] = $this->_sFolderId;
$this->_aViewData['sResourceUrl'] = $this->imageResource->getMediaUrl();
$this->_aViewData['sThumbsUrl'] = $this->imageResource->getThumbnailUrl();
$this->_aViewData['sFoldername'] = $this->imageResource->getFolderName();
$this->_aViewData['sFolderId'] = $this->imageResource->getFolderId();
$this->_aViewData['sTab'] = Registry::getRequest()->getRequestEscapedParameter('tab');

$request = Registry::getRequest();
Expand Down Expand Up @@ -107,7 +110,7 @@ public function upload()
$sFileType = $_FILES['file']['type'];

$sSourcePath = $_FILES['file']['tmp_name'];
$sDestPath = Path::join($this->mediaService->getMediaPath(), $_FILES['file']['name']);
$sDestPath = Path::join($this->imageResource->getMediaPath(), $_FILES['file']['name']);

$aResult = $this->mediaService->uploadMedia($sSourcePath, $sDestPath, $sFileSize, $sFileType, true);
$sId = $aResult['id'];
Expand Down Expand Up @@ -243,7 +246,7 @@ public function rename()
'msg' => $sMsg,
'name' => $sNewName,
'id' => $sNewId,
'thumb' => $this->mediaService->getThumbnailUrl($sNewName),
'thumb' => $this->imageResource->getThumbnailUrl($sNewName),
]
);
die($sReturn);
Expand Down Expand Up @@ -320,14 +323,14 @@ public function getBreadcrumb()
$aBreadcrumb = [];

$oPath = new \stdClass();
$oPath->active = ($this->mediaService->getFolderName() ? false : true);
$oPath->active = ($this->imageResource->getFolderName() ? false : true);
$oPath->name = 'Root';
$aBreadcrumb[] = $oPath;

if ($this->mediaService->getFolderName()) {
if ($this->imageResource->getFolderName()) {
$oPath = new \stdClass();
$oPath->active = true;
$oPath->name = $this->mediaService->getFolderName();
$oPath->name = $this->imageResource->getFolderName();
$aBreadcrumb[] = $oPath;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Core/ViewConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class ViewConfig extends ViewConfig_parent
{
public function getMediaUrl($sFile = '')
{
$oMedia = $this->getService(\OxidEsales\MediaLibrary\Service\Media::class);

return $oMedia->getMediaUrl($sFile);
$imageResource = $this->getService('OxidEsales\MediaLibrary\Image\Service\ImageResourceInterface');
return $imageResource->getMediaUrl($sFile);
}
}
27 changes: 27 additions & 0 deletions src/Image/DataTransfer/ImageSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Image\DataTransfer;

class ImageSize implements ImageSizeInterface
{
public function __construct(private readonly int $width, private readonly int $height)
{
}

public function getWidth(): int
{
return $this->width;
}

public function getHeight(): int
{
return $this->height;
}
}
9 changes: 9 additions & 0 deletions src/Image/DataTransfer/ImageSizeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OxidEsales\MediaLibrary\Image\DataTransfer;

interface ImageSizeInterface
{
public function getWidth(): int;
public function getheight(): int;
}
224 changes: 224 additions & 0 deletions src/Image/Service/ImageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

namespace OxidEsales\MediaLibrary\Image\Service;

use Doctrine\DBAL\Connection;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSize;
use OxidEsales\MediaLibrary\Image\DataTransfer\ImageSizeInterface;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\MediaLibrary\Service\ModuleSettings;
use Symfony\Component\Filesystem\Path;

class ImageResource implements ImageResourceInterface
{
protected int $defaultThumbnailSize = 185;
public const MEDIA_PATH = '/out/pictures/ddmedia/';
public const MEDIA_PATH_SHORT = '/ddmedia/';
public string $_sFolderName = '';
protected Connection $connection;
public string $_sFolderId = '';

public function __construct(
protected Config $shopConfig,
protected ModuleSettings $moduleSettings,
protected ThumbnailGeneratorInterface $thumbnailGenerator,
ConnectionProviderInterface $connectionProvider,
) {
$this->connection = $connectionProvider->get();
}


public function getFolderName(): string
{
return $this->_sFolderName;
}

public function setFolderName($sFolderName): void
{
$this->_sFolderName = $sFolderName;
}

/**
* @deprecated This is temporary solution, it should be removed in next release.
*/
public function getFolderId(): string
{
return $this->_sFolderId;
}

/**
* @param $sId
*
* @return void
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
*/
private function setFolderNameForFolderId($sId)
{
$iShopId = $this->shopConfig->getActiveShop()->getShopId();

$sSelect = "SELECT `DDFILENAME` FROM `ddmedia` WHERE `OXID` = ? AND `DDFILETYPE` = ? AND `OXSHOPID` = ?";
$folderName = $this->connection->fetchOne($sSelect, [$sId, 'directory', $iShopId]);
$sFolderName = $folderName ?: '';

if ($sFolderName) {
$this->setFolderName($sFolderName);
}
}

public function setFolder($sFolderId = ''): void
{
$this->_sFolderId = $sFolderId;
if ($sFolderId) {
$this->setFolderNameForFolderId($sFolderId);
}
}

public function getDefaultThumbnailSize(): int
{
return $this->defaultThumbnailSize;
}

public function getThumbName($sFile, ?ImageSizeInterface $imageSize = null, $thumbnailCrop = true): string
{
$imageSize = $this->getDefaultImageSizeIfNotProvided($imageSize);
$imageSizeString = sprintf(
'%d*%d%s%s',
$imageSize->getWidth(),
$imageSize->getHeight(),
$thumbnailCrop ? '' : '_nocrop',
'.jpg'
);
return str_replace('.', '_', md5(basename($sFile))) . '_thumb_' . $imageSizeString;
}

public function getThumbnailUrl($sFile = '', ?ImageSizeInterface $imageSize = null, $thumbnailCrop = true): string
{
if (!$sFile) {
return $this->getMediaUrl('thumbs/');
}

$imageSize = $this->getDefaultImageSizeIfNotProvided($imageSize);
$sThumbName = $this->getThumbName($sFile, $imageSize, $thumbnailCrop);
if (!is_file($this->getThumbnailPath($sThumbName))) {
$sThumbName = $this->createThumbnail($sFile, $imageSize, $thumbnailCrop);
}
if (is_file($this->getThumbnailPath($sThumbName))) {
return $this->getMediaUrl('thumbs/' . $sThumbName);
}

return false;
}

public function getThumbnailPath($filename = ''): string
{
return Path::join($this->getMediaPath(), 'thumbs', $filename);
}

/**
* todo: exception in place of bool response
*/
public function getMediaUrl($filename = '')
{
$filepath = $this->getMediaPath($filename);

if ($this->isAlternativeImageUrlConfigured()) {
return $filepath;
}

if (!is_readable($filepath)) {
return false;
}

if (strpos($filename, 'thumbs/') === false) {
$filename = basename($filename);
}

return Path::join(
$this->shopConfig->getSslShopUrl(),
self::MEDIA_PATH,
(isset($this->_sFolderName) ? $this->_sFolderName . '/' : ''),
$filename
);
}

public function getMediaPath($filename = '', $blDoNotSetFolder = false): string
{
if (!$blDoNotSetFolder) {
$this->checkAndSetFolderName($filename);
}

$sPath = $this->getPathToMediaFiles() . '/' . ($this->_sFolderName ? $this->_sFolderName . '/' : '');

if ($filename) {
return $sPath . (strpos($filename, 'thumbs/') !== false ? $filename : basename($filename));
}

return $sPath;
}

public function createThumbnail($sFileName, ?ImageSizeInterface $imageSize = null, $thumbnailCrop = true)
{
$sFilePath = $this->getMediaPath($sFileName, true);
if (is_readable($sFilePath)) {
$imageSize = $this->getDefaultImageSizeIfNotProvided($imageSize);
$sThumbName = $this->getThumbName($sFileName, $imageSize, $thumbnailCrop);
$thumbnailPath = $this->getThumbnailPath($sThumbName);
$this->thumbnailGenerator->generateThumbnail($sFilePath, $thumbnailPath, $imageSize, $thumbnailCrop);

return $sThumbName;
}

return false;
}

private function isAlternativeImageUrlConfigured(): bool
{
return (bool)$this->getAlternativeImageUrl();
}

/**
* @param $sFile
*/
protected function checkAndSetFolderName($sFile)
{
if ($sFile) {
if (($iPos = strpos($sFile, '/')) !== false) {
$folderName = substr($sFile, 0, $iPos);
if ($folderName != 'thumbs') {
$this->_sFolderName = substr($sFile, 0, $iPos);
}
} else {
$this->_sFolderName = '';
}
}
}

private function getPathToMediaFiles(): string
{
$basePath = '';
if ($this->isAlternativeImageUrlConfigured()) {
$basePath = $this->getAlternativeImageUrl();
$mediaPath = self::MEDIA_PATH_SHORT;
} else {
$basePath = $this->shopConfig->getConfigParam('sShopDir');
$mediaPath = self::MEDIA_PATH;
}

return Path::join($basePath, $mediaPath);
}

private function getAlternativeImageUrl(): string
{
return $this->moduleSettings->getAlternativeImageDirectory();
}

private function getDefaultImageSizeIfNotProvided(?ImageSizeInterface $imageSize = null): ImageSizeInterface
{
if (!$imageSize instanceof ImageSizeInterface) {
$iSize = $this->getDefaultThumbnailSize();
$imageSize = new ImageSize($iSize, $iSize);
}
return $imageSize;
}
}
Loading

0 comments on commit 989abc1

Please sign in to comment.