1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2022-2023, Matias De lellis |
4
|
|
|
* |
5
|
|
|
* @author Matias De lellis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license AGPL-3.0-or-later |
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
|
|
|
|
23
|
|
|
namespace OCA\FaceRecognition\Helper; |
24
|
|
|
|
25
|
|
|
use OCP\Files\File; |
26
|
|
|
use OCP\Http\Client\IClientService; |
27
|
|
|
use OCP\IConfig; |
28
|
|
|
use OCP\IImage; |
29
|
|
|
|
30
|
|
|
use OC\StreamImage; |
|
|
|
|
31
|
|
|
|
32
|
|
|
class Imaginary { |
33
|
|
|
|
34
|
|
|
/** @var IConfig */ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** @var IClientService */ |
38
|
|
|
private $service; |
39
|
|
|
|
40
|
5 |
|
public function __construct() { |
41
|
5 |
|
$this->config = \OC::$server->get(IConfig::class); |
42
|
5 |
|
$this->service = \OC::$server->get(IClientService::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
public function isEnabled(): bool { |
46
|
5 |
|
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
47
|
5 |
|
return ($imaginaryUrl !== 'invalid'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array Returns the array with the size of image. |
52
|
|
|
*/ |
53
|
|
|
public function getInfo(string $filepath): array { |
54
|
|
|
|
55
|
|
|
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
56
|
|
|
$imaginaryUrl = rtrim($imaginaryUrl, '/'); |
57
|
|
|
|
58
|
|
|
$httpClient = $this->service->newClient(); |
59
|
|
|
|
60
|
|
|
$options = []; |
61
|
|
|
$options['multipart'] = [[ |
62
|
|
|
'name' => 'file', |
63
|
|
|
'contents' => file_get_contents($filepath), |
64
|
|
|
'filename' => basename($filepath), |
65
|
|
|
]]; |
66
|
|
|
|
67
|
|
|
$response = $httpClient->post($imaginaryUrl . '/info', $options); |
68
|
|
|
|
69
|
|
|
if ($response->getStatusCode() !== 200) { |
70
|
|
|
throw new \RuntimeException('Error getting image information in Imaginary: ' . json_decode($response->getBody())['message']); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$info = json_decode($response->getBody(), true); |
74
|
|
|
return [ |
75
|
|
|
// Rotates the size, since it is important and Imaginary do not do that. |
76
|
|
|
'width' => $info['orientation'] < 5 ? $info['width'] : $info['height'], |
77
|
|
|
'height' => $info['orientation'] < 5 ? $info['height'] : $info['width'] |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string|resource Returns the resized image |
83
|
|
|
*/ |
84
|
|
|
public function getResized(string $filepath, int $width, int $height, string $mimeType) { |
85
|
|
|
|
86
|
|
|
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
87
|
|
|
$imaginaryUrl = rtrim($imaginaryUrl, '/'); |
88
|
|
|
|
89
|
|
|
$httpClient = $this->service->newClient(); |
90
|
|
|
|
91
|
|
|
switch ($mimeType) { |
92
|
|
|
case 'image/png': |
93
|
|
|
$type = 'png'; |
94
|
|
|
break; |
95
|
|
|
default: |
96
|
|
|
$type = 'jpeg'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$operations = [ |
100
|
|
|
[ |
101
|
|
|
'operation' => 'autorotate', |
102
|
|
|
], |
103
|
|
|
[ |
104
|
|
|
'operation' => 'resize', |
105
|
|
|
'params' => [ |
106
|
|
|
'width' => $width, |
107
|
|
|
'height' => $height, |
108
|
|
|
'stripmeta' => 'true', |
109
|
|
|
'type' => $type, |
110
|
|
|
'norotation' => 'true', |
111
|
|
|
'force' => 'true' |
112
|
|
|
] |
113
|
|
|
] |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$response = $httpClient->post( |
117
|
|
|
$imaginaryUrl . '/pipeline', [ |
118
|
|
|
'query' => ['operations' => json_encode($operations)], |
119
|
|
|
'body' => file_get_contents($filepath), |
120
|
|
|
'nextcloud' => ['allow_local_address' => true], |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
if ($response->getStatusCode() !== 200) { |
124
|
|
|
throw new \RuntimeException('Error generating temporary image in Imaginary: ' . json_decode($response->getBody())['message']); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $response->getBody(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths