Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better handling of preview generation errors #4620

Merged
merged 4 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/files_sharing/lib/Controller/PublicPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function getPreview(
return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
}
2 changes: 2 additions & 0 deletions apps/files_trashbin/lib/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public function getPreview(
return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
}
2 changes: 2 additions & 0 deletions apps/files_versions/lib/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function getPreview(
return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
}
2 changes: 2 additions & 0 deletions core/Controller/PreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public function getPreview(
return $response;
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

}
Expand Down
7 changes: 7 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __construct(
* @param string $mimeType
* @return ISimpleFile
* @throws NotFoundException
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
$this->eventDispatcher->dispatch(
Expand Down Expand Up @@ -299,10 +300,15 @@ private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHei
* @param int $maxHeight
* @return ISimpleFile
* @throws NotFoundException
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
*/
private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
$preview = $this->helper->getImage($maxPreview);

if (!$preview->valid()) {
throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
}

if ($crop) {
if ($height !== $preview->height() && $width !== $preview->width()) {
//Resize
Expand All @@ -325,6 +331,7 @@ private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxP
$preview->resize(max($width, $height));
}


$path = $this->generatePath($width, $height, $crop);
try {
$file = $previewFolder->newFile($path);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false)
* @param string $mimeType
* @return ISimpleFile
* @throws NotFoundException
* @since 11.0.0
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
if ($this->generator === null) {
Expand Down
8 changes: 4 additions & 4 deletions lib/private/legacy/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public function loadFromFile($imagePath = false) {
case IMAGETYPE_JPEG:
if (imagetypes() & IMG_JPG) {
if (getimagesize($imagePath) !== false) {
$this->resource = imagecreatefromjpeg($imagePath);
$this->resource = @imagecreatefromjpeg($imagePath);
} else {
$this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core'));
}
Expand All @@ -573,7 +573,7 @@ public function loadFromFile($imagePath = false) {
break;
case IMAGETYPE_PNG:
if (imagetypes() & IMG_PNG) {
$this->resource = imagecreatefrompng($imagePath);
$this->resource = @imagecreatefrompng($imagePath);
// Preserve transparency
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
Expand All @@ -583,14 +583,14 @@ public function loadFromFile($imagePath = false) {
break;
case IMAGETYPE_XBM:
if (imagetypes() & IMG_XPM) {
$this->resource = imagecreatefromxbm($imagePath);
$this->resource = @imagecreatefromxbm($imagePath);
} else {
$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_WBMP:
if (imagetypes() & IMG_WBMP) {
$this->resource = imagecreatefromwbmp($imagePath);
$this->resource = @imagecreatefromwbmp($imagePath);
} else {
$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
}
Expand Down
3 changes: 2 additions & 1 deletion lib/public/IPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false);
* @param string $mimeType To force a given mimetype for the file (files_versions needs this)
* @return ISimpleFile
* @throws NotFoundException
* @since 11.0.0
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null);

Expand Down
3 changes: 3 additions & 0 deletions tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function testGetNewPreview() {
$image = $this->createMock(IImage::class);
$image->method('width')->willReturn(2048);
$image->method('height')->willReturn(2048);
$image->method('valid')->willReturn(true);

$this->helper->method('getThumbnail')
->will($this->returnCallback(function ($provider, $file, $x, $y) use ($invalidProvider, $validProvider, $image) {
Expand Down Expand Up @@ -217,6 +218,7 @@ public function testGetNewPreview() {
->with(128);
$image->method('data')
->willReturn('my resized data');
$image->method('valid')->willReturn(true);

$previewFile->expects($this->once())
->method('putContent')
Expand Down Expand Up @@ -379,6 +381,7 @@ public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expec
->willReturn($image);
$image->method('height')->willReturn($maxY);
$image->method('width')->willReturn($maxX);
$image->method('valid')->willReturn(true);

$preview = $this->createMock(ISimpleFile::class);
$previewFolder->method('newFile')
Expand Down