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)) if (!$this->fileDao->exists($sourceName))
$sourceName = $post->getContentPath(); $sourceName = $post->getContentPath();
$this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->generateIfNeeded($sourceName, $size, $size);
$thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
} }
} }

View file

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

View file

@ -26,17 +26,19 @@ class ThumbnailService
foreach ($this->getUsedThumbnailSizes() as $size) foreach ($this->getUsedThumbnailSizes() as $size)
{ {
list ($width, $height) = $size; list ($width, $height) = $size;
$target = $this->getThumbnailName($sourceName, $width, $height); $target = $this->getTargetName($sourceName, $width, $height);
$this->fileDao->delete($target); $this->fileDao->delete($target);
} }
} }
public function generateIfNeeded($sourceName, $width, $height) public function generateIfNeeded($sourceName, $width, $height)
{ {
$thumbnailName = $this->getThumbnailName($sourceName, $width, $height); $targetName = $this->getTargetName($sourceName, $width, $height);
if (!$this->fileDao->exists($thumbnailName)) if (!$this->fileDao->exists($targetName))
$this->generate($sourceName, $width, $height); return $this->generate($sourceName, $width, $height);
return $targetName;
} }
public function generate($sourceName, $width, $height) public function generate($sourceName, $width, $height)
@ -55,21 +57,19 @@ class ThumbnailService
throw new \InvalidArgumentException('Invalid thumbnail crop style'); throw new \InvalidArgumentException('Invalid thumbnail crop style');
} }
$thumbnailName = $this->getThumbnailName($sourceName, $width, $height);
$source = $this->fileDao->load($sourceName); $source = $this->fileDao->load($sourceName);
$result = null;
if (!$source)
$source = $this->fileDao->load($this->getBlankThumbnailName());
if ($source) 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) return $this->getBlankThumbnailName();
$result = $this->fileDao->load($this->getBlankThumbnailName());
$this->fileDao->save($thumbnailName, $result);
} }
public function getUsedThumbnailSizes() 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; 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->configMock->set('misc/thumbnailCropStyle', 'outside');
$this->fileDaoMock $this->fileDaoMock
->expects($this->exactly(2)) ->expects($this->once())
->method('load') ->method('load')
->withConsecutive( ->with('nope')
['nope'], ->willReturn(null);
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'])
->will(
$this->onConsecutiveCalls(
null,
'content of blank thumbnail'));
$this->thumbnailGeneratorMock $this->thumbnailGeneratorMock
->expects($this->once()) ->expects($this->never())
->method('generate') ->method('generate');
->with(
'content of blank thumbnail',
100,
100,
IThumbnailGenerator::CROP_OUTSIDE)
->willReturn('generated thumbnail');
$this->fileDaoMock $this->fileDaoMock
->expects($this->once()) ->expects($this->never())
->method('save') ->method('save');
->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'generated thumbnail');
$thumbnailService = $this->getThumbnailService(); $thumbnailService = $this->getThumbnailService();
$thumbnailService->generate('nope', 100, 100); $this->assertEquals(
'thumbnails' . DIRECTORY_SEPARATOR . 'blank.png',
$thumbnailService->generate('nope', 100, 100));
} }
public function testThumbnailGeneratingFail() public function testThumbnailGeneratingFail()
@ -98,37 +88,63 @@ final class ThumbnailServiceTest extends AbstractTestCase
$this->configMock->set('misc/thumbnailCropStyle', 'outside'); $this->configMock->set('misc/thumbnailCropStyle', 'outside');
$this->fileDaoMock $this->fileDaoMock
->expects($this->exactly(3)) ->expects($this->once())
->method('load') ->method('load')
->withConsecutive( ->with('nope')
['nope'], ->willReturn('content of file');
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'],
['thumbnails' . DIRECTORY_SEPARATOR . 'blank.png'])
->will(
$this->onConsecutiveCalls(
null,
'content of blank thumbnail',
'content of blank thumbnail (2)'));
$this->thumbnailGeneratorMock $this->thumbnailGeneratorMock
->expects($this->once()) ->expects($this->once())
->method('generate') ->method('generate')
->with( ->with(
'content of blank thumbnail', 'content of file',
100, 100,
100, 100,
IThumbnailGenerator::CROP_OUTSIDE) IThumbnailGenerator::CROP_OUTSIDE)
->willReturn(null); ->willReturn(null);
$this->fileDaoMock $this->fileDaoMock
->expects($this->once()) ->expects($this->never())
->method('save') ->method('save');
->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'content of blank thumbnail (2)');
$thumbnailService = $this->getThumbnailService(); $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() private function getThumbnailService()
{ {