From 13617a04751b980fbb8b601829d8ff32e55274c3 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Tue, 14 Apr 2015 18:01:56 +0200 Subject: [PATCH 1/4] Send the mime icon if we can't generate a preview --- lib/private/preview.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 145b7924c0..d8a9a3e1b7 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -701,9 +701,12 @@ class Preview { $this->generatePreview($fileId); } - // We still don't have a preview, so we generate an empty object which can't be displayed + // We still don't have a preview, so we send back the mime icon if (is_null($this->preview)) { $this->preview = new \OC_Image(); + $mimeIconWebPath = \OC_Helper::mimetypeIcon($this->mimeType); + $mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath); + $this->preview->loadFromFile($mimeIconServerPath); } return $this->preview; From 8193e1d7c15485663eeaaa1af1480f1f19004c32 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Thu, 18 Jun 2015 13:24:15 +0200 Subject: [PATCH 2/4] Move media type icon preview creation to its own method --- lib/private/preview.php | 29 +++++++++++++++++++++++++---- tests/lib/preview.php | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index d8a9a3e1b7..3a341500e6 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -703,10 +703,7 @@ class Preview { // We still don't have a preview, so we send back the mime icon if (is_null($this->preview)) { - $this->preview = new \OC_Image(); - $mimeIconWebPath = \OC_Helper::mimetypeIcon($this->mimeType); - $mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath); - $this->preview->loadFromFile($mimeIconServerPath); + $this->getMimeIcon(); } return $this->preview; @@ -1094,6 +1091,30 @@ class Preview { } } + /** + * Creates a mime icon preview of the asked dimensions + * + * This will paste the mime icon in the middle of an empty preview of the asked dimension + */ + private function getMimeIcon() { + $image = new \OC_Image(); + $mimeIconWebPath = \OC_Helper::mimetypeIcon($this->mimeType); + if (empty(\OC::$WEBROOT)) { + $mimeIconServerPath = \OC::$SERVERROOT . $mimeIconWebPath; + } else { + $mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath); + } + $image->loadFromFile($mimeIconServerPath); + + $previewWidth = (int)$image->width(); + $previewHeight = (int)$image->height(); + $askedWidth = $this->getMaxX(); + $askedHeight = $this->getMaxY(); + $this->cropAndFill( + $image, $askedWidth, $askedHeight, $previewWidth, $previewHeight + ); + } + /** * Stores the max preview in the cache * diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 27410187f4..70b7218474 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -209,6 +209,27 @@ class Preview extends TestCase { ); } + /** + * Tests if the media type icon fits into the asked dimensions + */ + public function testIsMimePreviewTheRightSize() { + $width = 400; + $height = 200; + + // Previews for odt files are not enabled + $imgData = file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.odt'); + $imgPath = '/' . self::TEST_PREVIEW_USER1 . '/files/testimage.odt'; + $this->rootView->file_put_contents($imgPath, $imgData); + + $preview = + new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.odt', $width, $height); + $preview->getPreview(); + $image = $preview->getPreview(); + + $this->assertSame($width, $image->width()); + $this->assertSame($height, $image->height()); + } + /** * We generate the data to use as it makes it easier to adjust in case we need to test * something different From 64f0fd08891ac5018fce8002246438b3e31159f6 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Thu, 18 Jun 2015 13:30:10 +0200 Subject: [PATCH 3/4] Remove unneeded returns from private cropping methods --- lib/private/preview.php | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 3a341500e6..731e999231 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -812,9 +812,8 @@ class Preview { */ // It turns out the scaled preview is now too big, so we crop the image if ($newPreviewWidth >= $askedWidth && $newPreviewHeight >= $askedHeight) { - list($newPreviewWidth, $newPreviewHeight) = - $this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight); - $this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight); + $this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight); + $this->storePreview($fileId, $askedWidth, $askedHeight); return; } @@ -822,11 +821,10 @@ class Preview { // At least one dimension of the scaled preview is too small, // so we fill the space with a transparent background if (($newPreviewWidth < $askedWidth || $newPreviewHeight < $askedHeight)) { - list($newPreviewWidth, $newPreviewHeight) = - $this->cropAndFill( - $image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight - ); - $this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight); + $this->cropAndFill( + $image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight + ); + $this->storePreview($fileId, $askedWidth, $askedHeight); return; } @@ -894,8 +892,6 @@ class Preview { * @param int $askedHeight * @param int $previewWidth * @param null $previewHeight - * - * @return \int[] */ private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) { $cropX = floor(abs($askedWidth - $previewWidth) * 0.5); @@ -904,8 +900,6 @@ class Preview { $cropY = 0; $image->crop($cropX, $cropY, $askedWidth, $askedHeight); $this->preview = $image; - - return [$askedWidth, $askedHeight]; } /** @@ -917,8 +911,6 @@ class Preview { * @param int $askedHeight * @param int $previewWidth * @param null $previewHeight - * - * @return \int[] */ private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) { if ($previewWidth > $askedWidth) { @@ -954,8 +946,6 @@ class Preview { $image = new \OC_Image($backgroundLayer); $this->preview = $image; - - return [$askedWidth, $askedHeight]; } /** From ba3b6f9be40bc3a916e8b163be62fe7530836cee Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Fri, 19 Jun 2015 15:07:49 +0200 Subject: [PATCH 4/4] Remove unneeded returns from private cropping methods --- lib/private/preview.php | 28 ++++++++++++---------------- tests/lib/preview.php | 7 +++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 731e999231..f359985283 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -701,9 +701,9 @@ class Preview { $this->generatePreview($fileId); } - // We still don't have a preview, so we send back the mime icon + // We still don't have a preview, so we send back an empty object if (is_null($this->preview)) { - $this->getMimeIcon(); + $this->preview = new \OC_Image(); } return $this->preview; @@ -712,22 +712,26 @@ class Preview { /** * Sends the preview, including the headers to client which requested it * - * @param null|string $mimeType + * @param null|string $mimeTypeForHeaders the media type to use when sending back the reply * * @throws NotFoundException */ - public function showPreview($mimeType = null) { + public function showPreview($mimeTypeForHeaders = null) { // Check if file is valid if ($this->isFileValid() === false) { throw new NotFoundException('File not found.'); } - \OCP\Response::enableCaching(3600 * 24); // 24 hours if (is_null($this->preview)) { $this->getPreview(); } if ($this->preview instanceof \OCP\IImage) { - $this->preview->show($mimeType); + if ($this->preview->valid()) { + \OCP\Response::enableCaching(3600 * 24); // 24 hours + } else { + $this->getMimeIcon(); + } + $this->preview->show($mimeTypeForHeaders); } } @@ -1082,9 +1086,7 @@ class Preview { } /** - * Creates a mime icon preview of the asked dimensions - * - * This will paste the mime icon in the middle of an empty preview of the asked dimension + * Defines the media icon, for the media type of the original file, as the preview */ private function getMimeIcon() { $image = new \OC_Image(); @@ -1096,13 +1098,7 @@ class Preview { } $image->loadFromFile($mimeIconServerPath); - $previewWidth = (int)$image->width(); - $previewHeight = (int)$image->height(); - $askedWidth = $this->getMaxX(); - $askedHeight = $this->getMaxY(); - $this->cropAndFill( - $image, $askedWidth, $askedHeight, $previewWidth, $previewHeight - ); + $this->preview = $image; } /** diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 70b7218474..ca7fa6987d 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -210,9 +210,9 @@ class Preview extends TestCase { } /** - * Tests if the media type icon fits into the asked dimensions + * Tests if unsupported previews return an empty object */ - public function testIsMimePreviewTheRightSize() { + public function testUnsupportedPreviewsReturnEmptyObject() { $width = 400; $height = 200; @@ -226,8 +226,7 @@ class Preview extends TestCase { $preview->getPreview(); $image = $preview->getPreview(); - $this->assertSame($width, $image->width()); - $this->assertSame($height, $image->height()); + $this->assertSame(false, $image->valid()); } /**