1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use dariusiii\rarinfo\ArchiveInfo; |
6
|
|
|
|
7
|
|
|
class ArchiveProcessingService |
8
|
|
|
{ |
9
|
|
|
public function __construct(private readonly ArchiveInfo $archiveInfo) {} |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Analyze compressed data (RAR/ZIP/etc.). |
13
|
|
|
* Returns an array with: ok(bool), error(?string), summary(?array), is_encrypted(bool) |
14
|
|
|
*/ |
15
|
|
|
public function analyze(string $compressedData): array |
16
|
|
|
{ |
17
|
|
|
$ok = $this->archiveInfo->setData($compressedData, true); |
18
|
|
|
if (! $ok) { |
19
|
|
|
return ['ok' => false, 'error' => $this->archiveInfo->error ?: 'Unknown error', 'summary' => null, 'is_encrypted' => false]; |
20
|
|
|
} |
21
|
|
|
if ($this->archiveInfo->error !== '') { |
22
|
|
|
return ['ok' => false, 'error' => $this->archiveInfo->error, 'summary' => null, 'is_encrypted' => false]; |
23
|
|
|
} |
24
|
|
|
$summary = $this->archiveInfo->getSummary(true); |
25
|
|
|
$isEncrypted = ! empty($this->archiveInfo->isEncrypted) || (isset($summary['is_encrypted']) && (int) $summary['is_encrypted'] !== 0); |
26
|
|
|
|
27
|
|
|
return ['ok' => true, 'error' => null, 'summary' => $summary, 'is_encrypted' => $isEncrypted]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get file list from currently analyzed archive. |
32
|
|
|
*/ |
33
|
|
|
public function getFileList(): array |
34
|
|
|
{ |
35
|
|
|
return $this->archiveInfo->getArchiveFileList() ?: []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get file contents by name and source from the current archive. |
40
|
|
|
*/ |
41
|
|
|
public function getFileData(string $name, string $source): string|false |
42
|
|
|
{ |
43
|
|
|
return $this->archiveInfo->getFileData($name, $source); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Extract file by name from the current archive to destination path. |
48
|
|
|
*/ |
49
|
|
|
public function extractFile(string $name, string $destPath): bool |
50
|
|
|
{ |
51
|
|
|
return $this->archiveInfo->extractFile($name, $destPath); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|