Browse Source

manage error on missing image file and return missing-file.png

Olivier Massot 1 year ago
parent
commit
55aa498c15

+ 4 - 0
.env

@@ -22,6 +22,10 @@ APP_SECRET=6a76497c8658bb23e2236f97a2627df3
 # => defined in the .env.<environment> files
 ###< doctrine/doctrine-bundle ###
 
+###> url v2 ###
+PUBLIC_API_BASE_URL=https://local.ap2i.opentalent.fr
+###
+
 ###> lexik/jwt-authentication-bundle ###
 JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
 JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem

BIN
public/images/missing-file.png


+ 1 - 1
src/Service/File/Storage/ApiLegacyStorage.php

@@ -43,7 +43,7 @@ class ApiLegacyStorage implements FileStorageInterface
      */
     public function getImageUrl(File $file, string $size, bool $relativePath): string{
         $url = sprintf('api/files/%s/download/%s?relativePath=1', $file->getId(), $size);
-        return UrlBuilder::concat($this->legacyBaseUrl, [$this->apiLegacyRequestService->getContent($url)], []);
+        return UrlBuilder::concat($this->legacyBaseUrl, [$this->apiLegacyRequestService->getContent($url)]);
     }
 
     /**

+ 8 - 2
src/Service/File/Storage/LocalStorage.php

@@ -99,7 +99,7 @@ class LocalStorage implements FileStorageInterface
     }
 
     /**
-     * Retoune l'URL de l'image, à la bonne taille, contenu dans la File
+     * Retourne l'URL de l'image, à la bonne taille, contenu dans le File
      * @param File $file
      * @param string $size
      * @param bool $relativePath
@@ -109,8 +109,14 @@ class LocalStorage implements FileStorageInterface
     {
         $filterName = $this->getFilterFromSizeAndConfig($size, !empty($file->getConfig()));
         $path = $file->getPath();
+
         if (!$this->cacheManager->isStored($path, $filterName)) {
-            $this->imageFactory->createImageContent($file, $filterName);
+            try {
+                $this->imageFactory->createImageContent($file, $filterName);
+            } catch (\Exception $e) {
+                $path = "images/missing-file.png";
+                return $relativePath ? $path : $this->urlBuilder->getAbsoluteUrl($path);
+            }
         }
         $url = $this->cacheManager->resolve($path, $filterName);
         return $relativePath ? $this->urlBuilder->getRelativeUrl($url) : $url;

+ 5 - 5
src/Service/Utils/UrlBuilder.php

@@ -17,14 +17,14 @@ class UrlBuilder
      * Concatenate a base url and a path
      *
      * @param string $base The base url
-     * @param array $tails La suite de l'URL sous forme de tableau
+     * @param array<string> $tails La suite de l'URL sous forme de tableau
      * @return string
      */
     public static function concatPath(string $base, array $tails): string
     {
         $url = $base;
         foreach ($tails as $tail){
-            $url = trim($url) . '/' . trim(strval($tail));
+            $url = trim($url, '/') . '/' . trim(strval($tail), '/');
         }
 
         return $url;
@@ -76,12 +76,12 @@ class UrlBuilder
      * Build an url
      *
      * @param string $url The base url
-     * @param array $tails la suite de l'url sous forme de tableau
+     * @param array<string> $tails la suite de l'url sous forme de tableau
      * @param list<string> $parameters A list of parameters (can be an empty array)
      * @param bool $preprendHttps Should the 'https://' be prepended if missing
      * @return string
      */
-    public static function concat(string $url, array $tails, array $parameters, bool $preprendHttps = false): string
+    public static function concat(string $url, array $tails, array $parameters = [], bool $preprendHttps = false): string
     {
         $url = self::concatParameters(self::concatPath($url, $tails), $parameters);
         if ($preprendHttps) {
@@ -105,6 +105,6 @@ class UrlBuilder
      * @return string
      */
     public function getAbsoluteUrl(string $path): string{
-        return self::concat($this->baseUrl, [$path], []);
+        return self::concat($this->baseUrl, [$path]);
     }
 }