MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/help.cafsindia.com/src/Controller/ImageCache/ImageCacheController.php
<?php

namespace App\Controller\ImageCache;

use App\Service\UrlImageCacheService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ImageCacheController extends AbstractController
{
    /**
     * Logo URL
     */
    const UVDESK_LOGO = 'https://updates.uvdesk.com/uvdesk-logo.png';

    protected $urlImageCacheService;
    protected $urlGenerator;

    public function __construct(
        UrlImageCacheService $urlImageCacheService
    ) {
        $this->urlImageCacheService = $urlImageCacheService;
    }

    /**
     * Get the cached image response
     * @return Response
     */
    public function getCachedImage(Request $request): Response
    {
        $siteUrl = $request->getSchemeAndHttpHost() . $request->getBasePath();
        $response = $this->showImage(self::UVDESK_LOGO, $siteUrl);

        // 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);

        // Construct the image URL by combining the base URL and the relative path
        $imageUrl = $siteUrl . $relativePath;

        // Return the image URL as JSON
        return new Response(json_encode($imageUrl));
    }

    public function showImage(string $imageUrl, string $domain): Response
    {
        $cachedImagePath = $this->urlImageCacheService->getCachedImage($imageUrl, $domain);

        return $this->file($cachedImagePath);
    }
}