|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/data-file |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/data-file |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Modify\Compress; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DataFile\Node\LocalFileNodeInterface; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use Psr\Log\LogLevel; |
|
19
|
|
|
|
|
20
|
|
|
trait DeCompressorTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Decompress a file and return the decompressed file |
|
24
|
|
|
* |
|
25
|
|
|
* @param LocalFileNodeInterface $node |
|
26
|
|
|
* @param array $options |
|
27
|
|
|
* |
|
28
|
|
|
* @return LocalFileNodeInterface |
|
29
|
|
|
*/ |
|
30
|
12 |
|
public function decompress(LocalFileNodeInterface $node, array $options = []) |
|
31
|
|
|
{ |
|
32
|
12 |
|
$pathInfo = pathinfo($node->getPath()); |
|
33
|
|
|
|
|
34
|
12 |
|
if (!$node->exists()) { |
|
35
|
2 |
|
throw new InvalidArgumentException("The file: $node does not exist"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
10 |
|
$outputFile = $node->getClone() |
|
39
|
10 |
|
->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename']) |
|
40
|
10 |
|
->setCompression(CompressionFactory::TYPE_NONE); |
|
41
|
|
|
|
|
42
|
10 |
|
$this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [ |
|
43
|
10 |
|
'file' => $node, |
|
44
|
10 |
|
'target' => $outputFile, |
|
45
|
10 |
|
'compression' => $this->getName(), |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
10 |
|
$cmd = $this->getDecompressCommand($node, $outputFile); |
|
49
|
|
|
|
|
50
|
10 |
|
$keepOld = (isset($options['keepOldFile'])) ? $options['keepOldFile'] : true; |
|
51
|
|
|
|
|
52
|
10 |
|
return $this->processFile($node, $outputFile, $cmd, $keepOld); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Abstract Log function that might should be handed by the OptionalLoggerTrait or similar |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $level |
|
59
|
|
|
* @param string $message |
|
60
|
|
|
* @param array $context |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
abstract protected function log($level, $message, array $context = []); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param LocalFileNodeInterface $node |
|
68
|
|
|
* @param LocalFileNodeInterface $outputFile |
|
69
|
|
|
* @param string $cmd |
|
70
|
|
|
* @param bool $keepOld |
|
71
|
|
|
* |
|
72
|
|
|
* @return LocalFileNodeInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract protected function processFile( |
|
75
|
|
|
LocalFileNodeInterface $node, |
|
76
|
|
|
LocalFileNodeInterface $outputFile, |
|
77
|
|
|
$cmd, |
|
78
|
|
|
$keepOld = true |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract public function getExtension(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
abstract public function getName(); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the command line to decompress a file |
|
93
|
|
|
* |
|
94
|
|
|
* @param LocalFileNodeInterface $from |
|
95
|
|
|
* @param LocalFileNodeInterface $to |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract public function getDecompressCommand(LocalFileNodeInterface $from, LocalFileNodeInterface $to); |
|
100
|
|
|
} |
|
101
|
|
|
|