From bdfb54111b5d2a469e124bf7fbd00ef9dd573ca0 Mon Sep 17 00:00:00 2001 From: Ayush Raj Srivastava Date: Mon, 26 Aug 2024 12:52:16 +0530 Subject: [PATCH] added dir permission for creating cache image --- config/services.yaml | 2 +- src/Controller/ImageCache/ImageCacheController.php | 13 +++++++++---- src/Service/UrlImageCacheService.php | 7 ++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 68d4ecdb..425650af 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -5,7 +5,7 @@ # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration parameters: locale: 'en' - uvdesk.version: "v1.1.3" + uvdesk.version: "v1.1.5" services: # default configuration for services in *this* file diff --git a/src/Controller/ImageCache/ImageCacheController.php b/src/Controller/ImageCache/ImageCacheController.php index 98265376..9b9284c8 100644 --- a/src/Controller/ImageCache/ImageCacheController.php +++ b/src/Controller/ImageCache/ImageCacheController.php @@ -30,15 +30,20 @@ public function __construct( public function getCachedImage(Request $request): Response { $response = $this->showImage(self::UVDESK_LOGO); - + + // Get the file and its real path $file = $response->getFile(); $filePath = $file->getRealPath(); + // Get the relative path by removing the kernel.project_dir $relativePath = str_replace($this->getParameter('kernel.project_dir') . '/public', '', $filePath); - $imageUrl = $this->getParameter('uvdesk.site_url') . $relativePath; - // Construct the URL to access the cached image - $imageUrl = preg_match('/\/$/', $this->getParameter('uvdesk.site_url')) ? $this->getParameter('uvdesk.site_url') . ltrim($relativePath, '/') : $this->getParameter('uvdesk.site_url') . '/' . ltrim($relativePath, '/'); + // Get the base URL, including the project name + $basePath = '/' . ltrim($request->getBasePath(), '/'); + $siteUrl = rtrim($this->getParameter('uvdesk.site_url'), '/'); + + // Construct the image URL by combining the base URL and the relative path + $imageUrl = $siteUrl . $basePath . $relativePath; // Return the image URL as JSON return new Response(json_encode($imageUrl)); diff --git a/src/Service/UrlImageCacheService.php b/src/Service/UrlImageCacheService.php index a39d64d0..c5eeec1e 100644 --- a/src/Service/UrlImageCacheService.php +++ b/src/Service/UrlImageCacheService.php @@ -14,7 +14,7 @@ class UrlImageCacheService public function __construct(ContainerInterface $container) { $this->container = $container; - $this->cacheDir = $this->container->getParameter('kernel.project_dir').'/public/cache/images'; + $this->cacheDir = $this->container->getParameter('kernel.project_dir') . '/public/cache/images'; $this->imageManager = new ImageManager($this->container); } @@ -23,6 +23,11 @@ public function getCachedImage(string $url): string $cacheKey = md5($url); $cachePath = $this->cacheDir . '/' . $cacheKey . '.png'; + // Ensure the cache directory exists + if (!is_dir($this->cacheDir)) { + mkdir($this->cacheDir, 0775, true); + } + if ($this->isCacheExpired($cachePath)) { if (file_exists($cachePath)) { unlink($cachePath); // Delete the file