Skip to content

Commit 58e97b1

Browse files
committed
Use findBinaryPath for previews
Signed-off-by: J0WI <[email protected]>
1 parent f6b61ac commit 58e97b1

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
@@ -4366,16 +4366,6 @@
43664366
<code>$second</code>
43674367
</InvalidScalarArgument>
43684368
</file>
4369-
<file src="lib/private/Preview/Office.php">
4370-
<ForbiddenCode occurrences="3">
4371-
<code>shell_exec($exec)</code>
4372-
<code>shell_exec('command -v libreoffice')</code>
4373-
<code>shell_exec('command -v openoffice')</code>
4374-
</ForbiddenCode>
4375-
<ImplicitToStringCast occurrences="1">
4376-
<code>$png</code>
4377-
</ImplicitToStringCast>
4378-
</file>
43794369
<file src="lib/private/Preview/ProviderV1Adapter.php">
43804370
<InvalidReturnStatement occurrences="1">
43814371
<code>$thumbnail === false ? null: $thumbnail</code>
@@ -4397,12 +4387,6 @@
43974387
<code>$svg</code>
43984388
</ImplicitToStringCast>
43994389
</file>
4400-
<file src="lib/private/PreviewManager.php">
4401-
<ForbiddenCode occurrences="2">
4402-
<code>shell_exec('command -v libreoffice')</code>
4403-
<code>shell_exec('command -v openoffice')</code>
4404-
</ForbiddenCode>
4405-
</file>
44064390
<file src="lib/private/RedisFactory.php">
44074391
<InvalidPropertyAssignmentValue occurrences="2">
44084392
<code>new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout)</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,25 +30,60 @@
3030
namespace OC\Preview;
3131

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

3536
class Movie extends ProviderV2 {
37+
38+
/**
39+
* @deprecated 22.0.0 pass option to \OCP\Preview\ProviderV2
40+
* @var string
41+
*/
3642
public static $avconvBinary;
43+
44+
/**
45+
* @deprecated 22.0.0 pass option to \OCP\Preview\ProviderV2
46+
* @var string
47+
*/
3748
public static $ffmpegBinary;
3849

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

60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function isAvailable(FileInfo $file): bool {
64+
// TODO: remove when avconv is dropped
65+
if (is_null($this->binary)) {
66+
if (isset($this->options['movieBinary'])) {
67+
$this->binary = $this->options['movieBinary'];
68+
} elseif (is_string(self::$avconvBinary)) {
69+
$this->binary = self::$avconvBinary;
70+
} elseif (is_string(self::$ffmpegBinary)) {
71+
$this->binary = self::$ffmpegBinary;
72+
}
73+
}
74+
return is_string($this->binary);
75+
}
76+
4677
/**
4778
* {@inheritDoc}
4879
*/
4980
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
5081
// TODO: use proc_open() and stream the source file ?
5182
83+
if (!$this->isAvailable($file)) {
84+
return null;
85+
}
86+
5287
$absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB
5388

5489
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
@@ -74,17 +109,23 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
74109
private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage {
75110
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
76111

77-
if (self::$avconvBinary) {
78-
$cmd = self::$avconvBinary . ' -y -ss ' . escapeshellarg($second) .
112+
$binaryType = substr(strrchr($this->binary, '/'), 1);
113+
114+
if ($binaryType === 'avconv') {
115+
$cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
79116
' -i ' . escapeshellarg($absPath) .
80117
' -an -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) .
81118
' > /dev/null 2>&1';
82-
} else {
83-
$cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) .
119+
} elseif ($binaryType === 'ffmpeg') {
120+
$cmd = $this->binary . ' -y -ss ' . escapeshellarg($second) .
84121
' -i ' . escapeshellarg($absPath) .
85122
' -f mjpeg -vframes 1' .
86123
' ' . escapeshellarg($tmpPath) .
87124
' > /dev/null 2>&1';
125+
} else {
126+
// Not supported
127+
unlink($tmpPath);
128+
return null;
88129
}
89130

90131
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
@@ -394,41 +394,34 @@ protected function registerCoreProviders() {
394394
}
395395

396396
if (count($checkImagick->queryFormats('PDF')) === 1) {
397-
if (\OC_Helper::is_function_enabled('shell_exec')) {
398-
$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
399-
400-
if (!$officeFound) {
401-
//let's see if there is libreoffice or openoffice on this machine
402-
$whichLibreOffice = shell_exec('command -v libreoffice');
403-
$officeFound = !empty($whichLibreOffice);
404-
if (!$officeFound) {
405-
$whichOpenOffice = shell_exec('command -v openoffice');
406-
$officeFound = !empty($whichOpenOffice);
407-
}
408-
}
397+
// Office requires openoffice or libreoffice
398+
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null);
399+
if (is_null($officeBinary)) {
400+
$officeBinary = \OC_Helper::findBinaryPath('libreoffice');
401+
}
402+
if (is_null($officeBinary)) {
403+
$officeBinary = \OC_Helper::findBinaryPath('openoffice');
404+
}
409405

410-
if ($officeFound) {
411-
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/');
412-
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/');
413-
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/');
414-
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
415-
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/');
416-
}
406+
if (is_string($officeBinary)) {
407+
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/', ["officeBinary" => $officeBinary]);
408+
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/', ["officeBinary" => $officeBinary]);
409+
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/', ["officeBinary" => $officeBinary]);
410+
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/', ["officeBinary" => $officeBinary]);
411+
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/', ["officeBinary" => $officeBinary]);
417412
}
418413
}
419414
}
420415

421416
// Video requires avconv or ffmpeg
422417
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
423-
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
424-
$ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg');
425-
426-
if ($avconvBinary || $ffmpegBinary) {
427-
// FIXME // a bit hacky but didn't want to use subclasses
428-
\OC\Preview\Movie::$avconvBinary = $avconvBinary;
429-
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
418+
$movieBinary = \OC_Helper::findBinaryPath('avconv');
419+
if (is_null($movieBinary)) {
420+
$movieBinary = \OC_Helper::findBinaryPath('ffmpeg');
421+
}
430422

431-
$this->registerCoreProvider(Preview\Movie::class, '/video\/.*/');
423+
if (is_string($movieBinary)) {
424+
$this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ["movieBinary" => $movieBinary]);
432425
}
433426
}
434427
}

0 commit comments

Comments
 (0)