From c7f077d89a35e0cf74f014c5edb235c217ce70f1 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 25 Jul 2014 18:55:27 +0200 Subject: [PATCH] Fixed thumbnails for deleted Youtube videos --- scripts/generate-thumbs.php | 11 ++++++- src/Api/Jobs/PostJobs/GetPostThumbnailJob.php | 30 ++++++++++++++----- .../SmartThumbnailGenerator.php | 16 +++------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/scripts/generate-thumbs.php b/scripts/generate-thumbs.php index 91bce5f5..f59404f4 100644 --- a/scripts/generate-thumbs.php +++ b/scripts/generate-thumbs.php @@ -28,7 +28,16 @@ foreach ($posts as $post) unlink($post->getThumbnailPath()); if (!file_exists($post->getThumbnailPath())) - $post->generateThumbnail(); + { + try + { + $post->generateThumbnail(); + } + catch (Exception $e) + { + echo $e->getMessage(); + } + } } echo 'Don\'t forget to check access rights.' . PHP_EOL; diff --git a/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php b/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php index bd179020..13411277 100644 --- a/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php +++ b/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php @@ -13,21 +13,37 @@ class GetPostThumbnailJob extends AbstractJob $post = $this->postRetriever->retrieve(); $path = $post->getThumbnailPath(); - if (!file_exists($path) or !is_readable($path)) + if (!$this->isValidThumbnailPath($path)) { - $post->generateThumbnail(); - $path = $post->getThumbnailPath(); - - if (!file_exists($path) or !is_readable($path)) + try { - $path = Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'; - $path = TextHelper::absolutePath($path); + $post->generateThumbnail(); + $path = $post->getThumbnailPath(); } + catch (Exception $e) + { + $path = null; + } + + if (!$this->isValidThumbnailPath($path)) + $path = $this->getDefaultThumbnailPath(); } return new ApiFileOutput($path, 'thumbnail.jpg'); } + private function isValidThumbnailPath($path) + { + return file_exists($path) and is_readable($path); + } + + private function getDefaultThumbnailPath() + { + $path = Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'; + $path = TextHelper::absolutePath($path); + return $path; + } + public function getRequiredArguments() { return $this->postRetriever->getRequiredArguments(); diff --git a/src/ThumbnailGenerators/SmartThumbnailGenerator.php b/src/ThumbnailGenerators/SmartThumbnailGenerator.php index b014a28d..3c2aa786 100644 --- a/src/ThumbnailGenerators/SmartThumbnailGenerator.php +++ b/src/ThumbnailGenerators/SmartThumbnailGenerator.php @@ -5,18 +5,10 @@ class SmartThumbnailGenerator implements IThumbnailGenerator { $tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.jpg'; - try - { - TransferHelper::download( - $url, - $tmpPath, - null); - } - catch (SimpleException $e) - { - echo $e->getMessage(); - return false; - } + TransferHelper::download( + $url, + $tmpPath, + null); $ret = self::generateFromFile($tmpPath, $dstPath, $width, $height);