Disabled blank thumbnail saving

This commit is contained in:
Marcin Kurczewski 2014-10-10 21:18:54 +02:00
parent 823fa2da15
commit a1b71ed9e1
4 changed files with 70 additions and 56 deletions

View file

@ -57,8 +57,7 @@ final class PostContentController extends AbstractController
if (!$this->fileDao->exists($sourceName))
$sourceName = $post->getContentPath();
$this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
}
}

View file

@ -71,8 +71,7 @@ final class UserAvatarController extends AbstractController
private function serveFromFile($sourceName, $size)
{
$this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
}

View file

@ -26,17 +26,19 @@ class ThumbnailService
foreach ($this->getUsedThumbnailSizes() as $size)
{
list ($width, $height) = $size;
$target = $this->getThumbnailName($sourceName, $width, $height);
$target = $this->getTargetName($sourceName, $width, $height);
$this->fileDao->delete($target);
}
}
public function generateIfNeeded($sourceName, $width, $height)
{
$thumbnailName = $this->getThumbnailName($sourceName, $width, $height);
$targetName = $this->getTargetName($sourceName, $width, $height);
if (!$this->fileDao->exists($thumbnailName))
$this->generate($sourceName, $width, $height);
if (!$this->fileDao->exists($targetName))
return $this->generate($sourceName, $width, $height);
return $targetName;
}
public function generate($sourceName, $width, $height)
@ -55,21 +57,19 @@ class ThumbnailService
throw new \InvalidArgumentException('Invalid thumbnail crop style');
}
$thumbnailName = $this->getThumbnailName($sourceName, $width, $height);
$source = $this->fileDao->load($sourceName);
$result = null;
if (!$source)
$source = $this->fileDao->load($this->getBlankThumbnailName());
if ($source)
$result = $this->thumbnailGenerator->generate($source, $width, $height, $cropStyle);
{
$thumbnailContent = $this->thumbnailGenerator->generate($source, $width, $height, $cropStyle);
if ($thumbnailContent)
{
$targetName = $this->getTargetName($sourceName, $width, $height);
$this->fileDao->save($targetName, $thumbnailContent);
return $targetName;
}
}
if (!$result)
$result = $this->fileDao->load($this->getBlankThumbnailName());
$this->fileDao->save($thumbnailName, $result);
return $this->getBlankThumbnailName();
}
public function getUsedThumbnailSizes()
@ -89,7 +89,7 @@ class ThumbnailService
}
}
public function getThumbnailName($source, $width, $height)
public function getTargetName($source, $width, $height)
{
return 'thumbnails' . DIRECTORY_SEPARATOR . $width . 'x' . $height . DIRECTORY_SEPARATOR . $source;
}

View file

@ -64,33 +64,23 @@ final class ThumbnailServiceTest extends AbstractTestCase
$this->configMock->set('misc/thumbnailCropStyle', 'outside');
$this->fileDaoMock
->expects($this->exactly(2))
->expects($this->once())
->method('load')
->withConsecutive(
['nope'],
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'])
->will(
$this->onConsecutiveCalls(
null,
'content of blank thumbnail'));
->with('nope')
->willReturn(null);
$this->thumbnailGeneratorMock
->expects($this->once())
->method('generate')
->with(
'content of blank thumbnail',
100,
100,
IThumbnailGenerator::CROP_OUTSIDE)
->willReturn('generated thumbnail');
->expects($this->never())
->method('generate');
$this->fileDaoMock
->expects($this->once())
->method('save')
->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'generated thumbnail');
->expects($this->never())
->method('save');
$thumbnailService = $this->getThumbnailService();
$thumbnailService->generate('nope', 100, 100);
$this->assertEquals(
'thumbnails' . DIRECTORY_SEPARATOR . 'blank.png',
$thumbnailService->generate('nope', 100, 100));
}
public function testThumbnailGeneratingFail()
@ -98,37 +88,63 @@ final class ThumbnailServiceTest extends AbstractTestCase
$this->configMock->set('misc/thumbnailCropStyle', 'outside');
$this->fileDaoMock
->expects($this->exactly(3))
->expects($this->once())
->method('load')
->withConsecutive(
['nope'],
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'],
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'])
->will(
$this->onConsecutiveCalls(
null,
'content of blank thumbnail',
'content of blank thumbnail (2)'));
->with('nope')
->willReturn('content of file');
$this->thumbnailGeneratorMock
->expects($this->once())
->method('generate')
->with(
'content of blank thumbnail',
'content of file',
100,
100,
IThumbnailGenerator::CROP_OUTSIDE)
->willReturn(null);
$this->fileDaoMock
->expects($this->once())
->method('save')
->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'content of blank thumbnail (2)');
->expects($this->never())
->method('save');
$thumbnailService = $this->getThumbnailService();
$thumbnailService->generate('nope', 100, 100);
$this->assertEquals(
'thumbnails' . DIRECTORY_SEPARATOR . 'blank.png',
$thumbnailService->generate('nope', 100, 100));
}
public function testThumbnailGeneratingSuccess()
{
$this->configMock->set('misc/thumbnailCropStyle', 'outside');
$this->fileDaoMock
->expects($this->once())
->method('load')
->with('okay')
->willReturn('content of file');
$this->thumbnailGeneratorMock
->expects($this->once())
->method('generate')
->with(
'content of file',
100,
100,
IThumbnailGenerator::CROP_OUTSIDE)
->willReturn('content of thumbnail');
$this->fileDaoMock
->expects($this->once())
->method('save')
->with(
'thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'okay',
'content of thumbnail');
$thumbnailService = $this->getThumbnailService();
$this->assertEquals(
'thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'okay',
$thumbnailService->generate('okay', 100, 100));
}
private function getThumbnailService()
{