Server: getPreview on a broken image shouldn't crash

Created on 24 Nov 2017  路  6Comments  路  Source: nextcloud/server

When trying to obtain a preview from a broken image file, nextcloud 12 currently crashes:

An unhandled exception has been thrown:
Error: Call to a member function file_get_contents() on null in /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Files/Filesystem.php:717
Stack trace:
#0 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/legacy/image.php(628): OC\Files\Filesystem::file_get_contents('/srv/nextcloud/...')
#1 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Preview/Image.php(57): OC_Image->loadFromFile('/srv/nextcloud/...')
#2 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Preview/GeneratorHelper.php(54): OC\Preview\Image->getThumbnail('/files/MALE/alb...', 2048, 2048, false, Object(OC\Files\View))
#3 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Preview/Generator.php(162): OC\Preview\GeneratorHelper->getThumbnail(Object(OC\Preview\JPEG), Object(OC\Files\Node\File), 2048, 2048)
#4 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Preview/Generator.php(110): OC\Preview\Generator->getMaxPreview(Object(OC\Files\SimpleFS\SimpleFolder), Object(OC\Files\Node\File), 'image/jpeg')
#5 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/PreviewManager.php(201): OC\Preview\Generator->getPreview(Object(OC\Files\Node\File), 32, 32, true, 'fill', 'image/jpeg')
#6 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(213): OC\PreviewManager->getPreview(Object(OC\Files\Node\File), 32, 32, true)
#7 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(245): OCA\PreviewGenerator\Command\PreGenerate->processFile(Object(OC\Files\Node\File))
#8 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(247): OCA\PreviewGenerator\Command\PreGenerate->processFolder(Object(OC\Files\Node\Folder))
#9 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(247): OCA\PreviewGenerator\Command\PreGenerate->processFolder(Object(OC\Files\Node\Folder))
#10 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(247): OCA\PreviewGenerator\Command\PreGenerate->processFolder(Object(OC\Files\Node\Folder))
#11 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(201): OCA\PreviewGenerator\Command\PreGenerate->processFolder(Object(OC\Files\Node\Folder))
#12 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(170): OCA\PreviewGenerator\Command\PreGenerate->processRow(Array)
#13 /usr/local/nextcloud/nextcloud-12.0.2/apps/previewgenerator/lib/Command/PreGenerate.php(137): OCA\PreviewGenerator\Command\PreGenerate->startProcessing()
#14 /usr/local/nextcloud/nextcloud-12.0.2/3rdparty/symfony/console/Command/Command.php(256): OCA\PreviewGenerator\Command\PreGenerate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /usr/local/nextcloud/nextcloud-12.0.2/3rdparty/symfony/console/Application.php(818): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /usr/local/nextcloud/nextcloud-12.0.2/3rdparty/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(OCA\PreviewGenerator\Command\PreGenerate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /usr/local/nextcloud/nextcloud-12.0.2/3rdparty/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /usr/local/nextcloud/nextcloud-12.0.2/lib/private/Console/Application.php(170): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /usr/local/nextcloud/nextcloud-12.0.2/console.php(100): OC\Console\Application->run()
#20 /usr/local/nextcloud/nextcloud-12.0.2/occ(11): require_once('/usr/local/next...')
#21 {main}

(Tested with a _.jpg_ file containing only null characters. Preview generation was triggered by the preview generator app.)

exif_imagetype($imagePath) fails determining the image type. The following file-type switch statement falls back to the default case and fails because Filesystem isn't configured:

            default:

                // this is mostly file created from encrypted file
                $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
                $iType = IMAGETYPE_PNG;
                $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
                break;

My workaround for this is:

            default:

                // this is mostly file created from encrypted file
                if (\OC\Files\Filesystem::getView() === null) {
                    $this->logger->warning('LEO: Bad image: '.$imagePath);
                    $this->resource = file_get_contents($imagePath);
                    // return false;
                } else {
                    $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
                }
                $iType = IMAGETYPE_PNG;
                $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
                break;

... which certainly isn't the optimal solution. (I don't know if the file contents are useful at all in this case.) But at least it doesn't crash.

1. to develop bug previews and thumbnails

Most helpful comment

I am experiencing the same issues. For example if a file with a .jpg extension is actually a html file it will generate this same problem.

All 6 comments

cc @rullzer

Is this still a thing?

yeah it is.
I guess we can do better now with php7.

But I would need a broken jpg to test with

Hi @rullzer

The bug is still present. Come here for this report. https://github.com/matiasdelellis/facerecognition/issues/295

But I would need a broken jpg to test with.

MMM.. The main problem is that Nexcloud trusts the extensions very much. if the file has .jpg extension, already returns mimetype 'image/jpeg'. I trust in this mimetype, and try to open it with OC_Image. and for example the file is an mp4 video, with jpeg extension (This is the case that started our bug report.), fails miserably as this report. :sweat:

So, here an example: https://delellis.com.ar/s/x7yw66odGqPCKwF

It is just an mp4 with jpg extension..

I am experiencing the same issues. For example if a file with a .jpg extension is actually a html file it will generate this same problem.

You don't need an MP4 file or a corrupted picture to trigger this. Just create an empty file and stick .jpg onto the end of the filename. I've got a bunch of empty .jpg files lying around (legacy of a filesystem crash without proper backups...) and they consistently trigger this bug in the Face Recognition app.

Was this page helpful?
0 / 5 - 0 ratings