matiasdelellis /
facerecognition
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | /** |
||
| 5 | * @copyright Copyright (c) 2019-2024 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 | default: |
||
| 54 | throw new \Exception('Unsupported file format: ' . $extension); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | break; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | 1 | private function bunzip2(string $inputFile, string $outputFile): void { |
|
| 60 | 1 | $in_file = bzopen ($inputFile, "r"); |
|
| 61 | 1 | $out_file = fopen ($outputFile, "w"); |
|
| 62 | |||
| 63 | 1 | if ($out_file === false) |
|
| 64 | throw new \Exception('Could not open the file to write: ' . $outputFile); |
||
| 65 | |||
| 66 | 1 | while ($buffer = bzread ($in_file, 4096)) { |
|
| 67 | 1 | if($buffer === false) |
|
| 68 | throw new \Exception('Read problem: ' . bzerrstr($in_file)); |
||
| 69 | 1 | if(bzerrno($in_file) !== 0) |
|
| 70 | throw new \Exception('Compression problem: '. bzerrstr($in_file)); |
||
| 71 | |||
| 72 | 1 | fwrite ($out_file, $buffer, 4096); |
|
| 73 | } |
||
| 74 | |||
| 75 | 1 | bzclose ($in_file); |
|
| 76 | 1 | fclose ($out_file); |
|
| 77 | } |
||
| 78 | |||
| 79 | } |
||
| 80 |