Skip to content

Commit

Permalink
Merge pull request #41045 from owncloud/fix/pdf-preview-first-page
Browse files Browse the repository at this point in the history
fix: create previews from first page on multi-page documents
  • Loading branch information
phil-davis committed Feb 9, 2024
2 parents 561bd10 + 14d5bb2 commit 96303f6
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/41045
@@ -0,0 +1,6 @@
Bugfix: create previews from first page

On multi-page documents (doc, odt, pdf etc) the preview will now be generated
from the first page and no longer from the last page.

https://github.com/owncloud/core/pull/41045
2 changes: 2 additions & 0 deletions lib/private/Preview/Bitmap.php
Expand Up @@ -83,7 +83,9 @@ public function isAvailable(FileInfo $file) {
private function getResizedPreview($stream, $maxX, $maxY) {
$bp = new Imagick();

# setIteratorIndex(0) will make previews to be generated from the first page
$bp->readImageFile($stream);
$bp->setIteratorIndex(0);

$bp = $this->resize($bp, $maxX, $maxY);

Expand Down
6 changes: 2 additions & 4 deletions lib/private/PreviewManager.php
Expand Up @@ -327,8 +327,6 @@ protected function registerCoreProviders() {
// This library adds imagick support for SVG, WMF, OpenEXR, DjVu and Graphviz:

if (\extension_loaded('imagick')) {
$checkImagick = new \Imagick();

$imagickProviders = [
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
Expand All @@ -348,12 +346,12 @@ protected function registerCoreProviders() {
continue;
}

if (\count($checkImagick->queryFormats($queryFormat)) === 1) {
if (\count(\Imagick::queryFormats($queryFormat)) === 1) {
$this->registerCoreProvider($class, $provider['mimetype']);
}
}

if (\count($checkImagick->queryFormats('PDF')) === 1) {
if (\count(\Imagick::queryFormats('PDF')) === 1) {
// Office previews are only supported if either LibreOffice or OpenOffice is installed on the server
if (\OC_Helper::is_function_enabled('shell_exec')) {
$officeFound = \is_string($this->config->getSystemValue('preview_libreoffice_path', null));
Expand Down
9 changes: 9 additions & 0 deletions tests/acceptance/features/apiWebdavPreviews/previews.feature
Expand Up @@ -276,3 +276,12 @@ Feature: previews of files downloaded through the webdav API
When user "Alice" downloads the preview of "/testavatar_high.jpg" with width "32" and height "32" using the WebDAV API
Then the HTTP status code should be "200"
And the requested JPEG image should have a quality value of "100"


Scenario: the preview should be of content in the first page of a multi-page document
Given the administrator has updated system config key "enabledPreviewProviders 0" with value "OC\Preview\PDF"
And user "Alice" has uploaded file "fixtures/Preview-test.pdf" to "/Preview-test.pdf"
When user "Alice" downloads the preview of "/Preview-test.pdf" with width "612" and height "792" using the WebDAV API
Then the HTTP status code should be "200"
And the downloaded image should be "612" pixels wide and "792" pixels high
And the downloaded preview content should match with "Preview-test-612-792.png" fixtures preview content
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/acceptance/fixtures/Preview-test.odt
Binary file not shown.
Binary file added tests/acceptance/fixtures/Preview-test.pdf
Binary file not shown.
Binary file modified tests/data/testimage.odt
Binary file not shown.
Binary file added tests/data/testimage.pdf
Binary file not shown.
52 changes: 52 additions & 0 deletions tests/lib/Preview/PDFTest.php
@@ -0,0 +1,52 @@
<?php
/**
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace lib\Preview;

use OC\Preview\PDF;
use OCP\Files\NotFoundException;
use Test\Preview\Provider;

/**
* Class SVGTest
*
* @group DB
*
* @package Test\Preview
*/
class PDFTest extends Provider {
/**
* @throws NotFoundException
*/
public function setUp(): void {
if (\count(\Imagick::queryFormats('SVG')) === 1) {
parent::setUp();

$fileName = 'testimage.pdf';
$this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
$this->width = 595;
$this->height = 842;
$this->provider = new PDF();
} else {
$this->markTestSkipped('No PDF provider present');
}
}
}
11 changes: 4 additions & 7 deletions tests/lib/Preview/Provider.php
Expand Up @@ -21,12 +21,14 @@

namespace Test\Preview;

use OC\Preview\TXT;
use OCP\Files\File;
use OCP\Files\Node;
use OCP\Preview\IProvider2;
use Test\TestCase;
use Test\Traits\UserTrait;

abstract class Provider extends \Test\TestCase {
abstract class Provider extends TestCase {
use UserTrait;

/** @var File */
Expand Down Expand Up @@ -92,15 +94,10 @@ public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
$this->maxWidth = $this->width - $widthAdjustment;
$this->maxHeight = $this->height - $heightAdjustment;

// Testing code
/*print_r("w $this->width ");
print_r("h $this->height ");
print_r("r $ratio ");*/

$preview = $this->getPreview($this->provider);
// The TXT provider uses the max dimensions to create its canvas,
// so the ratio will always be the one of the max dimension canvas
if (!$this->provider instanceof \OC\Preview\TXT) {
if (!$this->provider instanceof TXT) {
$this->doesRatioMatch($preview, $ratio);
}
$this->doesPreviewFit($preview);
Expand Down

0 comments on commit 96303f6

Please sign in to comment.