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->getId(),
|
||||||
$post->getName(),
|
$post->getName(),
|
||||||
$post->tryGetWorkingFullPath(),
|
$post->getContentPath(),
|
||||||
$post->getMimeType(),
|
$post->getMimeType(),
|
||||||
]). PHP_EOL;
|
]). PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,9 +23,11 @@ foreach ($posts as $post)
|
||||||
{
|
{
|
||||||
++ $i;
|
++ $i;
|
||||||
printf('%s (%d/%d)' . PHP_EOL, TextHelper::reprPost($post), $i, $entityCount);
|
printf('%s (%d/%d)' . PHP_EOL, TextHelper::reprPost($post), $i, $entityCount);
|
||||||
if ($post->tryGetWorkingThumbnailPath() and $force)
|
|
||||||
unlink($post->tryGetWorkingThumbnailPath());
|
if (file_exists($post->getThumbnailPath()) and $force)
|
||||||
if (!$post->tryGetWorkingThumbnailPath())
|
unlink($post->getThumbnailPath());
|
||||||
|
|
||||||
|
if (!file_exists($post->getThumbnailPath()))
|
||||||
$post->generateThumbnail();
|
$post->generateThumbnail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ switch ($action)
|
||||||
$func = function($name) use ($dir)
|
$func = function($name) use ($dir)
|
||||||
{
|
{
|
||||||
echo $name . PHP_EOL;
|
echo $name . PHP_EOL;
|
||||||
$srcPath = PostModel::getFullPath($name);
|
$srcPath = Core::getConfig()->main->filesPath . DS . $name;
|
||||||
$dstPath = $dir . DS . $name;
|
$dstPath = $dir . DS . $name;
|
||||||
rename($srcPath, $dstPath);
|
rename($srcPath, $dstPath);
|
||||||
};
|
};
|
||||||
|
@ -50,7 +50,7 @@ switch ($action)
|
||||||
$func = function($name)
|
$func = function($name)
|
||||||
{
|
{
|
||||||
echo $name . PHP_EOL;
|
echo $name . PHP_EOL;
|
||||||
$srcPath = PostModel::getFullPath($name);
|
$srcPath = Core::getConfig()->main->filesPath . DS . $name;
|
||||||
unlink($srcPath);
|
unlink($srcPath);
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -13,9 +13,11 @@ class GetPostContentJob extends AbstractJob
|
||||||
$post = $this->postRetriever->retrieve();
|
$post = $this->postRetriever->retrieve();
|
||||||
$config = Core::getConfig();
|
$config = Core::getConfig();
|
||||||
|
|
||||||
$path = $post->tryGetWorkingFullPath();
|
$path = $post->getContentPath();
|
||||||
if (!$path)
|
if (!file_exists($path))
|
||||||
throw new SimpleNotFoundException('Post file does not exist');
|
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',
|
$fileName = sprintf('%s_%s_%s.%s',
|
||||||
$config->main->title,
|
$config->main->title,
|
||||||
|
|
|
@ -10,25 +10,15 @@ class GetPostThumbnailJob extends AbstractJob
|
||||||
|
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
//optimize - save extra query to DB
|
$post = $this->postRetriever->retrieve();
|
||||||
if ($this->hasArgument(JobArgs::ARG_POST_NAME))
|
|
||||||
$name = $this->getArgument(JobArgs::ARG_POST_NAME);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$post = $this->postRetriever->retrieve();
|
|
||||||
$name = $post->getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = PostModel::tryGetWorkingThumbnailPath($name);
|
$path = $post->getThumbnailPath();
|
||||||
if (!$path)
|
if (!file_exists($path) or !is_readable($path))
|
||||||
{
|
{
|
||||||
$post = PostModel::getByName($name);
|
|
||||||
$post = $this->postRetriever->retrieve();
|
|
||||||
|
|
||||||
$post->generateThumbnail();
|
$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 = Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumbnail.jpg';
|
||||||
$path = TextHelper::absolutePath($path);
|
$path = TextHelper::absolutePath($path);
|
||||||
|
|
|
@ -27,7 +27,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
{
|
{
|
||||||
$this->setName(md5(mt_rand() . uniqid()));
|
$this->setName(md5(mt_rand() . uniqid()));
|
||||||
}
|
}
|
||||||
while (file_exists($this->getFullPath()));
|
while (file_exists($this->getContentPath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fillFromDatabase($row)
|
public function fillFromDatabase($row)
|
||||||
|
@ -78,8 +78,14 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
if (empty($this->getType()))
|
if (empty($this->getType()))
|
||||||
throw new SimpleException('No post type detected');
|
throw new SimpleException('No post type detected');
|
||||||
|
|
||||||
if (empty($this->tryGetWorkingFullPath()) and $this->type->toInteger() != PostType::Youtube)
|
if ($this->type->toInteger() != PostType::Youtube)
|
||||||
throw new SimpleException('No post content');
|
{
|
||||||
|
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()))
|
if (empty($this->getTags()))
|
||||||
throw new SimpleException('No tags set');
|
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);
|
$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());
|
return Core::getRouter()->linkTo(['PostController', 'thumbnailView'], ['name' => $this->getName()]);
|
||||||
}
|
|
||||||
|
|
||||||
public function tryGetWorkingThumbnailPath()
|
|
||||||
{
|
|
||||||
return $this->model->tryGetWorkingThumbnailPath($this->getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCustomThumbnailSourcePath()
|
public function getCustomThumbnailSourcePath()
|
||||||
{
|
{
|
||||||
return $this->model->getCustomThumbnailSourcePath($this->getName());
|
return Core::getConfig()->main->thumbnailsPath . DS . $this->name . '.thumb_source';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getThumbnailPath()
|
public function getThumbnailPath()
|
||||||
{
|
{
|
||||||
return $this->model->getThumbnailPath($this->getName());
|
return Core::getConfig()->main->thumbnailsPath . DS . $this->name . '.thumb';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasCustomThumbnail()
|
public function hasCustomThumbnail()
|
||||||
|
@ -374,15 +371,12 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
public function setCustomThumbnailFromPath($srcPath)
|
public function setCustomThumbnailFromPath($srcPath)
|
||||||
{
|
{
|
||||||
$config = Core::getConfig();
|
$config = Core::getConfig();
|
||||||
|
|
||||||
$mimeType = mime_content_type($srcPath);
|
$mimeType = mime_content_type($srcPath);
|
||||||
if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg']))
|
if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg']))
|
||||||
throw new SimpleException('Invalid file type "%s"', $mimeType);
|
throw new SimpleException('Invalid file type "%s"', $mimeType);
|
||||||
|
|
||||||
$dstPath = $this->getCustomThumbnailSourcePath();
|
$dstPath = $this->getCustomThumbnailSourcePath();
|
||||||
|
|
||||||
TransferHelper::copy($srcPath, $dstPath);
|
TransferHelper::copy($srcPath, $dstPath);
|
||||||
|
|
||||||
$this->generateThumbnail();
|
$this->generateThumbnail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,7 +385,6 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
$width = Core::getConfig()->browsing->thumbnailWidth;
|
$width = Core::getConfig()->browsing->thumbnailWidth;
|
||||||
$height = Core::getConfig()->browsing->thumbnailHeight;
|
$height = Core::getConfig()->browsing->thumbnailHeight;
|
||||||
$dstPath = $this->getThumbnailPath();
|
$dstPath = $this->getThumbnailPath();
|
||||||
|
|
||||||
$thumbnailGenerator = new SmartThumbnailGenerator();
|
$thumbnailGenerator = new SmartThumbnailGenerator();
|
||||||
|
|
||||||
if (file_exists($this->getCustomThumbnailSourcePath()))
|
if (file_exists($this->getCustomThumbnailSourcePath()))
|
||||||
|
@ -414,13 +407,19 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return $thumbnailGenerator->generateFromFile(
|
return $thumbnailGenerator->generateFromFile(
|
||||||
$this->getFullPath(),
|
$this->getContentPath(),
|
||||||
$dstPath,
|
$dstPath,
|
||||||
$width,
|
$width,
|
||||||
$height);
|
$height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getContentPath()
|
||||||
|
{
|
||||||
|
return TextHelper::absolutePath(Core::getConfig()->main->filesPath . DS . $this->name);
|
||||||
|
}
|
||||||
|
|
||||||
public function setContentFromPath($srcPath, $origName)
|
public function setContentFromPath($srcPath, $origName)
|
||||||
{
|
{
|
||||||
$this->setFileSize(filesize($srcPath));
|
$this->setFileSize(filesize($srcPath));
|
||||||
|
@ -470,8 +469,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
TextHelper::reprPost($duplicatedPost));
|
TextHelper::reprPost($duplicatedPost));
|
||||||
}
|
}
|
||||||
|
|
||||||
$dstPath = $this->getFullPath();
|
$dstPath = $this->getContentPath();
|
||||||
|
|
||||||
TransferHelper::copy($srcPath, $dstPath);
|
TransferHelper::copy($srcPath, $dstPath);
|
||||||
|
|
||||||
$thumbnailPath = $this->getThumbnailPath();
|
$thumbnailPath = $this->getThumbnailPath();
|
||||||
|
@ -511,13 +509,10 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmpPath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
|
$tmpPath = tempnam(sys_get_temp_dir(), 'upload') . '.dat';
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize'));
|
$maxBytes = TextHelper::stripBytesUnits(ini_get('upload_max_filesize'));
|
||||||
|
|
||||||
TransferHelper::download($srcUrl, $tmpPath, $maxBytes);
|
TransferHelper::download($srcUrl, $tmpPath, $maxBytes);
|
||||||
|
|
||||||
$this->setContentFromPath($tmpPath, basename($srcUrl));
|
$this->setContentFromPath($tmpPath, basename($srcUrl));
|
||||||
}
|
}
|
||||||
finally
|
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()
|
public static function getSpaceUsage()
|
||||||
{
|
{
|
||||||
$unixTime = PropertyModel::get(PropertyModel::PostSpaceUsageUnixTime);
|
$unixTime = PropertyModel::get(PropertyModel::PostSpaceUsageUnixTime);
|
||||||
|
|
|
@ -49,7 +49,7 @@ if ($masstag)
|
||||||
|
|
||||||
<img
|
<img
|
||||||
class="thumb"
|
class="thumb"
|
||||||
src="<?= Core::getRouter()->linkTo(['PostController', 'thumbnailView'], ['name' => $this->context->post->getName()]) ?>"
|
src="<?= $this->context->post->getThumbnailUrl() ?>"
|
||||||
alt="<?= TextHelper::reprPost($this->context->post) ?>"/>
|
alt="<?= TextHelper::reprPost($this->context->post) ?>"/>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
|
@ -17,7 +17,7 @@ class PostMocker extends AbstractMocker implements IMocker
|
||||||
$post = PostModel::spawn();
|
$post = PostModel::spawn();
|
||||||
$post->setType(new PostType(PostType::Image));
|
$post->setType(new PostType(PostType::Image));
|
||||||
$post->setTags([$this->tagMocker->mockSingle()]);
|
$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);
|
return PostModel::save($post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ class AddPostJobTest extends AbstractTest
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assert->areEqual(
|
$this->assert->areEqual(
|
||||||
file_get_contents($post->getFullPath()),
|
file_get_contents($post->getContentPath()),
|
||||||
file_get_contents($this->testSupport->getPath('image.jpg')));
|
file_get_contents($this->testSupport->getPath('image.jpg')));
|
||||||
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
||||||
$this->assert->isNotNull($post->getUploaderId());
|
$this->assert->isNotNull($post->getUploaderId());
|
||||||
|
@ -81,7 +81,7 @@ class AddPostJobTest extends AbstractTest
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assert->areEqual(
|
$this->assert->areEqual(
|
||||||
file_get_contents($post->getFullPath()),
|
file_get_contents($post->getContentPath()),
|
||||||
file_get_contents($this->testSupport->getPath('image.jpg')));
|
file_get_contents($this->testSupport->getPath('image.jpg')));
|
||||||
$this->assert->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
$this->assert->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
||||||
$this->assert->isNull($post->getUploaderId());
|
$this->assert->isNull($post->getUploaderId());
|
||||||
|
|
|
@ -197,10 +197,11 @@ class EditPostContentJobTest extends AbstractTest
|
||||||
JobArgs::ARG_NEW_POST_CONTENT_URL => $url,
|
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(
|
$this->assert->areEqual(
|
||||||
file_get_contents($this->testSupport->getPath($fileName)),
|
file_get_contents($this->testSupport->getPath($fileName)),
|
||||||
file_get_contents($post->tryGetWorkingFullPath()));
|
file_get_contents($post->getContentPath()));
|
||||||
|
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
@ -218,10 +219,11 @@ class EditPostContentJobTest extends AbstractTest
|
||||||
new ApiFileInput($this->testSupport->getPath($fileName), 'test.jpg'),
|
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(
|
$this->assert->areEqual(
|
||||||
file_get_contents($this->testSupport->getPath($fileName)),
|
file_get_contents($this->testSupport->getPath($fileName)),
|
||||||
file_get_contents($post->tryGetWorkingFullPath()));
|
file_get_contents($post->getContentPath()));
|
||||||
|
|
||||||
return $post;
|
return $post;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ class EditPostThumbnailJobTest extends AbstractTest
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assert->isTrue($post->hasCustomThumbnail());
|
$this->assert->isTrue($post->hasCustomThumbnail());
|
||||||
$img = imagecreatefromjpeg($post->tryGetWorkingThumbnailPath());
|
$img = imagecreatefromjpeg($post->getThumbnailPath());
|
||||||
$this->assert->areEqual(150, imagesx($img));
|
$this->assert->areEqual(150, imagesx($img));
|
||||||
$this->assert->areEqual(150, imagesy($img));
|
$this->assert->areEqual(150, imagesy($img));
|
||||||
imagedestroy($img);
|
imagedestroy($img);
|
||||||
|
@ -43,7 +43,7 @@ class EditPostThumbnailJobTest extends AbstractTest
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assert->isTrue($post->hasCustomThumbnail());
|
$this->assert->isTrue($post->hasCustomThumbnail());
|
||||||
$img = imagecreatefromjpeg($post->tryGetWorkingThumbnailPath());
|
$img = imagecreatefromjpeg($post->getThumbnailPath());
|
||||||
$this->assert->areEqual(150, imagesx($img));
|
$this->assert->areEqual(150, imagesx($img));
|
||||||
$this->assert->areEqual(150, imagesy($img));
|
$this->assert->areEqual(150, imagesy($img));
|
||||||
imagedestroy($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(
|
$this->assert->areEqual(
|
||||||
file_get_contents($this->testSupport->getPath('image.jpg')),
|
file_get_contents($this->testSupport->getPath('image.jpg')),
|
||||||
$output->fileContent);
|
$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->areEqual('image/jpeg', $output->mimeType);
|
||||||
$this->assert->areNotEqual(
|
$this->assert->areNotEqual(
|
||||||
file_get_contents(Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'),
|
file_get_contents(Core::getConfig()->main->mediaPath . DS . 'img' . DS . 'thumb.jpg'),
|
||||||
|
|
Loading…
Reference in a new issue