Simplified thumbnail and content retrieval code
This commit is contained in:
parent
132e9ce3c0
commit
bf8e6e9e00
14 changed files with 56 additions and 96 deletions
|
@ -13,7 +13,7 @@ foreach ($posts as $post)
|
|||
[
|
||||
$post->getId(),
|
||||
$post->getName(),
|
||||
$post->tryGetWorkingFullPath(),
|
||||
$post->getContentPath(),
|
||||
$post->getMimeType(),
|
||||
]). PHP_EOL;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -49,7 +49,7 @@ if ($masstag)
|
|||
|
||||
<img
|
||||
class="thumb"
|
||||
src="<?= Core::getRouter()->linkTo(['PostController', 'thumbnailView'], ['name' => $this->context->post->getName()]) ?>"
|
||||
src="<?= $this->context->post->getThumbnailUrl() ?>"
|
||||
alt="<?= TextHelper::reprPost($this->context->post) ?>"/>
|
||||
|
||||
<?php
|
||||
|
|
|
@ -17,7 +17,7 @@ class PostMocker extends AbstractMocker implements IMocker
|
|||
$post = PostModel::spawn();
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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'),
|
||||
|
|
Loading…
Reference in a new issue