Passed
Pull Request — master (#616)
by Matias
08:34 queued 06:32
created

Imaginary::getInfo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 28
ccs 0
cts 19
cp 0
crap 12
rs 9.7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2022, 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
use Psr\Log\LoggerInterface;
32
33
class Imaginary {
34
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var IClientService */
39
	private $service;
40
41
	/** @var LoggerInterface */
42
	private $logger;
43
44 5
	public function __construct() {
45 5
		$this->config = \OC::$server->get(IConfig::class);
46 5
		$this->service = \OC::$server->get(IClientService::class);
47 5
		$this->logger = \OC::$server->get(LoggerInterface::class);
48
	}
49
50 5
	public function isEnabled(): bool {
51 5
		$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
52 5
		return ($imaginaryUrl !== 'invalid');
53
	}
54
55
	public function getInfo(string $filepath): array {
56
57
		$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
58
		$imaginaryUrl = rtrim($imaginaryUrl, '/');
59
60
		$httpClient = $this->service->newClient();
61
62
		$options['multipart'] = [[
0 ignored issues
show
Comprehensibility Best Practice introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.
Loading history...
63
			'name' => 'file',
64
			'contents' => file_get_contents($filepath),
65
			'filename' => basename($filepath),
66
		]];
67
68
		try {
69
			$response = $httpClient->post($imaginaryUrl . '/info', $options);
70
		} catch (\Exception $e) {
71
			$this->logger->error('Error getting image information in Imaginary: ' . $e->getMessage(), [
72
				'exception' => $e,
73
			]);
74
			return [];
75
		}
76
77
		if ($response->getStatusCode() !== 200) {
78
			$this->logger->error('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

78
			$this->logger->error('Error getting image information in Imaginary: ' . json_decode(/** @scrutinizer ignore-type */ $response->getBody())['message']);
Loading history...
79
			return [];
80
		}
81
82
		return json_decode($response->getBody(), true);
83
	}
84
85
	/**
86
	 * @return false|resource|\GdImage Returns the resized image
87
	 */
88
	public function getResized(string $filepath, int $width, int $height, string $mimeType) {
89
90
		$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
91
		$imaginaryUrl = rtrim($imaginaryUrl, '/');
92
93
		// Object store
94
		$stream = fopen($filepath, 'r');
0 ignored issues
show
Unused Code introduced by
The assignment to $stream is dead and can be removed.
Loading history...
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
				'operation' => 'autorotate',
109
			],
110
			[
111
				'operation' => 'resize',
112
				'params' => [
113
					'width' => $width,
114
					'height' => $height,
115
					'stripmeta' => 'true',
116
					'type' => $type,
117
					'norotation' => 'true',
118
				]
119
			]
120
		];
121
122
		try {
123
			$response = $httpClient->post(
124
				$imaginaryUrl . '/pipeline', [
125
					'query' => ['operations' => json_encode($operations)],
126
					'body' => file_get_contents($filepath),
127
					'nextcloud' => ['allow_local_address' => true],
128
				]);
129
		} catch (\Exception $e) {
130
			$this->logger->error('Error generating temporary image in Imaginary: ' . $e->getMessage(), [
131
				'exception' => $e,
132
			]);
133
			return false;
134
		}
135
136
		if ($response->getStatusCode() !== 200) {
137
			$this->logger->error('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

137
			$this->logger->error('Error generating temporary image in Imaginary: ' . json_decode(/** @scrutinizer ignore-type */ $response->getBody())['message']);
Loading history...
138
			return false;
139
		}
140
141
		$body = $response->getBody();
142
143
		return $body;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $body also could return the type string which is incompatible with the documented return type GdImage|false|resource.
Loading history...
144
	}
145
146
}
147