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 \GuzzleHttp\Client; |
26
|
|
|
use \GuzzleHttp\Post\PostFile; |
27
|
|
|
use OCP\Http\Client\IClient; |
28
|
|
|
use OCP\Http\Client\IClientService; |
29
|
|
|
use OCP\IConfig; |
30
|
|
|
|
31
|
|
|
abstract class Office extends Provider { |
32
|
|
|
|
33
|
|
|
/** @var IClientService */ |
34
|
|
|
private $clientService; |
35
|
|
|
|
36
|
|
|
/** @var IConfig */ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
public function __construct(IClientService $clientService, IConfig $config) { |
40
|
|
|
$this->clientService = $clientService; |
41
|
|
|
$this->config = $config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function getWopiURL(): string { |
45
|
|
|
return $this->config->getAppValue('richdocuments', 'wopi_url'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
52
|
|
|
$stream = $fileview->fopen($path, 'r'); |
53
|
|
|
|
54
|
|
|
$client = $this->clientService->newClient(); |
55
|
|
|
try { |
56
|
|
|
// Since we upgraded guzzle in NC14 we have to do some dark magic here |
57
|
|
|
if (version_compare($this->config->getSystemValue('version'), '14.0.0.0', '<')) { |
58
|
|
|
$response = $client->post($this->getWopiURL() . '/lool/convert-to/png', [ |
59
|
|
|
'timeout' => 10.0, |
60
|
|
|
'body' => [ |
61
|
|
|
new PostFile($path, $stream), |
62
|
|
|
], |
63
|
|
|
]); |
64
|
|
|
} else { |
65
|
|
|
$response = $client->post($this->getWopiURL() . '/lool/convert-to/png', [ |
66
|
|
|
'timeout' => 10.0, |
67
|
|
|
'multipart' => [ |
68
|
|
|
[ |
69
|
|
|
'name' => $path, |
70
|
|
|
'contents' => $stream, |
71
|
|
|
] |
72
|
|
|
], |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
} catch (\Exception $e) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$image = new \OC_Image(); |
80
|
|
|
$image->loadFromData($response->getBody()); |
81
|
|
|
|
82
|
|
|
if ($image->valid()) { |
83
|
|
|
$image->scaleDownToFit($maxX, $maxY); |
84
|
|
|
return $image; |
85
|
|
|
} |
86
|
|
|
return false; |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|