1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace wapmorgan\UnifiedArchive\Commands; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use wapmorgan\UnifiedArchive\Abilities; |
7
|
|
|
use wapmorgan\UnifiedArchive\Drivers\Basic\BasicDriver; |
8
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
9
|
|
|
use wapmorgan\UnifiedArchive\UnifiedArchive; |
10
|
|
|
|
11
|
|
|
class BaseCommand extends \Symfony\Component\Console\Command\Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param $file |
15
|
|
|
* @param null $password |
|
|
|
|
16
|
|
|
* @return UnifiedArchive |
17
|
|
|
* @throws \Exception |
18
|
|
|
*/ |
19
|
|
|
protected function open($file, $password = null) |
20
|
|
|
{ |
21
|
|
|
if (!UnifiedArchive::canOpen($file, !empty($password))) { |
22
|
|
|
throw new \Exception( |
23
|
|
|
'Could not open archive ' . $file . '. Try installing suggested packages or run `cam -f` to see formats support.' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$archive = UnifiedArchive::open($file, [], $password); |
28
|
|
|
if ($archive === null) |
29
|
|
|
throw new \Exception('Could not open archive '.$file); |
30
|
|
|
|
31
|
|
|
return $archive; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $unixtime |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function formatDate($unixtime) |
41
|
|
|
{ |
42
|
|
|
if (strtotime('today') < $unixtime) |
43
|
|
|
return 'Today, '.date('G:m', $unixtime); |
44
|
|
|
else if (strtotime('yesterday') < $unixtime) |
45
|
|
|
return 'Yesterday, '.date('G:m', $unixtime); |
46
|
|
|
else { |
47
|
|
|
$datetime = new \DateTime(); |
48
|
|
|
$datetime->setTimestamp($unixtime); |
49
|
|
|
if ($datetime->format('Y') == date('Y')) |
50
|
|
|
return $datetime->format('d M, H:m'); |
51
|
|
|
else |
52
|
|
|
return $datetime->format('d M Y, H:m'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $bytes |
58
|
|
|
* @param int $precision |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function formatSize($bytes, $precision = 2) |
62
|
|
|
{ |
63
|
|
|
$units = ['b', 'k', 'm', 'g', 't']; |
64
|
|
|
|
65
|
|
|
$bytes = max($bytes, 0); |
66
|
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
67
|
|
|
$pow = min($pow, count($units) - 1); |
68
|
|
|
$bytes /= pow(1024, $pow); |
69
|
|
|
$i = round($bytes, $precision); |
70
|
|
|
if ($precision == 1 && $i >= 10) { |
71
|
|
|
$i = round($i / 1024, 1); |
72
|
|
|
$pow++; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return [$i, $units[$pow]]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $stream |
80
|
|
|
* @return false|string |
81
|
|
|
*/ |
82
|
|
|
protected function getMimeTypeByStream($stream) |
83
|
|
|
{ |
84
|
|
|
return @mime_content_type($stream); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getDriverBaseName($driverClass) |
88
|
|
|
{ |
89
|
|
|
return substr($driverClass, strrpos($driverClass, '\\') + 1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function resolveDriverName($driver) |
93
|
|
|
{ |
94
|
|
|
if (strpos($driver, '\\') === false) { |
95
|
|
|
if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver)) { |
96
|
|
|
$driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\' . $driver; |
97
|
|
|
} else if (class_exists('\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver)) { |
98
|
|
|
$driver = '\\wapmorgan\\UnifiedArchive\\Drivers\\OneFile\\' . $driver; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
if ($driver[0] !== '\\') { |
102
|
|
|
$driver = '\\'.$driver; |
103
|
|
|
} |
104
|
|
|
return $driver; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|