Browse Source

add the new ImageService

Olivier Massot 1 năm trước cách đây
mục cha
commit
92054804d3

+ 8 - 0
icon.svg

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
+<svg width="800px" height="800px" viewBox="-2 0 260 260" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
+    <g>
+        <path d="M109.525333,4.05333333 C104.810667,8.08533333 101.461333,12.8 101.461333,26.88 C101.461333,65.1946667 149.824,180.288 182.762667,180.288 C186.462268,180.338187 190.147176,179.812799 193.685333,178.730667 L193.616,178.75 L192.774258,180.100166 C164.346546,225.411559 130.133077,258.650903 109.429541,259.317782 L108.8,259.328 C63.8293333,259.328 0,123.562667 0,63.8293333 C0,54.4213333 2.13333333,47.04 5.376,42.4533333 C20.8426667,23.552 69.2053333,8.74666667 109.525333,4.05333333 Z M172.672,0 C214.314667,0 256,6.72 256,30.2293333 C256,77.9306667 225.749333,135.744 210.304,135.744 C182.762667,135.744 148.437333,59.136 148.437333,20.8213333 C148.437333,3.34933333 155.136,0 172.608,0 L172.672,0 Z" fill="#F49700">
+
</path>
+    </g>
+</svg>

+ 7 - 4
ot_core/Classes/Service/OpentalentApiService.php

@@ -57,11 +57,12 @@ class OpentalentApiService
      * Return the API URI for the current repository
      *
      * @param string $trailing_part
+     * @param bool $public Si vrai, retourne l'url publique et non interne
      * @return string
      */
-    public function getApiUri(string $trailing_part = ""): string
+    public function getApiUri(string $trailing_part = "", bool $public = false): string
     {
-        $uri = OpentalentEnvService::get('API_BASE_URI');
+        $uri = OpentalentEnvService::get($public ? 'PUBLIC_API_BASE_URI' : 'API_BASE_URI');
         return rtrim($uri, '/') . '/' . ltrim($trailing_part, '/');
     }
 
@@ -95,15 +96,17 @@ class OpentalentApiService
      *
      * @param string $uri
      * @param array $params
+     * @param array $options
      * @return string
      * @throws ApiRequestException
      */
     public function getBody(
         string $uri,
-        array $params = []
+        array $params = [],
+        array $options = []
     ): string
     {
-        return (string)$this->get($uri, $params)->getBody();
+        return (string)$this->get($uri, $params, $options)->getBody();
     }
 
     /**

+ 1 - 1
ot_core/Classes/Service/OpentalentEnvService.php

@@ -18,7 +18,7 @@ class OpentalentEnvService
 {
     public static function get($varname) {
         if (!array_key_exists($varname, $GLOBALS['OT'])) {
-            throw new \RuntimeException('Un-existing OT environment variable requested: ' . $varname);
+            throw new \RuntimeException('Non-existing OT environment variable requested: ' . $varname);
         }
         return $GLOBALS['OT'][$varname];
     }

+ 28 - 0
ot_core/Classes/Service/OpentalentImageService.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Opentalent\OtCore\Service;
+
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+class OpentalentImageService
+{
+    public const IMAGE_SM = 'sm';
+    public const IMAGE_MD = 'md';
+    public const IMAGE_LG = 'lg';
+
+    public function __construct(
+        OpentalentApiService $apiService = null
+    )
+    {
+        $this->apiService = $apiService ?? GeneralUtility::makeInstance(OpentalentApiService::class);
+    }
+
+    public function getImageUrl(int $fileId, string $size = self::IMAGE_MD): string {
+        if (!in_array($size, [self::IMAGE_SM, self::IMAGE_MD, self::IMAGE_LG])) {
+            throw new \RuntimeException('Invalid $size : ' . $size);
+        }
+
+        $path = $this->apiService->getBody("api/public/files/$fileId/download/$size", ['relativePath' => true]);
+        return $this->apiService->getApiUri($path, true);
+    }
+}

+ 1 - 0
ot_core/ext_localconf.php

@@ -35,6 +35,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][TYPO3\CMS\Backend\Middleware\Backe
 // (/!\ do not forget to clear the cache after any update here)
 $GLOBALS['OT'] = array_merge([
     'API_BASE_URI' => 'http://docker.nginx.opentalent.fr',
+    'PUBLIC_API_BASE_URI' => 'https://local.api.opentalent.fr',
     'DB_HOST' => 'db',
     'DB_USER' => 'dbcloner',
     'DB_PASSWORD' => 'wWZ4hYcrmHLW2mUK',

+ 72 - 0
ot_templating/Classes/ViewHelpers/GetImageUrlViewHelper.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace Opentalent\OtTemplating\ViewHelpers;
+
+
+use Closure;
+use Opentalent\OtCore\Service\OpentalentImageService;
+use Opentalent\OtCore\Website\OtPageRepository;
+use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
+use Opentalent\OtTemplating\ViewHelpers\RootPage\GetIdViewHelper;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
+
+/**
+ * Returns the url of the requested image
+ *
+ *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
+ *
+ *     {ot:getImageUrl(fileId: 123, size: 'md')}
+ *
+ * @see ot_core/Classes/Service/OpentalentImageService.php
+ * @package Opentalent\OtTemplating\ViewHelpers
+ */
+class GetImageUrlViewHelper extends OtAbstractViewHelper
+{
+    public OpentalentImageService $opentalentImageService;
+
+    /**
+     * @param OpentalentImageService $opentalentImageService
+     * @return void
+     */
+    public function injectOpentalentImageService(OpentalentImageService $opentalentImageService)
+    {
+        $this->opentalentImageService = $opentalentImageService;
+    }
+
+    /**
+     * -- This method is expected by Fluid --
+     * Declares the viewhelper's parameters
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument(
+            'fileId',
+            'int',
+            'The id of the File object',
+            true
+        );
+        $this->registerArgument(
+            'size',
+            'string',
+            'The expected size (sm, md, or lg)',
+            false,
+            'md'
+        );
+    }
+
+    /**
+     * -- This method is expected by Fluid --
+     * Renders the content as html
+     *
+     * @return string Rendered tag
+     */
+    public function render() {
+        $fileId = $this->arguments['fileId'];
+        $size = $this->arguments['size'];
+        return $this->opentalentImageService->getImageUrl($fileId, $size);
+    }
+
+}

+ 1 - 1
ot_templating/Resources/Private/Partials/Modern/Menu.html

@@ -25,7 +25,7 @@
                                     <f:then>
                                         <a href="{ot:rootPage.getUri()}" title="{settings.structureName}">
                                             <img id="logo_img"
-                                                 src="{ot:request.getOtEnvVar(argument: 'FILE_STORAGE_URL')}{settings.structureLogoId}"
+                                                 src="{ot:getImageUrl(fileId: settings.structureLogoId, size: 'sm')}"
                                                  alt="{settings.structureName}"/>
                                         </a>
                                     </f:then>