-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ayush Raj Srivastava
committed
Aug 26, 2024
1 parent
650d4dd
commit a32210a
Showing
8 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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 | ||
{ | ||
$response = $this->showImage(self::UVDESK_LOGO); | ||
|
||
$file = $response->getFile(); | ||
$filePath = $file->getRealPath(); | ||
|
||
$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, '/'); | ||
|
||
// Return the image URL as JSON | ||
return new Response(json_encode($imageUrl)); | ||
} | ||
|
||
public function showImage(string $url): Response | ||
{ | ||
$cachedImagePath = $this->urlImageCacheService->getCachedImage($url); | ||
|
||
return $this->file($cachedImagePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Controller\ImageCache; | ||
|
||
use Intervention\Image\Exception\NotReadableException; | ||
use Intervention\Image\ImageManager as BaseImageManager; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class ImageManager extends BaseImageManager | ||
{ | ||
protected $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function make($data) | ||
{ | ||
$driver = $this->createDriver(); | ||
|
||
if (filter_var($data, FILTER_VALIDATE_URL)) { | ||
return $this->initFromUrl($driver, $data); | ||
} | ||
|
||
return $driver->init($data); | ||
} | ||
|
||
/** | ||
* This method hit the tracker image url and create a live instance | ||
* | ||
* @param mixed $driver | ||
* @param mixed $url | ||
* @throws \Intervention\Image\Exception\NotReadableException | ||
* @return mixed | ||
*/ | ||
public function initFromUrl($driver, $url) | ||
{ | ||
$domain = $this->container->getParameter('uvdesk.site_url'); | ||
|
||
$options = [ | ||
'http' => [ | ||
'method' => 'GET', | ||
'protocol_version' => 1.1, | ||
'header' => "Accept-language: en\r\n". | ||
"Domain: $domain\r\n". | ||
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36\r\n", | ||
], | ||
]; | ||
$context = stream_context_create($options); | ||
$data = @file_get_contents($url, false, $context); | ||
|
||
if ($data = @file_get_contents($url, false, $context)) { | ||
return $driver->decoder->initFromBinary($data); | ||
} | ||
|
||
throw new NotReadableException('Unable to init from given URL ('.$url.').'); | ||
} | ||
|
||
private function createDriver() | ||
{ | ||
$driverName = ucfirst($this->config['driver']); | ||
$driverClass = sprintf('Intervention\\Image\\%s\\Driver', $driverName); | ||
|
||
if (class_exists($driverClass)) { | ||
return new $driverClass; | ||
} | ||
|
||
throw new \Exception("Driver ({$driverName}) could not be instantiated."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use App\Controller\ImageCache\ImageManager; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class UrlImageCacheService | ||
{ | ||
protected $cacheDir; | ||
protected $container; | ||
private $imageManager; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
$this->cacheDir = $this->container->getParameter('kernel.project_dir').'/public/cache/images'; | ||
$this->imageManager = new ImageManager($this->container); | ||
} | ||
|
||
public function getCachedImage(string $url): string | ||
{ | ||
$cacheKey = md5($url); | ||
$cachePath = $this->cacheDir . '/' . $cacheKey . '.png'; | ||
|
||
if ($this->isCacheExpired($cachePath)) { | ||
if (file_exists($cachePath)) { | ||
unlink($cachePath); // Delete the file | ||
} | ||
|
||
$this->cacheImage($url, $cachePath); | ||
} | ||
|
||
return $cachePath; | ||
} | ||
|
||
private function isCacheExpired(string $cachePath): bool | ||
{ | ||
if (!file_exists($cachePath)) { | ||
return true; | ||
} | ||
|
||
$cacheLifetime = 7 * 24 * 60 * 60; // 1 week in seconds | ||
|
||
return (time() - filemtime($cachePath)) > $cacheLifetime; | ||
} | ||
|
||
private function cacheImage(string $url, string $cachePath): void | ||
{ | ||
$image = $this->imageManager->make($url); | ||
$image->save($cachePath); | ||
} | ||
} |