1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
5
|
|
|
* |
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Streaming; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use GuzzleHttp\Client; |
16
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
17
|
|
|
use Streaming\Exception\Exception; |
18
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
19
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
20
|
|
|
|
21
|
|
|
class Helper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* round a number to nearest even number |
25
|
|
|
* |
26
|
|
|
* @param float $number |
27
|
|
|
* @return int |
28
|
|
|
*/ |
29
|
|
|
public static function roundToEven(float $number): int |
30
|
|
|
{ |
31
|
|
|
return (($number = intval($number)) % 2 == 0) ? $number : $number + 1; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $dirname |
36
|
|
|
* @param int $mode |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
|
|
public static function makeDir($dirname, $mode = 0777): void |
40
|
|
|
{ |
41
|
|
|
$filesystem = new Filesystem(); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$filesystem->mkdir($dirname, $mode); |
45
|
|
|
} catch (IOExceptionInterface $exception) { |
46
|
|
|
throw new Exception("An error occurred while creating your directory at " . $exception->getPath()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $url |
52
|
|
|
* @param string|null $save_to |
53
|
|
|
* @param string $method |
54
|
|
|
* @param array $request_options |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
public static function downloadFile(string $url, string $save_to = null, string $method = "GET", $request_options = []): void |
58
|
|
|
{ |
59
|
|
|
$request_options = array_merge($request_options, ['sink' => $save_to]); |
60
|
|
|
$client = new Client(); |
61
|
|
|
try { |
62
|
|
|
$client->request($method, $url, $request_options); |
63
|
|
|
} catch (GuzzleException $e) { |
64
|
|
|
|
65
|
|
|
$error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s", |
66
|
|
|
$url, |
67
|
|
|
$e->getCode(), |
68
|
|
|
$e->getMessage(), |
69
|
|
|
$e->getResponse()->getBody()->getContents() |
|
|
|
|
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
throw new Exception($error); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $length |
78
|
|
|
* @return bool|string |
79
|
|
|
*/ |
80
|
|
|
public static function randomString($length = 10) |
81
|
|
|
{ |
82
|
|
|
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
83
|
|
|
return substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length); |
|
|
|
|
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $dir |
89
|
|
|
* @return int|null |
90
|
|
|
*/ |
91
|
|
|
public static function directorySize($dir) |
92
|
|
|
{ |
93
|
|
|
if (is_dir($dir)) { |
94
|
|
|
$size = 0; |
95
|
|
|
foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) { |
96
|
|
|
$size += is_file($each) ? filesize($each) : static::directorySize($each); |
97
|
|
|
} |
98
|
|
|
return $size; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $ext |
106
|
|
|
* @return string |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
|
|
public static function tmpFile(string $ext = ""): string |
110
|
|
|
{ |
111
|
|
|
if ("" !== $ext) { |
112
|
|
|
$ext = "." . $ext; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming"; |
116
|
|
|
static::makeDir($tmp_path); |
117
|
|
|
|
118
|
|
|
return $tmp_path . DIRECTORY_SEPARATOR . static::randomString() . $ext; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
* @throws Exception |
124
|
|
|
*/ |
125
|
|
|
public static function tmpDir(): string |
126
|
|
|
{ |
127
|
|
|
return static::tmpFile() . DIRECTORY_SEPARATOR; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
public static function moveDir(string $source, string $destination) |
132
|
|
|
{ |
133
|
|
|
foreach (scandir($source) as $file) { |
134
|
|
|
if (in_array($file, [".", ".."])) continue; |
135
|
|
|
if (copy($source . $file, $destination . $file)) { |
136
|
|
|
unlink($source . $file); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $dir |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public static function deleteDirectory($dir) |
146
|
|
|
{ |
147
|
|
|
if (!file_exists($dir)) { |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!is_dir($dir)) { |
152
|
|
|
return @unlink($dir); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach (scandir($dir) as $item) { |
156
|
|
|
if (in_array($item, [".", ".."])) continue; |
157
|
|
|
if (!static::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return @rmdir($dir); |
163
|
|
|
} |
164
|
|
|
} |