From f56ae37e8fe31afb109889816273c21704361f15 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Fri, 9 Sep 2016 10:58:52 +0200 Subject: [PATCH] Opening the trashbin causes errors in log for files without preview * put a file without a generated preview in the trashbin (e.g. a *.docx file) * open the trashbin * following errors will show up in the nextcloud.log: - filesize(): stat failed for ... - fopen(...): failed to open stream: No such file or directory at ... - fread() expects parameter 1 to be resource, boolean given at ... - fclose() expects parameter 1 to be resource, boolean given at ... - imagecreatefromstring(): Empty string or invalid image at ... This is because the preview code tries to load an SVG image, which is obviously only text. The fix simply handles this before the loading happens and the web UI keeps showing the default mimetype icon. --- apps/files_trashbin/ajax/preview.php | 2 ++ lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Preview.php | 6 +++++ lib/private/PreviewNotAvailableException.php | 27 ++++++++++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 lib/private/PreviewNotAvailableException.php diff --git a/apps/files_trashbin/ajax/preview.php b/apps/files_trashbin/ajax/preview.php index 1a0131da28..3f895161f0 100644 --- a/apps/files_trashbin/ajax/preview.php +++ b/apps/files_trashbin/ajax/preview.php @@ -72,6 +72,8 @@ try{ $preview->setScalingUp($scalingUp); $preview->showPreview(); +} catch (\OC\PreviewNotAvailableException $e) { + \OC_Response::setStatus(404); }catch(\Exception $e) { \OC_Response::setStatus(500); \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 94170933e5..2dbb0075f7 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -598,6 +598,7 @@ return array( 'OC\\OCS\\Result' => $baseDir . '/lib/private/OCS/Result.php', 'OC\\Preview' => $baseDir . '/lib/private/Preview.php', 'OC\\PreviewManager' => $baseDir . '/lib/private/PreviewManager.php', + 'OC\\PreviewNotAvailableException' => $baseDir . '/lib/private/PreviewNotAvailableException.php', 'OC\\Preview\\BMP' => $baseDir . '/lib/private/Preview/BMP.php', 'OC\\Preview\\Bitmap' => $baseDir . '/lib/private/Preview/Bitmap.php', 'OC\\Preview\\Font' => $baseDir . '/lib/private/Preview/Font.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index c7e833c02f..3e39b83fdd 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -628,6 +628,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\OCS\\Result' => __DIR__ . '/../../..' . '/lib/private/OCS/Result.php', 'OC\\Preview' => __DIR__ . '/../../..' . '/lib/private/Preview.php', 'OC\\PreviewManager' => __DIR__ . '/../../..' . '/lib/private/PreviewManager.php', + 'OC\\PreviewNotAvailableException' => __DIR__ . '/../../..' . '/lib/private/PreviewNotAvailableException.php', 'OC\\Preview\\BMP' => __DIR__ . '/../../..' . '/lib/private/Preview/BMP.php', 'OC\\Preview\\Bitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/Bitmap.php', 'OC\\Preview\\Font' => __DIR__ . '/../../..' . '/lib/private/Preview/Font.php', diff --git a/lib/private/Preview.php b/lib/private/Preview.php index 67838a8d4a..9c6a7b5498 100644 --- a/lib/private/Preview.php +++ b/lib/private/Preview.php @@ -791,6 +791,7 @@ class Preview { * @param null|string $mimeTypeForHeaders the media type to use when sending back the reply * * @throws NotFoundException + * @throws PreviewNotAvailableException */ public function showPreview($mimeTypeForHeaders = null) { // Check if file is valid @@ -1172,6 +1173,7 @@ class Preview { /** * Defines the media icon, for the media type of the original file, as the preview + * @throws PreviewNotAvailableException */ private function getMimeIcon() { $image = new \OC_Image(); @@ -1181,6 +1183,10 @@ class Preview { } else { $mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath); } + // we can't load SVGs into an image + if (substr($mimeIconWebPath, -4) === '.svg') { + throw new PreviewNotAvailableException('SVG mimetype cannot be rendered'); + } $image->loadFromFile($mimeIconServerPath); $this->preview = $image; diff --git a/lib/private/PreviewNotAvailableException.php b/lib/private/PreviewNotAvailableException.php new file mode 100644 index 0000000000..7d92e86062 --- /dev/null +++ b/lib/private/PreviewNotAvailableException.php @@ -0,0 +1,27 @@ + + * + * @author Morris Jobke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC; + +class PreviewNotAvailableException extends \Exception { +}