Passed
Push — master ( 2da96f...f3a9cc )
by Matias
13:54
created

Imaginary   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 9.84%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 56
c 5
b 1
f 0
dl 0
loc 106
ccs 6
cts 61
cp 0.0984
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 3 1
A __construct() 0 3 1
A getInfo() 0 32 5
A getResized() 0 47 4
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
75
		$type = $info['type'];
76
		//NOTE: Imaginary has problems rorating heic images. Issue #662
77
		$autorotate = ($info['orientation'] > 4 && $type != 'heif');
78
79
		return [
80
			'type'   => $type,
81
			'autorotate' => $autorotate,
82
			// Rotates the size, since it is important and Imaginary do not do that.
83
			'width'  => $autorotate ? $info['height'] : $info['width'],
84
			'height' => $autorotate ? $info['width'] :  $info['height']
85
		];
86
	}
87
88
	/**
89
	 * @return string|resource Returns the resized image
90
	 */
91
	public function getResized(string $filepath, int $width, int $height, bool $autorotate, string $mimeType) {
92
93
		$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
94
		$imaginaryUrl = rtrim($imaginaryUrl, '/');
95
96
		$httpClient = $this->service->newClient();
97
98
		switch ($mimeType) {
99
			case 'image/png':
100
				$type = 'png';
101
				break;
102
			default:
103
				$type = 'jpeg';
104
		}
105
106
		$operations = [];
107
108
		if ($autorotate) {
109
			$operations[] = [
110
				'operation' => 'autorotate',
111
			];
112
		}
113
114
		$operations[] = [
115
			'operation' => 'resize',
116
			'params' => [
117
				'width' => $width,
118
				'height' => $height,
119
				'stripmeta' => 'true',
120
				'type' => $type,
121
				'norotation' => 'true',
122
				'force' => 'true'
123
			]
124
		];
125
126
		$response = $httpClient->post(
127
			$imaginaryUrl . '/pipeline', [
128
				'query' => ['operations' => json_encode($operations)],
129
				'body' => file_get_contents($filepath),
130
				'nextcloud' => ['allow_local_address' => true],
131
			]);
132
133
		if ($response->getStatusCode() !== 200) {
134
			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

134
			throw new \RuntimeException('Error generating temporary image in Imaginary: ' . json_decode(/** @scrutinizer ignore-type */ $response->getBody())['message']);
Loading history...
135
		}
136
137
		return $response->getBody();
138
	}
139
140
}
141