Passed
Pull Request — master (#751)
by Matias
07:33 queued 04:51
created

CompressionService::decompress()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 15.3773

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 19
ccs 8
cts 14
cp 0.5714
crap 15.3773
rs 8.0555
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @copyright Copyright (c) 2019-2023 Matias De lellis <[email protected]>
6
 *
7
 * @author Matias De lellis <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\FaceRecognition\Service;
27
28
class CompressionService {
29
30
	/**
31
	 * Decompress according to file extension
32
	 *
33
	 * @param string $inputFile
34
	 * @param string $outputFile
35
	 *
36
	 * @throws \Exception
37
	 *
38
	 * @return void
39
	 */
40 1
	public function decompress(string $inputFile, string $outputFile): void {
41 1
		if (!file_exists ($inputFile) || !is_readable ($inputFile))
42
			throw new \Exception('The file ' . $inputFile . ' not exists or is not readable');
43
44 1
		if ((!file_exists($outputFile) && !is_writeable(dirname($outputFile))) ||
45 1
		    (file_exists($outputFile) && !is_writable($outputFile)))
46
			throw new \Exception('The file ' . $outputFile . ' exists or is not writable');
47
48 1
		$extension = pathinfo($inputFile, PATHINFO_EXTENSION);
49
		switch ($extension) {
50 1
		case 'bz2':
51 1
			$this->bunzip2($inputFile, $outputFile);
52 1
			break;
53
		case '7z':
54
			$this->un7z($inputFile, $outputFile);
55
			break;
56
		default:
57
			throw new \Exception('Unsupported file format: ' . $extension);
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

57
			throw new \Exception('Unsupported file format: ' . /** @scrutinizer ignore-type */ $extension);
Loading history...
58
			break;
59
		}
60
	}
61
62 1
	private function bunzip2(string $inputFile, string $outputFile): void {
63 1
		$in_file = bzopen ($inputFile, "r");
64 1
		$out_file = fopen ($outputFile, "w");
65
66 1
		if ($out_file === false)
67
			throw new \Exception('Could not open the file to write: ' . $outputFile);
68
69 1
		while ($buffer = bzread ($in_file, 4096)) {
70 1
			if($buffer === false)
71
				throw new \Exception('Read problem: ' . bzerrstr($in_file));
72 1
			if(bzerrno($in_file) !== 0)
73
				throw new \Exception('Compression problem: '. bzerrstr($in_file));
74
75 1
			fwrite ($out_file, $buffer, 4096);
76
		}
77
78 1
		bzclose ($in_file);
79 1
		fclose ($out_file);
80
	}
81
82
	private function un7z(string $inputFile, string $outputFile): void {
83
		$cmd = '7z x ' . $inputFile . ' -o/tmp/';
84
		$output = null;
85
		$retval = null;
86
		exec($cmd, $output, $retval);
87
		if ($retval != 0)
88
			throw new \Exception('Fail to extract ' . $inputFile .  ': ' . $output);
89
		rename('/tmp/' . basename($outputFile), $outputFile);
90
	}
91
92
}
93