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
|
|
|
/** |
78
|
|
|
* @param int $length |
79
|
|
|
* @return bool|string |
80
|
|
|
*/ |
81
|
|
|
public static function randomString($length = 10) |
82
|
|
|
{ |
83
|
|
|
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
84
|
|
|
return substr(str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))), 1, $length); |
|
|
|
|
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $dir |
90
|
|
|
* @return int|null |
91
|
|
|
*/ |
92
|
|
|
public static function directorySize($dir) |
93
|
|
|
{ |
94
|
|
|
if (is_dir($dir)) { |
95
|
|
|
$size = 0; |
96
|
|
|
foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) { |
97
|
|
|
$size += is_file($each) ? filesize($each) : static::directorySize($each); |
98
|
|
|
} |
99
|
|
|
return $size; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
} |