Merge pull request #4620 from nextcloud/preview-error-handling

better handling of preview generation errors
This commit is contained in:
Roeland Jago Douma 2017-05-02 21:49:14 +02:00 committed by GitHub
commit 24ff230f93
9 changed files with 26 additions and 6 deletions

View File

@ -97,6 +97,8 @@ class PublicPreviewController extends Controller {
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);
}
}
}

View File

@ -114,6 +114,8 @@ class PreviewController extends Controller {
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);
}
}
}

View File

@ -94,6 +94,8 @@ class PreviewController extends Controller {
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);
}
}
}

View File

@ -126,6 +126,8 @@ class PreviewController extends Controller {
return $response;
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}

View File

@ -84,6 +84,7 @@ class Generator {
* @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(
@ -299,10 +300,15 @@ class Generator {
* @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
@ -325,6 +331,7 @@ class Generator {
$preview->resize(max($width, $height));
}
$path = $this->generatePath($width, $height, $crop);
try {
$file = $previewFolder->newFile($path);

View File

@ -182,7 +182,8 @@ class PreviewManager implements IPreview {
* @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) {

View File

@ -563,7 +563,7 @@ class OC_Image implements \OCP\IImage {
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'));
}
@ -573,7 +573,7 @@ class OC_Image implements \OCP\IImage {
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);
@ -583,14 +583,14 @@ class OC_Image implements \OCP\IImage {
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'));
}

View File

@ -104,7 +104,8 @@ interface IPreview {
* @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);

View File

@ -169,6 +169,7 @@ class GeneratorTest extends \Test\TestCase {
$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) {
@ -217,6 +218,7 @@ class GeneratorTest extends \Test\TestCase {
->with(128);
$image->method('data')
->willReturn('my resized data');
$image->method('valid')->willReturn(true);
$previewFile->expects($this->once())
->method('putContent')
@ -379,6 +381,7 @@ class GeneratorTest extends \Test\TestCase {
->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')