diff --git a/src/Api/Jobs/PostJobs/GetPostContentJob.php b/src/Api/Jobs/PostJobs/GetPostContentJob.php index e0ac9722..271cd7fc 100644 --- a/src/Api/Jobs/PostJobs/GetPostContentJob.php +++ b/src/Api/Jobs/PostJobs/GetPostContentJob.php @@ -13,12 +13,9 @@ class GetPostContentJob extends AbstractJob $post = $this->postRetriever->retrieve(); $config = getConfig(); - $path = $config->main->filesPath . DS . $post->getName(); - $path = TextHelper::absolutePath($path); - if (!file_exists($path)) + $path = $post->tryGetWorkingFullPath(); + if (!$path) throw new SimpleNotFoundException('Post file does not exist'); - if (!is_readable($path)) - throw new SimpleException('Post file is not readable'); $fileName = sprintf('%s_%s_%s.%s', $config->main->title, diff --git a/src/Api/Jobs/PostJobs/GetPostThumbJob.php b/src/Api/Jobs/PostJobs/GetPostThumbJob.php index f8c5f700..4b2e89d0 100644 --- a/src/Api/Jobs/PostJobs/GetPostThumbJob.php +++ b/src/Api/Jobs/PostJobs/GetPostThumbJob.php @@ -22,28 +22,21 @@ class GetPostThumbJob extends AbstractJob $width = $this->hasArgument(JobArgs::ARG_THUMB_WIDTH) ? $this->getArgument(JobArgs::ARG_THUMB_WIDTH) : null; $height = $this->hasArgument(JobArgs::ARG_THUMB_HEIGHT) ? $this->getArgument(JobArgs::ARG_THUMB_HEIGHT) : null; - $path = PostModel::getThumbCustomPath($name, $width, $height); - if (!file_exists($path)) + $path = PostModel::tryGetWorkingThumbPath($name, $width, $height); + if (!$path) { - $path = PostModel::getThumbDefaultPath($name, $width, $height); + $post = PostModel::getByName($name); + $post = $this->postRetriever->retrieve(); + + $post->generateThumb($width, $height); + if (!file_exists($path)) { - $post = PostModel::getByName($name); - $post = $this->postRetriever->retrieve(); - - $post->generateThumb($width, $height); - - if (!file_exists($path)) - { - $path = getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'; - $path = TextHelper::absolutePath($path); - } + $path = getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'; + $path = TextHelper::absolutePath($path); } } - if (!is_readable($path)) - throw new SimpleException('Thumbnail file is not readable'); - return new ApiFileOutput($path, 'thumbnail.jpg'); } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 80473743..5ba5b820 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -315,6 +315,21 @@ final class PostEntity extends AbstractEntity implements IValidatable $this->source = $source === null ? null : trim($source); } + public function tryGetWorkingFullPath() + { + return PostModel::tryGetWorkingFullPath($this->getName()); + } + + public function getFullPath() + { + return PostModel::getFullPath($this->getName()); + } + + public function tryGetWorkingThumbPath($width = null, $height = null) + { + return PostModel::tryGetWorkingThumbPath($this->getName(), $width, $height); + } + public function getThumbCustomPath($width = null, $height = null) { return PostModel::getThumbCustomPath($this->getName(), $width, $height); @@ -325,11 +340,6 @@ final class PostEntity extends AbstractEntity implements IValidatable return PostModel::getThumbDefaultPath($this->getName(), $width, $height); } - public function getFullPath() - { - return PostModel::getFullPath($this->getName()); - } - public function hasCustomThumb($width = null, $height = null) { $thumbPath = $this->getThumbCustomPath($width, $height); diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 5e9456dd..222239de 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -264,14 +264,17 @@ final class PostModel extends AbstractCrudModel return [$width, $height]; } - private static function getThumbPathTokenized($text, $name, $width = null, $height = null) + public static function tryGetWorkingThumbPath($name, $width = null, $height = null) { - list ($width, $height) = self::validateThumbSize($width, $height); + $path = PostModel::getThumbCustomPath($name, $width, $height); + if (file_exists($path) and is_readable($path)) + return $path; - return TextHelper::absolutePath(TextHelper::replaceTokens($text, [ - 'fullpath' => getConfig()->main->thumbsPath . DS . $name, - 'width' => $width, - 'height' => $height])); + $path = PostModel::getThumbDefaultPath($name, $width, $height); + if (file_exists($path) and is_readable($path)) + return $path; + + return null; } public static function getThumbCustomPath($name, $width = null, $height = null) @@ -284,6 +287,25 @@ final class PostModel extends AbstractCrudModel return self::getThumbPathTokenized('{fullpath}-{width}x{height}.default', $name, $width, $height); } + private static function getThumbPathTokenized($text, $name, $width = null, $height = null) + { + list ($width, $height) = self::validateThumbSize($width, $height); + + return TextHelper::absolutePath(TextHelper::replaceTokens($text, [ + 'fullpath' => getConfig()->main->thumbsPath . DS . $name, + 'width' => $width, + 'height' => $height])); + } + + public static function tryGetWorkingFullPath($name) + { + $path = self::getFullPath($name); + if (file_exists($path) and is_readable($path)) + return $path; + + return null; + } + public static function getFullPath($name) { return TextHelper::absolutePath(getConfig()->main->filesPath . DS . $name); diff --git a/tests/JobTests/EditPostContentJobTest.php b/tests/JobTests/EditPostContentJobTest.php index 4c3652af..5ac499f6 100644 --- a/tests/JobTests/EditPostContentJobTest.php +++ b/tests/JobTests/EditPostContentJobTest.php @@ -137,9 +137,10 @@ class EditPostContentJobTest extends AbstractTest JobArgs::ARG_NEW_POST_CONTENT_URL => $url, ]); + $this->assert->isNotNull($post->tryGetWorkingFullPath()); $this->assert->areEqual( file_get_contents($this->getPath($fileName)), - file_get_contents(getConfig()->main->filesPath . DS . $post->getName())); + file_get_contents($post->tryGetWorkingFullPath())); return $post; } @@ -156,9 +157,10 @@ class EditPostContentJobTest extends AbstractTest JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath($fileName), 'test.jpg'), ]); + $this->assert->isNotNull($post->tryGetWorkingFullPath()); $this->assert->areEqual( file_get_contents($this->getPath($fileName)), - file_get_contents(getConfig()->main->filesPath . DS . $post->getName())); + file_get_contents($post->tryGetWorkingFullPath())); return $post; } diff --git a/tests/JobTests/EditPostThumbJobTest.php b/tests/JobTests/EditPostThumbJobTest.php new file mode 100644 index 00000000..ae7d6e2e --- /dev/null +++ b/tests/JobTests/EditPostThumbJobTest.php @@ -0,0 +1,51 @@ +grantAccess('editPostThumb'); + + $post = $this->mockPost(Auth::getCurrentUser()); + + $this->assert->isFalse($post->hasCustomThumb()); + + $post = $this->assert->doesNotThrow(function() use ($post) + { + return Api::run( + new EditPostThumbJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_THUMB_CONTENT => new ApiFileInput($this->getPath('thumb.jpg'), 'test.jpg'), + ]); + }); + + $this->assert->isTrue($post->hasCustomThumb()); + $this->assert->isNotNull($post->getThumbCustomPath()); + $this->assert->areEqual($post->getThumbCustomPath(), $post->tryGetWorkingThumbPath()); + $this->assert->areEqual( + file_get_contents($this->getPath('thumb.jpg')), + file_get_contents($post->tryGetWorkingThumbPath())); + } + + public function testFileInvalidDimensions() + { + $this->grantAccess('editPostThumb'); + + $post = $this->mockPost(Auth::getCurrentUser()); + + $this->assert->isFalse($post->hasCustomThumb()); + + $this->assert->throws(function() use ($post) + { + return Api::run( + new EditPostThumbJob(), + [ + JobArgs::ARG_POST_ID => $post->getId(), + JobArgs::ARG_NEW_THUMB_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'), + ]); + }, 'invalid thumbnail size'); + + $this->assert->isFalse($post->hasCustomThumb()); + $this->assert->areNotEqual($post->getThumbCustomPath(), $post->tryGetWorkingThumbPath()); + } +}