Skip to content

Commit

Permalink
Introduce \OCP\Preview\IProvider2 with the new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 authored and gitmate-bot committed Jan 18, 2018
1 parent d5a4d5f commit 95e0e0c
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 125 deletions.
5 changes: 2 additions & 3 deletions lib/private/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ private function generatePreview() {

foreach ($providers as $closure) {
$provider = $closure();
if (!($provider instanceof \OCP\Preview\IProvider)) {
if (!($provider instanceof \OCP\Preview\IProvider2)) {
continue;
}

Expand All @@ -1117,8 +1117,7 @@ private function generatePreview() {
);

/** @var $provider Provider */
$preview = $provider->getThumbnail(
$file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false);
$preview = $provider->getThumbnail($file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false);

if (!($preview instanceof \OCP\IImage)) {
continue;
Expand Down
32 changes: 19 additions & 13 deletions lib/private/Preview/Bitmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@
namespace OC\Preview;

use Imagick;
use OCP\Files\File;
use OCP\Preview\IProvider2;
use OCP\Util;

/**
* Creates a PNG preview using ImageMagick via the PECL extension
*
* @package OC\Preview
*/
abstract class Bitmap extends Provider {
abstract class Bitmap implements IProvider2 {

/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {

$tmpPath = $fileview->toTmpFile($path);
if (!$tmpPath) {
return false;
}
$stream = $file->fopen('r');

// Creates \Imagick object from bitmap or vector file
try {
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
$bp = $this->getResizedPreview($stream, $maxX, $maxY);
} catch (\Exception $e) {
\OCP\Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), \OCP\Util::ERROR);
Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), Util::ERROR);
return false;
}

unlink($tmpPath);
fclose($stream);

//new bitmap image object
$image = new \OC_Image();
Expand All @@ -60,6 +60,13 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
return $image->valid() ? $image : false;
}

/**
* @inheritdoc
*/
public function isAvailable(File $file) {
return true;
}

/**
* Returns a preview of maxX times maxY dimensions in PNG format
*
Expand All @@ -68,17 +75,16 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
* ICC profiles are here: http://www.color.org/srgbprofiles.xalter
* * It's possible to Gamma-correct an image via gammaImage()
*
* @param string $tmpPath the location of the file to convert
* @param resource $stream the handle of the file to convert
* @param int $maxX
* @param int $maxY
*
* @return \Imagick
*/
private function getResizedPreview($tmpPath, $maxX, $maxY) {
private function getResizedPreview($stream, $maxX, $maxY) {
$bp = new Imagick();

// Layer 0 contains either the bitmap or a flat representation of all vector layers
$bp->readImage($tmpPath . '[0]');
$bp->readImageFile($stream);

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

Expand Down
2 changes: 1 addition & 1 deletion lib/private/Preview/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ class Font extends Bitmap {
public function getMimeType() {
return '/application\/(?:font-sfnt|x-font$)/';
}
}
}
34 changes: 15 additions & 19 deletions lib/private/Preview/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,28 @@
*/
namespace OC\Preview;

abstract class Image extends Provider {
use OCP\Files\File;
use OCP\Preview\IProvider2;

abstract class Image implements IProvider2 {

/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
//get fileinfo
$fileInfo = $fileview->getFileInfo($path);
if (!$fileInfo) {
return false;
}

public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
$maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50);
$size = $fileInfo->getSize();
$size = $file->getSize();

if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
return false;
}

$image = new \OC_Image();
$stream = $file->fopen('r');

$useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal();
if ($useTempFile) {
$fileName = $fileview->toTmpFile($path);
} else {
$fileName = $fileview->getLocalFile($path);
}
$image->loadFromFile($fileName);
$image->loadFromFileHandle($stream);
$image->fixOrientation();
if ($useTempFile) {
unlink($fileName);
}
fclose($stream);
if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY);

Expand All @@ -66,4 +56,10 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
return false;
}

/**
* @inheritdoc
*/
public function isAvailable(File $file) {
return true;
}
}
34 changes: 28 additions & 6 deletions lib/private/Preview/MP3.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
namespace OC\Preview;

use ID3Parser\ID3Parser;
use OCP\Files\File;
use OCP\Preview\IProvider2;

class MP3 extends Provider {
class MP3 implements IProvider2 {
/**
* {@inheritDoc}
*/
Expand All @@ -38,12 +40,22 @@ public function getMimeType() {
/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
$getID3 = new ID3Parser();
public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {

$useFileDirectly = (!$file->isEncrypted() && !$file->isMounted());
if ($useFileDirectly) {
$absPath = $file->getStorage()->getLocalFile($file->getInternalPath());
} else {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();

$tmpPath = $fileview->toTmpFile($path);
$tags = $getID3->analyze($tmpPath);
unlink($tmpPath);
$handle = $file->fopen('rb');
file_put_contents($absPath, $handle);
fclose($handle);
}

$getID3 = new ID3Parser();
$tags = $getID3->analyze($absPath);
unlink($absPath);
$picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
$picture = $tags['id3v2']['PIC'][0]['data'];
Expand Down Expand Up @@ -80,4 +92,14 @@ private function getNoCoverThumbnail() {
return $image->valid() ? $image : false;
}

/**
* Check if a preview can be generated for $path
*
* @param File $file
* @return bool
* @since 10.1.0
*/
public function isAvailable(File $file) {
return true;
}
}
27 changes: 20 additions & 7 deletions lib/private/Preview/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
*/
namespace OC\Preview;

class Movie extends Provider {
use OCP\Files\File;
use OCP\Preview\IProvider2;

class Movie implements IProvider2 {
public static $avconvBinary;
public static $ffmpegBinary;
public static $atomicParsleyBinary;
Expand All @@ -46,23 +49,22 @@ public function getMimeType() {
/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
// TODO: use proc_open() and stream the source file ?
$fileInfo = $fileview->getFileInfo($path);
$useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());

$useFileDirectly = (!$file->isEncrypted() && !$file->isMounted());
if ($useFileDirectly) {
$absPath = $fileview->getLocalFile($path);
$absPath = $file->getStorage()->getLocalFile($file->getInternalPath());
} else {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();

$handle = $fileview->fopen($path, 'rb');
$handle = $file->fopen('rb');

// we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB.
// in some cases 1MB was no enough to generate thumbnail
$firstmb = stream_get_contents($handle, 5242880);
file_put_contents($absPath, $firstmb);
fclose($handle);
}

$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
Expand Down Expand Up @@ -174,4 +176,15 @@ private function generateThumbNail($maxX, $maxY, $absPath, $second) {
}
return false;
}

/**
* Check if a preview can be generated for $path
*
* @param File $file
* @return bool
* @since 10.1.0
*/
public function isAvailable(File $file) {
return true;
}
}
25 changes: 22 additions & 3 deletions lib/private/Preview/Office.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,31 @@
*/
namespace OC\Preview;

abstract class Office extends Provider {
use OCP\Files\File;
use OCP\Preview\IProvider2;

abstract class Office implements IProvider2 {
private $cmd;

/**
* {@inheritDoc}
*/
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
$this->initCmd();
if (is_null($this->cmd)) {
return false;
}

$absPath = $fileview->toTmpFile($path);
$useFileDirectly = (!$file->isEncrypted() && !$file->isMounted());
if ($useFileDirectly) {
$absPath = $file->getStorage()->getLocalFile($file->getInternalPath());
} else {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();

$handle = $file->fopen('rb');
file_put_contents($absPath, $handle);
fclose($handle);
}

$tmpDir = \OC::$server->getTempManager()->getTempBaseDir();

Expand Down Expand Up @@ -77,6 +89,13 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {

}

/**
* @inheritdoc
*/
public function isAvailable(File $file) {
return true;
}

private function initCmd() {
$cmd = '';

Expand Down
57 changes: 0 additions & 57 deletions lib/private/Preview/Provider.php

This file was deleted.

Loading

0 comments on commit 95e0e0c

Please sign in to comment.