Skip to content

Commit 047cab8

Browse files
committed
Use findBinaryPath for previews
Signed-off-by: J0WI <J0WI@users.noreply.github.com>
1 parent 623ac8c commit 047cab8

7 files changed

Lines changed: 94 additions & 80 deletions

File tree

build/psalm-baseline.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,16 +4319,6 @@
43194319
<code>$second</code>
43204320
</InvalidScalarArgument>
43214321
</file>
4322-
<file src="lib/private/Preview/Office.php">
4323-
<ForbiddenCode occurrences="3">
4324-
<code>shell_exec($exec)</code>
4325-
<code>shell_exec('command -v libreoffice')</code>
4326-
<code>shell_exec('command -v openoffice')</code>
4327-
</ForbiddenCode>
4328-
<ImplicitToStringCast occurrences="1">
4329-
<code>$png</code>
4330-
</ImplicitToStringCast>
4331-
</file>
43324322
<file src="lib/private/Preview/ProviderV1Adapter.php">
43334323
<InvalidReturnStatement occurrences="1">
43344324
<code>$thumbnail === false ? null: $thumbnail</code>
@@ -4350,12 +4340,6 @@
43504340
<code>$svg</code>
43514341
</ImplicitToStringCast>
43524342
</file>
4353-
<file src="lib/private/PreviewManager.php">
4354-
<ForbiddenCode occurrences="2">
4355-
<code>shell_exec('command -v libreoffice')</code>
4356-
<code>shell_exec('command -v openoffice')</code>
4357-
</ForbiddenCode>
4358-
</file>
43594343
<file src="lib/private/RedisFactory.php">
43604344
<InvalidScalarArgument occurrences="1">
43614345
<code>\RedisCluster::OPT_SLAVE_FAILOVER</code>

lib/private/Preview/HEIC.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
namespace OC\Preview;
3131

3232
use OCP\Files\File;
33+
use OCP\Files\FileInfo;
3334
use OCP\IImage;
3435
use OCP\ILogger;
3536

@@ -49,14 +50,18 @@ public function getMimeType(): string {
4950
/**
5051
* {@inheritDoc}
5152
*/
52-
public function isAvailable(\OCP\Files\FileInfo $file): bool {
53+
public function isAvailable(FileInfo $file): bool {
5354
return in_array('HEIC', \Imagick::queryFormats("HEI*"));
5455
}
5556

5657
/**
5758
* {@inheritDoc}
5859
*/
5960
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
61+
if (!$this->isAvailable($file)) {
62+
return null;
63+
}
64+
6065
$tmpPath = $this->getLocalFile($file);
6166

6267
// Creates \Imagick object from the heic file

lib/private/Preview/Movie.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,61 @@
3030
namespace OC\Preview;
3131

3232
use OCP\Files\File;
33+
use OCP\Files\FileInfo;
3334
use OCP\IImage;
3435
use Psr\Log\LoggerInterface;
3536

3637
class Movie extends ProviderV2 {
38+
39+
/**
40+
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
41+
* @var string
42+
*/
3743
public static $avconvBinary;
44+
45+
/**
46+
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
47+
* @var string
48+
*/
3849
public static $ffmpegBinary;
3950

51+
/** @var string */
52+
private $binary;
53+
4054
/**
4155
* {@inheritDoc}
4256
*/
4357
public function getMimeType(): string {
4458
return '/video\/.*/';
4559
}
4660

61+
/**
62+
* {@inheritDoc}
63+
*/
64+
public function isAvailable(FileInfo $file): bool {
65+
// TODO: remove when avconv is dropped
66+
if (is_null($this->binary)) {
67+
if (isset($this->options['movieBinary'])) {
68+
$this->binary = $this->options['movieBinary'];
69+
} elseif (is_string(self::$avconvBinary)) {
70+
$this->binary = self::$avconvBinary;
71+
} elseif (is_string(self::$ffmpegBinary)) {
72+
$this->binary = self::$ffmpegBinary;
73+
}
74+
}
75+
return is_string($this->binary);
76+
}
77+
4778
/**
4879
* {@inheritDoc}
4980
*/
5081
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
5182
// TODO: use proc_open() and stream the source file ?
5283
84+
if (!$this->isAvailable($file)) {
85+
return null;
86+
}
87+
5388
$result = null;
5489
if ($this->useTempFile($file)) {
5590
// try downloading 5 MB first as it's likely that the first frames are present there
@@ -92,17 +127,23 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
92127
private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage {
93128
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
94129

95-
if (self::$avconvBinary) {
96-
$cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
130+
$binaryType = substr(strrchr($this->binary, '/'), 1);
131+
132+
if ($binaryType === 'avconv') {
133+
$cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
97134
' -i ' . escapeshellarg($absPath) .
98135
' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
99136
' 2>&1';
100-
} else {
101-
$cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
137+
} elseif ($binaryType === 'ffmpeg') {
138+
$cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
102139
' -i ' . escapeshellarg($absPath) .
103140
' -f mjpeg -vframes 1' .
104141
' ' . escapeshellarg($tmpPath) .
105142
' 2>&1';
143+
} else {
144+
// Not supported
145+
unlink($tmpPath);
146+
return null;
106147
}
107148

108149
exec($cmd, $output, $returnCode);

lib/private/Preview/Office.php

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@
2929
namespace OC\Preview;
3030

3131
use OCP\Files\File;
32+
use OCP\Files\FileInfo;
3233
use OCP\IImage;
3334
use OCP\ILogger;
3435

3536
abstract class Office extends ProviderV2 {
36-
private $cmd;
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
public function isAvailable(FileInfo $file): bool {
41+
return is_string($this->options['officeBinary']);
42+
}
3743

3844
/**
3945
* {@inheritDoc}
4046
*/
4147
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
42-
$this->initCmd();
43-
if (is_null($this->cmd)) {
48+
if (!$this->isAvailable($file)) {
4449
return null;
4550
}
4651

@@ -51,9 +56,14 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
5156
$defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ';
5257
$clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters);
5358

54-
$exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
59+
$cmd = $this->options['officeBinary'] . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
60+
61+
exec($cmd, $output, $returnCode);
5562

56-
shell_exec($exec);
63+
if ($returnCode !== 0) {
64+
$this->cleanTmpFiles();
65+
return null;
66+
}
5767

5868
//create imagick object from png
5969
$pngPreview = null;
@@ -74,7 +84,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
7484
}
7585

7686
$image = new \OC_Image();
77-
$image->loadFromData($png);
87+
$image->loadFromData((string) $png);
7888

7989
$this->cleanTmpFiles();
8090
unlink($pngPreview);
@@ -86,29 +96,4 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8696
}
8797
return null;
8898
}
89-
90-
private function initCmd() {
91-
$cmd = '';
92-
93-
$libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null);
94-
if (is_string($libreOfficePath)) {
95-
$cmd = $libreOfficePath;
96-
}
97-
98-
$whichLibreOffice = shell_exec('command -v libreoffice');
99-
if ($cmd === '' && !empty($whichLibreOffice)) {
100-
$cmd = 'libreoffice';
101-
}
102-
103-
$whichOpenOffice = shell_exec('command -v openoffice');
104-
if ($cmd === '' && !empty($whichOpenOffice)) {
105-
$cmd = 'openoffice';
106-
}
107-
108-
if ($cmd === '') {
109-
$cmd = null;
110-
}
111-
112-
$this->cmd = $cmd;
113-
}
11499
}

lib/private/Preview/ProviderV2.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
use OCP\Preview\IProviderV2;
3232

3333
abstract class ProviderV2 implements IProviderV2 {
34-
private $options;
34+
/** @var array */
35+
protected $options;
3536

37+
/** @var array */
3638
private $tmpFiles = [];
3739

3840
/**

lib/private/Preview/TXT.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function isAvailable(FileInfo $file): bool {
5252
* {@inheritDoc}
5353
*/
5454
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
55+
if (!$this->isAvailable($file)) {
56+
return null;
57+
}
58+
5559
$content = $file->fopen('r');
5660

5761
if ($content === false) {

lib/private/PreviewManager.php

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -417,41 +417,34 @@ protected function registerCoreProviders() {
417417
}
418418

419419
if (count($checkImagick->queryFormats('PDF')) === 1) {
420-
if (\OC_Helper::is_function_enabled('shell_exec')) {
421-
$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
422-
423-
if (!$officeFound) {
424-
//let's see if there is libreoffice or openoffice on this machine
425-
$whichLibreOffice = shell_exec('command -v libreoffice');
426-
$officeFound = !empty($whichLibreOffice);
427-
if (!$officeFound) {
428-
$whichOpenOffice = shell_exec('command -v openoffice');
429-
$officeFound = !empty($whichOpenOffice);
430-
}
431-
}
420+
// Office requires openoffice or libreoffice
421+
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null);
422+
if (is_null($officeBinary)) {
423+
$officeBinary = \OC_Helper::findBinaryPath('libreoffice');
424+
}
425+
if (is_null($officeBinary)) {
426+
$officeBinary = \OC_Helper::findBinaryPath('openoffice');
427+
}
432428

433-
if ($officeFound) {
434-
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/');
435-
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/');
436-
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/');
437-
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
438-
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/');
439-
}
429+
if (is_string($officeBinary)) {
430+
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/', ["officeBinary" => $officeBinary]);
431+
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/', ["officeBinary" => $officeBinary]);
432+
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/', ["officeBinary" => $officeBinary]);
433+
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/', ["officeBinary" => $officeBinary]);
434+
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/', ["officeBinary" => $officeBinary]);
440435
}
441436
}
442437
}
443438

444439
// Video requires avconv or ffmpeg
445440
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
446-
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
447-
$ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg');
448-
449-
if ($avconvBinary || $ffmpegBinary) {
450-
// FIXME // a bit hacky but didn't want to use subclasses
451-
\OC\Preview\Movie::$avconvBinary = $avconvBinary;
452-
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
441+
$movieBinary = \OC_Helper::findBinaryPath('avconv');
442+
if (is_null($movieBinary)) {
443+
$movieBinary = \OC_Helper::findBinaryPath('ffmpeg');
444+
}
453445

454-
$this->registerCoreProvider(Preview\Movie::class, '/video\/.*/');
446+
if (is_string($movieBinary)) {
447+
$this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ["movieBinary" => $movieBinary]);
455448
}
456449
}
457450
}

0 commit comments

Comments
 (0)