Completed
Pull Request — master (#29319)
by
unknown
14:31 queued 02:38
created

Office::getThumbnail()   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 55
Code Lines 36

Duplication

Lines 9
Ratio 16.36 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nc 25
nop 4
dl 9
loc 55
rs 8.7752
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Olivier Paroz <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2018, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
namespace OC\Preview;
26
27
use OCP\Files\File;
28
use OCP\Preview\IProvider2;
29
30
abstract class Office implements IProvider2 {
31
	private $cmd;
32
33
	/**
34
	 * {@inheritDoc}
35
	 */
36
	public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
37
		$this->initCmd();
38
		if (is_null($this->cmd)) {
39
			return false;
40
		}
41
42
		$useFileDirectly = (!$file->isEncrypted() && !$file->isMounted());
43 View Code Duplication
		if ($useFileDirectly) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
			$absPath = $file->getStorage()->getLocalFile($file->getInternalPath());
45
		} else {
46
			$absPath = \OC::$server->getTempManager()->getTemporaryFile();
47
48
			$handle = $file->fopen('rb');
49
			file_put_contents($absPath, $handle);
50
			fclose($handle);
51
		}
52
53
		$tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
54
55
		$defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir ';
56
		$clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters);
57
58
		$exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
59
60
		shell_exec($exec);
61
62
		//create imagick object from pdf
63
		$pdfPreview = null;
64
		try {
65
			$pathInfo = pathinfo($absPath);
66
			$pdfPreview = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.pdf';
67
68
			$pdf = new \imagick($pdfPreview . '[0]');
69
			$pdf->setImageFormat('jpg');
70
		} catch (\Exception $e) {
71
			unlink($absPath);
72
			@unlink($pdfPreview);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
73
			\OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR);
74
			return false;
75
		}
76
77
		$image = new \OC_Image();
78
		$image->loadFromData($pdf);
79
80
		unlink($absPath);
81
		unlink($pdfPreview);
82
83
		if ($image->valid()) {
84
			$image->scaleDownToFit($maxX, $maxY);
85
86
			return $image;
87
		}
88
		return false;
89
90
	}
91
92
	/**
93
	 * @inheritdoc
94
	 */
95
	public function isAvailable(File $file) {
96
		return true;
97
	}
98
99
	private function initCmd() {
100
		$cmd = '';
101
102
		$libreOfficePath = \OC::$server->getConfig()->getSystemValue('preview_libreoffice_path', null);
103
		if (is_string($libreOfficePath)) {
104
			$cmd = $libreOfficePath;
105
		}
106
107
		$whichLibreOffice = shell_exec('command -v libreoffice');
108
		if ($cmd === '' && !empty($whichLibreOffice)) {
109
			$cmd = 'libreoffice';
110
		}
111
112
		$whichOpenOffice = shell_exec('command -v openoffice');
113
		if ($cmd === '' && !empty($whichOpenOffice)) {
114
			$cmd = 'openoffice';
115
		}
116
117
		if ($cmd === '') {
118
			$cmd = null;
119
		}
120
121
		$this->cmd = $cmd;
122
	}
123
}
124