Merge pull request #12546 from oparoz/patch-1

Send the mime icon if we can't generate a preview
This commit is contained in:
Björn Schießle 2015-06-22 13:55:17 +02:00
commit a88b370dc8
2 changed files with 51 additions and 21 deletions

View File

@ -701,7 +701,7 @@ class Preview {
$this->generatePreview($fileId); $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 an empty object
if (is_null($this->preview)) { if (is_null($this->preview)) {
$this->preview = new \OC_Image(); $this->preview = new \OC_Image();
} }
@ -712,22 +712,26 @@ class Preview {
/** /**
* Sends the preview, including the headers to client which requested it * 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 * @throws NotFoundException
*/ */
public function showPreview($mimeType = null) { public function showPreview($mimeTypeForHeaders = null) {
// Check if file is valid // Check if file is valid
if ($this->isFileValid() === false) { if ($this->isFileValid() === false) {
throw new NotFoundException('File not found.'); throw new NotFoundException('File not found.');
} }
\OCP\Response::enableCaching(3600 * 24); // 24 hours
if (is_null($this->preview)) { if (is_null($this->preview)) {
$this->getPreview(); $this->getPreview();
} }
if ($this->preview instanceof \OCP\IImage) { 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);
} }
} }
@ -812,9 +816,8 @@ class Preview {
*/ */
// It turns out the scaled preview is now too big, so we crop the image // It turns out the scaled preview is now too big, so we crop the image
if ($newPreviewWidth >= $askedWidth && $newPreviewHeight >= $askedHeight) { if ($newPreviewWidth >= $askedWidth && $newPreviewHeight >= $askedHeight) {
list($newPreviewWidth, $newPreviewHeight) = $this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight);
$this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight); $this->storePreview($fileId, $askedWidth, $askedHeight);
$this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
return; return;
} }
@ -822,11 +825,10 @@ class Preview {
// At least one dimension of the scaled preview is too small, // At least one dimension of the scaled preview is too small,
// so we fill the space with a transparent background // so we fill the space with a transparent background
if (($newPreviewWidth < $askedWidth || $newPreviewHeight < $askedHeight)) { if (($newPreviewWidth < $askedWidth || $newPreviewHeight < $askedHeight)) {
list($newPreviewWidth, $newPreviewHeight) = $this->cropAndFill(
$this->cropAndFill( $image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight
$image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight );
); $this->storePreview($fileId, $askedWidth, $askedHeight);
$this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
return; return;
} }
@ -894,8 +896,6 @@ class Preview {
* @param int $askedHeight * @param int $askedHeight
* @param int $previewWidth * @param int $previewWidth
* @param null $previewHeight * @param null $previewHeight
*
* @return \int[]
*/ */
private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) { private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) {
$cropX = floor(abs($askedWidth - $previewWidth) * 0.5); $cropX = floor(abs($askedWidth - $previewWidth) * 0.5);
@ -904,8 +904,6 @@ class Preview {
$cropY = 0; $cropY = 0;
$image->crop($cropX, $cropY, $askedWidth, $askedHeight); $image->crop($cropX, $cropY, $askedWidth, $askedHeight);
$this->preview = $image; $this->preview = $image;
return [$askedWidth, $askedHeight];
} }
/** /**
@ -917,8 +915,6 @@ class Preview {
* @param int $askedHeight * @param int $askedHeight
* @param int $previewWidth * @param int $previewWidth
* @param null $previewHeight * @param null $previewHeight
*
* @return \int[]
*/ */
private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) { private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) {
if ($previewWidth > $askedWidth) { if ($previewWidth > $askedWidth) {
@ -954,8 +950,6 @@ class Preview {
$image = new \OC_Image($backgroundLayer); $image = new \OC_Image($backgroundLayer);
$this->preview = $image; $this->preview = $image;
return [$askedWidth, $askedHeight];
} }
/** /**
@ -1091,6 +1085,22 @@ class Preview {
} }
} }
/**
* Defines the media icon, for the media type of the original file, as the preview
*/
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);
$this->preview = $image;
}
/** /**
* Stores the max preview in the cache * Stores the max preview in the cache
* *

View File

@ -209,6 +209,26 @@ class Preview extends TestCase {
); );
} }
/**
* Tests if unsupported previews return an empty object
*/
public function testUnsupportedPreviewsReturnEmptyObject() {
$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(false, $image->valid());
}
/** /**
* We generate the data to use as it makes it easier to adjust in case we need to test * We generate the data to use as it makes it easier to adjust in case we need to test
* something different * something different