From bf8e6e9e00a0ead20ed3538b3375bdd65984459c Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 1 Jun 2014 13:51:29 +0200 Subject: [PATCH] Simplified thumbnail and content retrieval code --- scripts/find-posts.php | 2 +- scripts/generate-thumbs.php | 8 ++-- scripts/process-detached-files.php | 4 +- src/Api/Jobs/PostJobs/GetPostContentJob.php | 6 ++- src/Api/Jobs/PostJobs/GetPostThumbnailJob.php | 20 ++------ src/Models/Entities/PostEntity.php | 47 +++++++++---------- src/Models/PostModel.php | 35 -------------- src/Views/post/post-small.phtml | 2 +- tests/Support/Mockers/PostMocker.php | 2 +- tests/Tests/JobTests/AddPostJobTest.php | 4 +- .../Tests/JobTests/EditPostContentJobTest.php | 10 ++-- .../JobTests/EditPostThumbnailJobTest.php | 4 +- .../Tests/JobTests/GetPostContentJobTest.php | 3 +- .../JobTests/GetPostThumbnailJobTest.php | 5 +- 14 files changed, 56 insertions(+), 96 deletions(-) diff --git a/scripts/find-posts.php b/scripts/find-posts.php index 30d7c534..2b4a1aad 100644 --- a/scripts/find-posts.php +++ b/scripts/find-posts.php @@ -13,7 +13,7 @@ foreach ($posts as $post) [ $post->getId(), $post->getName(), - $post->tryGetWorkingFullPath(), + $post->getContentPath(), $post->getMimeType(), ]). PHP_EOL; } diff --git a/scripts/generate-thumbs.php b/scripts/generate-thumbs.php index e1f54f21..91bce5f5 100644 --- a/scripts/generate-thumbs.php +++ b/scripts/generate-thumbs.php @@ -23,9 +23,11 @@ foreach ($posts as $post) { ++ $i; printf('%s (%d/%d)' . PHP_EOL, TextHelper::reprPost($post), $i, $entityCount); - if ($post->tryGetWorkingThumbnailPath() and $force) - unlink($post->tryGetWorkingThumbnailPath()); - if (!$post->tryGetWorkingThumbnailPath()) + + if (file_exists($post->getThumbnailPath()) and $force) + unlink($post->getThumbnailPath()); + + if (!file_exists($post->getThumbnailPath())) $post->generateThumbnail(); } diff --git a/scripts/process-detached-files.php b/scripts/process-detached-files.php index 04405f20..fc0e74e7 100644 --- a/scripts/process-detached-files.php +++ b/scripts/process-detached-files.php @@ -39,7 +39,7 @@ switch ($action) $func = function($name) use ($dir) { echo $name . PHP_EOL; - $srcPath = PostModel::getFullPath($name); + $srcPath = Core::getConfig()->main->filesPath . DS . $name; $dstPath = $dir . DS . $name; rename($srcPath, $dstPath); }; @@ -50,7 +50,7 @@ switch ($action) $func = function($name) { echo $name . PHP_EOL; - $srcPath = PostModel::getFullPath($name); + $srcPath = Core::getConfig()->main->filesPath . DS . $name; unlink($srcPath); }; break; diff --git a/src/Api/Jobs/PostJobs/GetPostContentJob.php b/src/Api/Jobs/PostJobs/GetPostContentJob.php index 36f52537..532fad35 100644 --- a/src/Api/Jobs/PostJobs/GetPostContentJob.php +++ b/src/Api/Jobs/PostJobs/GetPostContentJob.php @@ -13,9 +13,11 @@ class GetPostContentJob extends AbstractJob $post = $this->postRetriever->retrieve(); $config = Core::getConfig(); - $path = $post->tryGetWorkingFullPath(); - if (!$path) + $path = $post->getContentPath(); + if (!file_exists($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/GetPostThumbnailJob.php b/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php index 63a1aa9d..0dfce135 100644 --- a/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php +++ b/src/Api/Jobs/PostJobs/GetPostThumbnailJob.php @@ -10,25 +10,15 @@ class GetPostThumbnailJob extends AbstractJob public function execute() { - //optimize - save extra query to DB - if ($this->hasArgument(JobArgs::ARG_POST_NAME)) - $name = $this->getArgument(JobArgs::ARG_POST_NAME); - else - { - $post = $this->postRetriever->retrieve(); - $name = $post->getName(); - } + $post = $this->postRetriever->retrieve(); - $path = PostModel::tryGetWorkingThumbnailPath($name); - if (!$path) + $path = $post->getThumbnailPath(); + if (!file_exists($path) or !is_readable($path)) { - $post = PostModel::getByName($name); - $post = $this->postRetriever->retrieve(); - $post->generateThumbnail(); - $path = PostModel::tryGetWorkingThumbnailPath($name); + $path = $post->getThumbnailPath(); - if (!$path) + if (!file_exists($path) or !is_readable($path)) { $path = Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumbnail.jpg'; $path = TextHelper::absolutePath($path); diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 1b0a9452..1ca0d1c1 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -27,7 +27,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ { $this->setName(md5(mt_rand() . uniqid())); } - while (file_exists($this->getFullPath())); + while (file_exists($this->getContentPath())); } public function fillFromDatabase($row) @@ -78,8 +78,14 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ if (empty($this->getType())) throw new SimpleException('No post type detected'); - if (empty($this->tryGetWorkingFullPath()) and $this->type->toInteger() != PostType::Youtube) - throw new SimpleException('No post content'); + if ($this->type->toInteger() != PostType::Youtube) + { + if (!file_exists($this->getContentPath())) + throw new SimpleException('No post content'); + + if (!is_readable($this->getContentPath())) + throw new SimpleException('Post content is not readable (check file permissions)'); + } if (empty($this->getTags())) throw new SimpleException('No tags set'); @@ -340,29 +346,20 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ $this->source = $source === null ? null : trim($source); } - public function tryGetWorkingFullPath() - { - return $this->model->tryGetWorkingFullPath($this->getName()); - } - public function getFullPath() + public function getThumbnailUrl() { - return $this->model->getFullPath($this->getName()); - } - - public function tryGetWorkingThumbnailPath() - { - return $this->model->tryGetWorkingThumbnailPath($this->getName()); + return Core::getRouter()->linkTo(['PostController', 'thumbnailView'], ['name' => $this->getName()]); } public function getCustomThumbnailSourcePath() { - return $this->model->getCustomThumbnailSourcePath($this->getName()); + return Core::getConfig()->main->thumbnailsPath . DS . $this->name . '.thumb_source'; } public function getThumbnailPath() { - return $this->model->getThumbnailPath($this->getName()); + return Core::getConfig()->main->thumbnailsPath . DS . $this->name . '.thumb'; } public function hasCustomThumbnail() @@ -374,15 +371,12 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ public function setCustomThumbnailFromPath($srcPath) { $config = Core::getConfig(); - $mimeType = mime_content_type($srcPath); if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg'])) throw new SimpleException('Invalid file type "%s"', $mimeType); $dstPath = $this->getCustomThumbnailSourcePath(); - TransferHelper::copy($srcPath, $dstPath); - $this->generateThumbnail(); } @@ -391,7 +385,6 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ $width = Core::getConfig()->browsing->thumbnailWidth; $height = Core::getConfig()->browsing->thumbnailHeight; $dstPath = $this->getThumbnailPath(); - $thumbnailGenerator = new SmartThumbnailGenerator(); if (file_exists($this->getCustomThumbnailSourcePath())) @@ -414,13 +407,19 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ else { return $thumbnailGenerator->generateFromFile( - $this->getFullPath(), + $this->getContentPath(), $dstPath, $width, $height); } } + + public function getContentPath() + { + return TextHelper::absolutePath(Core::getConfig()->main->filesPath . DS . $this->name); + } + public function setContentFromPath($srcPath, $origName) { $this->setFileSize(filesize($srcPath)); @@ -470,8 +469,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ TextHelper::reprPost($duplicatedPost)); } - $dstPath = $this->getFullPath(); - + $dstPath = $this->getContentPath(); TransferHelper::copy($srcPath, $dstPath); $thumbnailPath = $this->getThumbnailPath(); @@ -511,13 +509,10 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ } $tmpPath = tempnam(sys_get_temp_dir(), 'upload') . '.dat'; - try { $maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize')); - TransferHelper::download($srcUrl, $tmpPath, $maxBytes); - $this->setContentFromPath($tmpPath, basename($srcUrl)); } finally diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 7ac3626b..4727caae 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -254,41 +254,6 @@ final class PostModel extends AbstractCrudModel - public static function tryGetWorkingThumbnailPath($name) - { - $path = PostModel::getThumbnailPath($name); - if (file_exists($path) and is_readable($path)) - return $path; - - return null; - } - - public static function getCustomThumbnailSourcePath($name) - { - return Core::getConfig()->main->thumbnailsPath . DS . $name . '.thumb_source'; - } - - public static function getThumbnailPath($name) - { - return Core::getConfig()->main->thumbnailsPath . DS . $name . '.thumb'; - } - - 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(Core::getConfig()->main->filesPath . DS . $name); - } - - - public static function getSpaceUsage() { $unixTime = PropertyModel::get(PropertyModel::PostSpaceUsageUnixTime); diff --git a/src/Views/post/post-small.phtml b/src/Views/post/post-small.phtml index 4a287cb5..a0567955 100644 --- a/src/Views/post/post-small.phtml +++ b/src/Views/post/post-small.phtml @@ -49,7 +49,7 @@ if ($masstag) <?= TextHelper::reprPost($this->context->post) ?> setType(new PostType(PostType::Image)); $post->setTags([$this->tagMocker->mockSingle()]); - copy($this->testSupport->getPath('image.jpg'), $post->getFullPath()); + copy($this->testSupport->getPath('image.jpg'), $post->getContentPath()); return PostModel::save($post); } } diff --git a/tests/Tests/JobTests/AddPostJobTest.php b/tests/Tests/JobTests/AddPostJobTest.php index 8b274d88..08b98c32 100644 --- a/tests/Tests/JobTests/AddPostJobTest.php +++ b/tests/Tests/JobTests/AddPostJobTest.php @@ -28,7 +28,7 @@ class AddPostJobTest extends AbstractTest }); $this->assert->areEqual( - file_get_contents($post->getFullPath()), + file_get_contents($post->getContentPath()), file_get_contents($this->testSupport->getPath('image.jpg'))); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId()); $this->assert->isNotNull($post->getUploaderId()); @@ -81,7 +81,7 @@ class AddPostJobTest extends AbstractTest }); $this->assert->areEqual( - file_get_contents($post->getFullPath()), + file_get_contents($post->getContentPath()), file_get_contents($this->testSupport->getPath('image.jpg'))); $this->assert->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId()); $this->assert->isNull($post->getUploaderId()); diff --git a/tests/Tests/JobTests/EditPostContentJobTest.php b/tests/Tests/JobTests/EditPostContentJobTest.php index 61eccd42..d7c449b7 100644 --- a/tests/Tests/JobTests/EditPostContentJobTest.php +++ b/tests/Tests/JobTests/EditPostContentJobTest.php @@ -197,10 +197,11 @@ class EditPostContentJobTest extends AbstractTest JobArgs::ARG_NEW_POST_CONTENT_URL => $url, ]); - $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->isNotNull($post->getContentPath()); + $this->assert->isTrue(file_exists($post->getContentPath())); $this->assert->areEqual( file_get_contents($this->testSupport->getPath($fileName)), - file_get_contents($post->tryGetWorkingFullPath())); + file_get_contents($post->getContentPath())); return $post; } @@ -218,10 +219,11 @@ class EditPostContentJobTest extends AbstractTest new ApiFileInput($this->testSupport->getPath($fileName), 'test.jpg'), ]); - $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->isNotNull($post->getContentPath()); + $this->assert->isTrue(file_exists($post->getContentPath())); $this->assert->areEqual( file_get_contents($this->testSupport->getPath($fileName)), - file_get_contents($post->tryGetWorkingFullPath())); + file_get_contents($post->getContentPath())); return $post; } diff --git a/tests/Tests/JobTests/EditPostThumbnailJobTest.php b/tests/Tests/JobTests/EditPostThumbnailJobTest.php index abb22384..acca3932 100644 --- a/tests/Tests/JobTests/EditPostThumbnailJobTest.php +++ b/tests/Tests/JobTests/EditPostThumbnailJobTest.php @@ -19,7 +19,7 @@ class EditPostThumbnailJobTest extends AbstractTest }); $this->assert->isTrue($post->hasCustomThumbnail()); - $img = imagecreatefromjpeg($post->tryGetWorkingThumbnailPath()); + $img = imagecreatefromjpeg($post->getThumbnailPath()); $this->assert->areEqual(150, imagesx($img)); $this->assert->areEqual(150, imagesy($img)); imagedestroy($img); @@ -43,7 +43,7 @@ class EditPostThumbnailJobTest extends AbstractTest }); $this->assert->isTrue($post->hasCustomThumbnail()); - $img = imagecreatefromjpeg($post->tryGetWorkingThumbnailPath()); + $img = imagecreatefromjpeg($post->getThumbnailPath()); $this->assert->areEqual(150, imagesx($img)); $this->assert->areEqual(150, imagesy($img)); imagedestroy($img); diff --git a/tests/Tests/JobTests/GetPostContentJobTest.php b/tests/Tests/JobTests/GetPostContentJobTest.php index 4a52dc91..f36f8553 100644 --- a/tests/Tests/JobTests/GetPostContentJobTest.php +++ b/tests/Tests/JobTests/GetPostContentJobTest.php @@ -15,7 +15,8 @@ class GetPostContentJobTest extends AbstractTest ]); }); - $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->isNotNull($post->getContentPath()); + $this->assert->isTrue(file_exists($post->getContentPath())); $this->assert->areEqual( file_get_contents($this->testSupport->getPath('image.jpg')), $output->fileContent); diff --git a/tests/Tests/JobTests/GetPostThumbnailJobTest.php b/tests/Tests/JobTests/GetPostThumbnailJobTest.php index ed20b8de..70c97986 100644 --- a/tests/Tests/JobTests/GetPostThumbnailJobTest.php +++ b/tests/Tests/JobTests/GetPostThumbnailJobTest.php @@ -15,7 +15,10 @@ class GetPostThumbnailJobTest extends AbstractTest ]); }); - $this->assert->isNotNull($post->tryGetWorkingFullPath()); + $this->assert->isNotNull($post->getContentPath()); + $this->assert->isNotNull($post->getThumbnailPath()); + $this->assert->isTrue(file_exists($post->getContentPath())); + $this->assert->isTrue(file_exists($post->getThumbnailPath())); $this->assert->areEqual('image/jpeg', $output->mimeType); $this->assert->areNotEqual( file_get_contents(Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'),