Office::getThumbnail()   B
last analyzed

Complexity

Conditions 8
Paths 25

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 36
cp 0
rs 7.9715
c 0
b 0
f 0
cc 8
nc 25
nop 5
crap 72
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, Collabora Productivity.
4
 *
5
 * @author Tor Lillqvist <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
namespace OCA\Richdocuments\Preview;
23
24
use GuzzleHttp\Psr7\LimitStream;
25
use function GuzzleHttp\Psr7\stream_for;
26
use OC\Preview\Provider;
27
use OCA\Richdocuments\Capabilities;
28
use OCP\Http\Client\IClientService;
29
use OCP\IConfig;
30
use OCP\ILogger;
31
use OCP\Image;
32
33
abstract class Office extends Provider {
34
35
	/** @var IClientService */
36
	private $clientService;
37
38
	/** @var IConfig */
39
	private $config;
40
41
	/** @var array */
42
	private $capabilitites;
43
44
	/** @var ILogger */
45
	private $logger;
46
47
	public function __construct(IClientService $clientService, IConfig $config, Capabilities $capabilities, ILogger $logger) {
48
		$this->clientService = $clientService;
49
		$this->config = $config;
50
		$this->capabilitites = $capabilities->getCapabilities()['richdocuments'];
51
		$this->logger = $logger;
52
	}
53
54
	private function getWopiURL() {
55
		return $this->config->getAppValue('richdocuments', 'wopi_url');
56
	}
57
58
	public function isAvailable(\OCP\Files\FileInfo $file) {
59
		if (isset($this->capabilitites['collabora']['convert-to']['available'])) {
60
			return (bool)$this->capabilitites['collabora']['convert-to']['available'];
61
		}
62
		return false;
63
	}
64
65
	/**
66
	 * {@inheritDoc}
67
	 */
68
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
69
		$fileInfo = $fileview->getFileInfo($path);
70
		if (!$fileInfo || $fileInfo->getSize() === 0) {
71
			return false;
72
		}
73
74
		$useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal();
75
		if ($useTempFile) {
76
			$fileName = $fileview->toTmpFile($path);
77
			$stream = fopen($fileName, 'r');
78
		} else {
79
			$stream = $fileview->fopen($path, 'r');
80
		}
81
82
		$client = $this->clientService->newClient();
83
		$options = ['timeout' => 10];
84
85
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
86
			$options['verify'] = false;
87
		}
88
89
		$options['multipart'] = [['name' => $path, 'contents' => $stream]];
90
91
		try {
92
			$response = $client->post($this->getWopiURL(). '/lool/convert-to/png', $options);
93
		} catch (\Exception $e) {
94
			$this->logger->logException($e, [
0 ignored issues
show
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method OCP\ILogger::logException() has been deprecated with message: 20.0.0 use the `exception` entry in the context of any method in \Psr\Log\LoggerInterface

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
95
				'message' => 'Failed to convert file to preview',
96
				'level' => ILogger::INFO,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated with message: 20.0.0

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
97
				'app' => 'richdocuments',
98
			]);
99
			return false;
100
		}
101
102
		$image = new Image();
103
		$image->loadFromData($response->getBody());
104
105
		if ($image->valid()) {
106
			$image->scaleDownToFit($maxX, $maxY);
107
			return $image;
108
		}
109
		return false;
110
111
	}
112
113
}
114