diff options
Diffstat (limited to 'lib/private/Preview')
-rw-r--r-- | lib/private/Preview/HEIC.php | 7 | ||||
-rw-r--r-- | lib/private/Preview/Movie.php | 49 | ||||
-rw-r--r-- | lib/private/Preview/Office.php | 47 | ||||
-rw-r--r-- | lib/private/Preview/ProviderV2.php | 4 | ||||
-rw-r--r-- | lib/private/Preview/TXT.php | 4 |
5 files changed, 74 insertions, 37 deletions
diff --git a/lib/private/Preview/HEIC.php b/lib/private/Preview/HEIC.php index 68c83fad97d..f2d43564c99 100644 --- a/lib/private/Preview/HEIC.php +++ b/lib/private/Preview/HEIC.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OC\Preview; use OCP\Files\File; +use OCP\Files\FileInfo; use OCP\IImage; use OCP\ILogger; @@ -49,7 +50,7 @@ class HEIC extends ProviderV2 { /** * {@inheritDoc} */ - public function isAvailable(\OCP\Files\FileInfo $file): bool { + public function isAvailable(FileInfo $file): bool { return in_array('HEIC', \Imagick::queryFormats("HEI*")); } @@ -57,6 +58,10 @@ class HEIC extends ProviderV2 { * {@inheritDoc} */ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + if (!$this->isAvailable($file)) { + return null; + } + $tmpPath = $this->getLocalFile($file); // Creates \Imagick object from the heic file diff --git a/lib/private/Preview/Movie.php b/lib/private/Preview/Movie.php index b139758596c..eab81e1ed48 100644 --- a/lib/private/Preview/Movie.php +++ b/lib/private/Preview/Movie.php @@ -30,13 +30,27 @@ namespace OC\Preview; use OCP\Files\File; +use OCP\Files\FileInfo; use OCP\IImage; use Psr\Log\LoggerInterface; class Movie extends ProviderV2 { + + /** + * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 + * @var string + */ public static $avconvBinary; + + /** + * @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2 + * @var string + */ public static $ffmpegBinary; + /** @var string */ + private $binary; + /** * {@inheritDoc} */ @@ -47,9 +61,30 @@ class Movie extends ProviderV2 { /** * {@inheritDoc} */ + public function isAvailable(FileInfo $file): bool { + // TODO: remove when avconv is dropped + if (is_null($this->binary)) { + if (isset($this->options['movieBinary'])) { + $this->binary = $this->options['movieBinary']; + } elseif (is_string(self::$avconvBinary)) { + $this->binary = self::$avconvBinary; + } elseif (is_string(self::$ffmpegBinary)) { + $this->binary = self::$ffmpegBinary; + } + } + return is_string($this->binary); + } + + /** + * {@inheritDoc} + */ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { // TODO: use proc_open() and stream the source file ? + if (!$this->isAvailable($file)) { + return null; + } + $result = null; if ($this->useTempFile($file)) { // try downloading 5 MB first as it's likely that the first frames are present there @@ -92,17 +127,23 @@ class Movie extends ProviderV2 { private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage { $tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); - if (self::$avconvBinary) { - $cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) . + $binaryType = substr(strrchr($this->binary, '/'), 1); + + if ($binaryType === 'avconv') { + $cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) . ' -i ' . escapeshellarg($absPath) . ' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) . ' 2>&1'; - } else { - $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) . + } elseif ($binaryType === 'ffmpeg') { + $cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) . ' -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1' . ' ' . escapeshellarg($tmpPath) . ' 2>&1'; + } else { + // Not supported + unlink($tmpPath); + return null; } exec($cmd, $output, $returnCode); diff --git a/lib/private/Preview/Office.php b/lib/private/Preview/Office.php index fef218823e2..b16544b3b23 100644 --- a/lib/private/Preview/Office.php +++ b/lib/private/Preview/Office.php @@ -29,18 +29,23 @@ namespace OC\Preview; use OCP\Files\File; +use OCP\Files\FileInfo; use OCP\IImage; use OCP\ILogger; abstract class Office extends ProviderV2 { - private $cmd; + /** + * {@inheritDoc} + */ + public function isAvailable(FileInfo $file): bool { + return is_string($this->options['officeBinary']); + } /** * {@inheritDoc} */ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { - $this->initCmd(); - if (is_null($this->cmd)) { + if (!$this->isAvailable($file)) { return null; } @@ -51,9 +56,14 @@ abstract class Office extends ProviderV2 { $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir '; $clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters); - $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); + $cmd = $this->options['officeBinary'] . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); + + exec($cmd, $output, $returnCode); - shell_exec($exec); + if ($returnCode !== 0) { + $this->cleanTmpFiles(); + return null; + } //create imagick object from png $pngPreview = null; @@ -74,7 +84,7 @@ abstract class Office extends ProviderV2 { } $image = new \OC_Image(); - $image->loadFromData($png); + $image->loadFromData((string) $png); $this->cleanTmpFiles(); unlink($pngPreview); @@ -86,29 +96,4 @@ abstract class Office extends ProviderV2 { } return null; } - - private function initCmd() { - $cmd = ''; - - $libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null); - if (is_string($libreOfficePath)) { - $cmd = $libreOfficePath; - } - - $whichLibreOffice = shell_exec('command -v libreoffice'); - if ($cmd === '' && !empty($whichLibreOffice)) { - $cmd = 'libreoffice'; - } - - $whichOpenOffice = shell_exec('command -v openoffice'); - if ($cmd === '' && !empty($whichOpenOffice)) { - $cmd = 'openoffice'; - } - - if ($cmd === '') { - $cmd = null; - } - - $this->cmd = $cmd; - } } diff --git a/lib/private/Preview/ProviderV2.php b/lib/private/Preview/ProviderV2.php index 1b5bee0e5cc..4323f149702 100644 --- a/lib/private/Preview/ProviderV2.php +++ b/lib/private/Preview/ProviderV2.php @@ -31,8 +31,10 @@ use OCP\IImage; use OCP\Preview\IProviderV2; abstract class ProviderV2 implements IProviderV2 { - private $options; + /** @var array */ + protected $options; + /** @var array */ private $tmpFiles = []; /** diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php index 0ae35fb3eec..d55d32386a7 100644 --- a/lib/private/Preview/TXT.php +++ b/lib/private/Preview/TXT.php @@ -52,6 +52,10 @@ class TXT extends ProviderV2 { * {@inheritDoc} */ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + if (!$this->isAvailable($file)) { + return null; + } + $content = $file->fopen('r'); if ($content === false) { |