Skip to content

Commit

Permalink
added dir permission for creating cache image
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush Raj Srivastava committed Aug 26, 2024
1 parent a32210a commit bdfb541
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/Controller/ImageCache/ImageCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 6 additions & 1 deletion src/Service/UrlImageCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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
Expand Down

0 comments on commit bdfb541

Please sign in to comment.