partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 44a1205ffeb12f819c643734019c68c0e4891c91
parent 6020f89fbdb952926dcb61bfcf953f1070544076
Author: Felicitus <felicitus@felicitus.org>
Date:   Tue,  1 Dec 2015 15:11:39 +0100

Refactored image errors, added unit tests. Related to #503

Diffstat:
Msrc/PartKeepr/ImageBundle/Controller/ImageController.php | 43++++++++++++++++++++++++-------------------
Msrc/PartKeepr/ImageBundle/Controller/TemporaryImageController.php | 2+-
Dsrc/PartKeepr/ImageBundle/Response/ImageNotFoundResponse.php | 52----------------------------------------------------
Asrc/PartKeepr/ImageBundle/Response/ImageResponse.php | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/ImageBundle/Tests/Fixtures/files/uploadtest.png | 0
Asrc/PartKeepr/ImageBundle/Tests/ImageControllerTest.php | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 147 insertions(+), 72 deletions(-)

diff --git a/src/PartKeepr/ImageBundle/Controller/ImageController.php b/src/PartKeepr/ImageBundle/Controller/ImageController.php @@ -2,14 +2,14 @@ namespace PartKeepr\ImageBundle\Controller; use Doctrine\ORM\EntityManager; -use Imagine\Exception\InvalidArgumentException; +use Gaufrette\Exception\FileNotFound; use Imagine\Gd\Imagine; use Imagine\Image\Box; use Imagine\Image\Point; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use PartKeepr\ImageBundle\Entity\CachedImage; use PartKeepr\ImageBundle\Entity\Image as PartKeeprImage; -use PartKeepr\ImageBundle\Response\ImageNotFoundResponse; +use PartKeepr\ImageBundle\Response\ImageResponse; use PartKeepr\UploadedFileBundle\Controller\FileController; use PartKeepr\UploadedFileBundle\Entity\UploadedFile; use Symfony\Component\HttpFoundation\Request; @@ -22,15 +22,15 @@ abstract class ImageController extends FileController * @ApiDoc( * description="Returns a scaled and cached image. Note that the binary data is directly returned.", * parameters={ - * {"name"="width", "dataType"="integer", "required"=true, "description"="The width in pixels"}, - * {"name"="height", "dataType"="integer", "required"=true, "description"="The height in pixels"} + * {"name"="maxWidth", "dataType"="integer", "required"=false, "description"="The width in pixels"}, + * {"name"="maxHeight", "dataType"="integer", "required"=false, "description"="The height in pixels"} * } * ) * * @param Request $request * @param $id * - * @return ImageNotFoundResponse|Response + * @return ImageResponse|Response */ public function getImageAction(Request $request, $id) { @@ -47,23 +47,28 @@ abstract class ImageController extends FileController $width = $request->get("maxWidth"); $height = $request->get("maxHeight"); - if ($width == 0) { + if ($width === null) { $width = 200; } - if ($height == 0) { + if ($height === null) { $height = 200; } if ($image === null) { - return new ImageNotFoundResponse($width, $height); + return new ImageResponse($width, $height, 404, "404 not found"); } try { $file = $this->fitWithin($image, $width, $height); - } catch (InvalidArgumentException $e) { + } catch (FileNotFound $e) { $this->get('logger')->error($e->getMessage()); - return new ImageNotFoundResponse($width, $height); + + return new ImageResponse($width, $height, 404, "404 not found"); + } catch (\Exception $e) { + $this->get('logger')->error($e->getMessage()); + + return new ImageResponse($width, $height, 500, "500 Server Error"); } return new Response(file_get_contents($file), 200, array("Content-Type" => "image/png")); @@ -96,10 +101,10 @@ abstract class ImageController extends FileController /** * Scales the image to fit within the given size. * - * @param UploadedFile $image The image to scale - * @param int $width The width - * @param int $height The height - * @param boolean $padding If true, pad the output image to the given size (transparent background). + * @param UploadedFile $image The image to scale + * @param int $width The width + * @param int $height The height + * @param boolean $padding If true, pad the output image to the given size (transparent background). * * @return string The path to the scaled file */ @@ -121,7 +126,7 @@ abstract class ImageController extends FileController $imagine = new Imagine(); - $localCacheFile = $this->getImageCacheDirectory() . $image->getFullFilename(); + $localCacheFile = $this->getImageCacheDirectory().$image->getFullFilename(); $storage = $this->get("partkeepr_uploadedfile_service")->getStorage($image); file_put_contents($localCacheFile, $storage->read($image->getFullFilename())); @@ -139,10 +144,10 @@ abstract class ImageController extends FileController /** * Returns the path to an image which has been cached in a particular width, height and mode. * - * @param UploadedFile $image The image - * @param integer $width The width - * @param integer $height The height - * @param string $mode The mode + * @param UploadedFile $image The image + * @param integer $width The width + * @param integer $height The height + * @param string $mode The mode * * @return string */ diff --git a/src/PartKeepr/ImageBundle/Controller/TemporaryImageController.php b/src/PartKeepr/ImageBundle/Controller/TemporaryImageController.php @@ -20,7 +20,7 @@ class TemporaryImageController extends ImageController /** * Handles a temporary image upload * - * @RequestParam(name="url",description="An URL where the image is located") + * @RequestParam(name="url",description="An URL where the image is located",strict=false) * ApiDoc(section="image",output="PartKeepr\ImageBundle\Response\TemporaryImageUploadResponse") * @View() * diff --git a/src/PartKeepr/ImageBundle/Response/ImageNotFoundResponse.php b/src/PartKeepr/ImageBundle/Response/ImageNotFoundResponse.php @@ -1,51 +0,0 @@ -<?php -namespace PartKeepr\ImageBundle\Response; - -use Imagine\Gd\Imagine; -use Imagine\Image\Box; -use Imagine\Image\Point; -use Symfony\Component\HttpFoundation\Response; - -class ImageNotFoundResponse extends Response -{ - /** - * Constructs a new ImageNotFoundResponse - * @param mixed|string $maxWidth - * @param int $maxHeight - */ - public function __construct ($maxWidth, $maxHeight) { - if ($maxWidth == 0) { - $maxWidth = 300; - } - - if ($maxHeight == 0) { - $maxHeight = 300; - } - - $imagine = new Imagine(); - - $size = new Box(300, 300); - $image = $imagine->create($size); - - $black = $image->palette()->color("000"); - - $path = realpath(__DIR__ . - "/../Resources/public/fonts/OpenSans-Regular.ttf" - ); - - $font = $imagine->font($path, 24, $black); - - $image->draw()->text("404 Not Found", $font, new Point(0, 0)); - - $box = $image->getSize(); - $box = $box->widen($maxWidth); - - if ($box->getHeight() > $maxHeight) { - $box = $box->heighten($maxHeight); - } - - $image->resize($box); - - return parent::__construct($image->get("png"), 404, array("Content-Type" => "image/png")); - } -}- \ No newline at end of file diff --git a/src/PartKeepr/ImageBundle/Response/ImageResponse.php b/src/PartKeepr/ImageBundle/Response/ImageResponse.php @@ -0,0 +1,54 @@ +<?php +namespace PartKeepr\ImageBundle\Response; + + +use Imagine\Gd\Imagine; +use Imagine\Image\Box; +use Imagine\Image\Point; +use Symfony\Component\HttpFoundation\Response; + +class ImageResponse extends Response +{ + /** + * Constructs a new ImageNotFoundResponse + * @param int $maxWidth + * @param int $maxHeight + * @param int $code + * @param string $message + */ + public function __construct ($maxWidth, $maxHeight, $code, $message) { + if ($maxWidth == 0) { + $maxWidth = 300; + } + + if ($maxHeight == 0) { + $maxHeight = 300; + } + + $imagine = new Imagine(); + + $size = new Box(300, 300); + $image = $imagine->create($size); + + $black = $image->palette()->color("000"); + + $path = realpath(__DIR__ . + "/../Resources/public/fonts/OpenSans-Regular.ttf" + ); + + $font = $imagine->font($path, 24, $black); + + $image->draw()->text($message, $font, new Point(0, 0)); + + $box = $image->getSize(); + $box = $box->widen($maxWidth); + + if ($box->getHeight() > $maxHeight) { + $box = $box->heighten($maxHeight); + } + + $image->resize($box); + + return parent::__construct($image->get("png"), $code, array("Content-Type" => "image/png")); + } +} diff --git a/src/PartKeepr/ImageBundle/Tests/Fixtures/files/uploadtest.png b/src/PartKeepr/ImageBundle/Tests/Fixtures/files/uploadtest.png Binary files differ. diff --git a/src/PartKeepr/ImageBundle/Tests/ImageControllerTest.php b/src/PartKeepr/ImageBundle/Tests/ImageControllerTest.php @@ -0,0 +1,68 @@ +<?php +namespace PartKeepr\ImageBundle\Tests; + + +use PartKeepr\CoreBundle\Tests\WebTestCase; +use PartKeepr\ImageBundle\Entity\TempImage; +use Symfony\Component\HttpFoundation\File\UploadedFile; + +class ImageControllerTest extends WebTestCase +{ + public function testGetImage () { + $client = static::makeClient(true); + + $file = __DIR__."/Fixtures/files/uploadtest.png"; + $originalFilename = 'uploadtest.png'; + $mimeType = "image/png"; + + $image = new UploadedFile( + $file, + $originalFilename, + $mimeType, + filesize($file) + ); + + $client->request( + 'POST', + '/api/temp_images/upload', + array(), + array('userfile' => $image) + ); + + $response = json_decode($client->getResponse()->getContent()); + + $property = "@id"; + $imageId = $response->image->$property; + $uri = $imageId . "/getImage"; + + $client->request( + 'GET', + $uri + ); + + $this->assertEquals("image/png", $client->getResponse()->headers->get("Content-Type")); + + $imageSize = getimagesizefromstring($client->getResponse()->getContent()); + + $this->assertEquals(51, $imageSize[0]); + $this->assertEquals(23, $imageSize[1]); + + $iriConverter = $this->getContainer()->get("api.iri_converter"); + + $image = $iriConverter->getItemFromIri($imageId); + + /** + * @var $image TempImage + */ + + $this->getContainer()->get("partkeepr_image_service")->delete($image); + + $client->request( + 'GET', + $uri + ); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + + } +}