[11/06/2024] Les premières modifs + installation de quelques modules indispensables

This commit is contained in:
2024-06-11 14:57:59 +02:00
parent 5ac5653ae5
commit 77cf2c7cc6
1626 changed files with 171457 additions and 131 deletions

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Tools\URL;
use TheliaLibrary\Service\ImageService;
/**
* @Route("/image-library", name="image_library_")
*/
class ImageController extends BaseFrontController
{
/**
* @Route("/{identifier}/{region}/{size}/{rotation}/{quality}.{format}", name="view")
*/
public function getImage(
$identifier,
$region,
$size,
$rotation,
$quality,
$format,
ImageService $imageService
) {
$imagePath = $imageService->geFormattedImage($identifier, $region, $size, $rotation, $quality, $format);
return new BinaryFileResponse($imagePath);
}
/**
* @Route("/{identifier}/info.json", name="info")
*/
public function getImageInformation(
$identifier,
ImageService $imageService
): Response {
$image = $imageService->openImage($identifier);
$size = $image->getSize();
$maxSize = $imageService->getMaxSize($image);
return new Response(
json_encode(
[
'@context' => 'http://iiif.io/api/image/3/context.json',
'id' => URL::getInstance()->absoluteUrl('image-library/'.$identifier),
'type' => 'ImageService3',
'protocol' => 'http://iiif.io/api/image',
'profile' => 'level2',
'width' => $size->getWidth(),
'height' => $size->getHeight(),
'maxWidth' => $maxSize->getWidth(),
'maxHeight' => $maxSize->getHeight(),
]
),
200,
[
'Content-Type' => 'application/ld+json;profile="http://iiif.io/api/image/3/context.json"',
]
);
}
}

View File

@@ -0,0 +1,148 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Model\ConfigQuery;
use Thelia\Model\ProductImage;
use Thelia\Tools\URL;
use TheliaLibrary\Service\ImageService;
use TheliaMain\PropelResolver;
/**
* @Route("/legacy-image-library", name="legacy_image_library_")
*/
class LegacyImageController extends BaseFrontController
{
/**
* @Route("/{itemType}_image_{imageId}/{region}/{size}/{rotation}/{quality}.{format}", name="view")
*/
public function getImage(
$itemType,
$imageId,
$region,
$size,
$rotation,
$quality,
$format,
ImageService $imageService
) {
$sourceFilePath = $this->getSourceFilePath($itemType, $imageId);
if (!\in_array(strtolower($format), ['jpg', 'jpeg', 'png', 'gif', 'jp2', 'webp'])) {
throw new HttpException(400, 'Bad format value');
}
$formattedImagePath = THELIA_WEB_DIR.'legacy-image-library'.DS.$itemType.'_image_'.$imageId.DS.$region.DS.$size.DS.$rotation;
if (!is_dir($formattedImagePath)) {
if (!@mkdir($formattedImagePath, 0755, true)) {
throw new \RuntimeException(sprintf('Failed to create %s file in cache directory', $formattedImagePath));
}
}
$formattedImagePath .= DS.$quality.'.'.$format;
if (file_exists($formattedImagePath)) {
return new BinaryFileResponse($formattedImagePath);
}
$image = $imageService->getImagineInstance()->open($sourceFilePath);
$image = $imageService->applyRegion($image, $region);
$image = $imageService->applySize($image, $size);
$image = $imageService->applyRotation($image, $rotation, $format);
$image = $imageService->applyQuality($image, $quality);
$image->save($formattedImagePath);
return new BinaryFileResponse($formattedImagePath);
}
/**
* @Route("/{itemType}_image_{imageId}/info.json", name="info")
*/
public function getImageInformation(
$itemType,
$imageId,
ImageService $imageService
): Response {
$sourceFilePath = $this->getSourceFilePath($itemType, $imageId);
$image = $imageService->getImagineInstance()->open($sourceFilePath);
$size = $image->getSize();
$maxSize = $imageService->getMaxSize($image);
return new Response(
json_encode(
[
'@context' => 'http://iiif.io/api/image/3/context.json',
'id' => URL::getInstance()->absoluteUrl('legacy-image-library/'.$itemType.'_image_'.$imageId),
'type' => 'ImageService3',
'protocol' => 'http://iiif.io/api/image',
'profile' => 'level2',
'width' => $size->getWidth(),
'height' => $size->getHeight(),
'maxWidth' => $maxSize->getWidth(),
'maxHeight' => $maxSize->getHeight(),
]
),
200,
[
'Content-Type' => 'application/ld+json;profile="http://iiif.io/api/image/3/context.json"',
]
);
}
private function getSnakeFromCamelCase($camelCase): string
{
$pattern = '/(?<=\\w)(?=[A-Z])|(?<=[a-z])(?=[0-9])/';
$snakeCase = preg_replace($pattern, '_', $camelCase);
return strtolower($snakeCase);
}
private function getSourceFilePath($itemType, $imageId)
{
$tableMapClass = PropelResolver::getTableMapByTableName($this->getSnakeFromCamelCase($itemType));
$tableMap = new $tableMapClass();
/** @var ModelCriteria $queryClass */
$queryClass = $tableMap->getClassName().'ImageQuery';
/** @var ProductImage $image */
$image = $queryClass::create()
->filterById($imageId)
->filterByVisible(1)
->findOne();
if (null === $image) {
return new Response(null, 404);
}
$baseSourceFilePath = ConfigQuery::read('images_library_path');
if ($baseSourceFilePath === null) {
$baseSourceFilePath = THELIA_LOCAL_DIR.'media'.DS.'images';
} else {
$baseSourceFilePath = THELIA_ROOT.$baseSourceFilePath;
}
return sprintf(
'%s/%s/%s',
$baseSourceFilePath,
$itemType,
$image->getFile()
);
}
}

View File

@@ -0,0 +1,224 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front\OpenApi;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Front\BaseFrontOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\LibraryImage;
use TheliaLibrary\Model\LibraryImageQuery;
use TheliaLibrary\Model\LibraryItemImageQuery;
/**
* @Route("/open_api/library/image", name="front_library_image")
*/
class ImageController extends BaseFrontOpenApiController
{
/**
* @Route("", name="_get", methods="GET")
*
* @OA\Get(
* path="/library/image",
* tags={"Library image"},
* summary="Get images",
* @OA\Parameter(
* name="id",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemType",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="title",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="code",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="onlyVisible",
* in="query",
* @OA\Schema(
* type="boolean",
* default="true"
* )
* ),
* @OA\Parameter(
* name="offset",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="limit",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="tagId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="width",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="height",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="locale",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getImage(
Request $request,
ModelFactory $modelFactory
) {
$locale = $this->findLocale($request);
$imageQuery = LibraryImageQuery::create()->orderById(Criteria::DESC);
if (null !== $id = $request->get('id')) {
$imageQuery->filterById($id);
}
if (null !== $title = $request->get('title')) {
$imageQuery->useLibraryImageI18nQuery()
->filterByLocale($locale)
->filterByTitle("%$title%", Criteria::LIKE)
->endUse();
}
$itemImageQuery = null;
if (null !== $itemId = $request->get('itemId')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByItemId($itemId);
}
if (null !== $itemType = $request->get('itemType')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByItemType($itemType);
}
if (null !== $code = $request->get('code')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByCode($code);
}
if (true === $request->get('onlyVisible')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByVisible(true);
}
if (null !== $itemImageQuery) {
$itemImageQuery->orderByPosition();
$itemImageQuery->endUse();
}
if (null !== $tagId = $request->get('tagId')) {
$itemImageQuery = $imageQuery->useLibraryImageTagQuery()->filterByTagId($tagId)->endUse();
}
if (null !== $limit = $request->get('limit', 20)) {
$imageQuery->limit($limit);
}
if (null !== $offset = $request->get('offset', 0)) {
$imageQuery->offset($offset);
}
$width = $request->get('width');
$height = $request->get('height');
return OpenApiService::jsonResponse(array_map(
function (LibraryImage $image) use ($modelFactory, $locale, $width, $height) {
/** @var \TheliaLibrary\Model\Api\LibraryImage $imageModel */
$imageModel = $modelFactory->buildModel('LibraryImage', $image, $locale);
$imageModel->setWidth($width);
$imageModel->setHeight($height);
return $imageModel;
},
iterator_to_array($imageQuery->find())
));
}
protected function getOrInitItemJoin($query, $itemImageQuery = null): LibraryItemImageQuery
{
if (null !== $itemImageQuery) {
return $itemImageQuery;
}
return $query->useLibraryItemImageQuery();
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getLang()->getLocale();
}
return $locale;
}
}

View File

@@ -0,0 +1,197 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front\OpenApi;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Front\BaseFrontOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\LibraryItemImage;
use TheliaLibrary\Model\LibraryItemImageQuery;
/**
* @Route("/open_api/library/item_image", name="front_library_item_image")
*/
class ItemImageController extends BaseFrontOpenApiController
{
/**
* @Route("", name="_get", methods="GET")
*
* @OA\Get(
* path="/library/item_image",
* tags={"Library image"},
* summary="Get item images association",
* @OA\Parameter(
* name="itemId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemType",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="code",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="offset",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="limit",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryItemImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getItemImage(
Request $request,
ModelFactory $modelFactory
) {
$locale = $this->findLocale($request);
$itemImageQuery = LibraryItemImageQuery::create();
if (null !== $imageId = $request->get('imageId')) {
$itemImageQuery->filterByImageId($imageId);
}
if (null !== $itemType = $request->get('itemType')) {
$itemImageQuery->filterByItemType($itemType);
}
if (null !== $itemId = $request->get('itemId')) {
$itemImageQuery->filterByItemId($itemId);
}
if (null !== $code = $request->get('code')) {
$itemImageQuery->filterByCode($code);
}
if (null !== $limit = $request->get('limit', 20)) {
$itemImageQuery->limit($limit);
}
if (null !== $offset = $request->get('offset', 0)) {
$itemImageQuery->offset($offset);
}
$itemImageQuery->orderByPosition();
return OpenApiService::jsonResponse(array_map(
function (LibraryItemImage $itemImage) use ($modelFactory, $locale) {
return $modelFactory->buildModel('LibraryItemImage', $itemImage, $locale);
},
iterator_to_array($itemImageQuery->find())
));
}
/**
* @Route("/types", name="_type_list", methods="GET")
*
* @OA\Get(
* path="/library/item_image/types",
* tags={"Library image"},
* summary="Get all item types availables",
* @OA\Parameter(
* name="onlyExisting",
* description="If false basic Thelia types will be added (product, content, ...) even if they have no image associated",
* in="query",
* @OA\Schema(
* type="boolean",
* default="false"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(
* type="array",
* @OA\Items(
* type="string",
* )
* )
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getItemTypes(
Request $request
) {
$itemTypes = array_map(
function (LibraryItemImage $libraryItemImage) {
return $libraryItemImage->getItemType();
},
iterator_to_array(LibraryItemImageQuery::create()
->groupByItemType()
->find())
);
if (false === $request->get('onlyExisting', false) || 'false' === $request->get('onlyExisting', false)) {
$itemTypes = array_merge(
[
'product',
'category',
'content',
'folder',
],
$itemTypes
);
}
return OpenApiService::jsonResponse(
$itemTypes
);
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getAdminEditionLang()->getLocale();
}
return $locale;
}
}