Modified post path management

This commit is contained in:
Marcin Kurczewski 2014-05-13 00:01:28 +02:00
parent 5514ed4fd6
commit 5d9513bac0
6 changed files with 109 additions and 34 deletions

View file

@ -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,

View file

@ -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');
}

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

View file

@ -0,0 +1,51 @@
<?php
class EditPostThumbJobTest extends AbstractTest
{
public function testFile()
{
$this->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());
}
}