Completed
Push — master ( 18c5b1...bc8eac )
by Julius
19:15 queued 17:39
created

Office::getThumbnail()   B

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 OC\Preview\Provider;
25
use OCA\Richdocuments\Capabilities;
26
use OCP\Http\Client\IClientService;
27
use OCP\IConfig;
28
use OCP\ILogger;
29
30
abstract class Office extends Provider {
31
32
	/** @var IClientService */
33
	private $clientService;
34
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var array */
39
	private $capabilitites;
40
41
	/** @var ILogger */
42
	private $logger;
43
44
	public function __construct(IClientService $clientService, IConfig $config, Capabilities $capabilities, ILogger $logger) {
45
		$this->clientService = $clientService;
46
		$this->config = $config;
47
		$this->capabilitites = $capabilities->getCapabilities()['richdocuments'];
48
		$this->logger = $logger;
49
	}
50
51
	private function getWopiURL() {
52
		return $this->config->getAppValue('richdocuments', 'wopi_url');
53
	}
54
55
	public function isAvailable(\OCP\Files\FileInfo $file) {
56
		if (isset($this->capabilitites['collabora']['convert-to'])) {
57
			return $this->capabilitites['collabora']['convert-to'];
58
		}
59
		return false;
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 */
65
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
66
		$fileInfo = $fileview->getFileInfo($path);
67
		if (!$fileInfo || $fileInfo->getSize() === 0) {
68
			return false;
69
		}
70
71
		$useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal();
72
		if ($useTempFile) {
73
			$fileName = $fileview->toTmpFile($path);
74
			$stream = fopen($fileName, 'r');
75
		} else {
76
			$stream = $fileview->fopen($path, 'r');
77
		}
78
79
		$client = $this->clientService->newClient();
80
		$options = ['timeout' => 10];
81
82
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
83
			$options['verify'] = false;
84
		}
85
86
		$options['multipart'] = [['name' => $path, 'contents' => $stream]];
87
88
		try {
89
			$response = $client->post($this->getWopiURL(). '/lool/convert-to/png', $options);
90
		} catch (\Exception $e) {
91
			$this->logger->logException($e, [
92
				'message' => 'Failed to convert file to preview',
93
				'level' => ILogger::INFO,
94
				'app' => 'richdocuments',
95
			]);
96
			return false;
97
		}
98
99
		$image = new \OC_Image();
100
		$image->loadFromData($response->getBody());
101
102
		if ($image->valid()) {
103
			$image->scaleDownToFit($maxX, $maxY);
104
			return $image;
105
		}
106
		return false;
107
108
	}
109
110
}
111