Passed
Push — master ( fe5b0d...b4efe6 )
by Matias
03:29
created

Imaginary::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
The type OC\StreamImage was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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']);
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
			throw new \RuntimeException('Error getting image information in Imaginary: ' . json_decode(/** @scrutinizer ignore-type */ $response->getBody())['message']);
Loading history...
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']);
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
			throw new \RuntimeException('Error generating temporary image in Imaginary: ' . json_decode(/** @scrutinizer ignore-type */ $response->getBody())['message']);
Loading history...
125
		}
126
127
		return $response->getBody();
128
	}
129
130
}
131