|
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
|
|
|
|
|
13
|
|
|
namespace Streaming; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
use GuzzleHttp\Client; |
|
17
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
18
|
|
|
use Streaming\Exception\Exception; |
|
19
|
|
|
use Streaming\Exception\RuntimeException; |
|
20
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
|
21
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
22
|
|
|
|
|
23
|
|
|
class FileManager |
|
24
|
|
|
{ |
|
25
|
|
|
private $client; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $url; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $method; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $options; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* FileManager constructor. |
|
41
|
|
|
* It is all about files |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $url |
|
44
|
|
|
* @param string $method |
|
45
|
|
|
* @param array $options |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(string $url, string $method = "GET", $options = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->client = new Client(); |
|
50
|
|
|
$this->url = $url; |
|
51
|
|
|
$this->method = $method; |
|
52
|
|
|
$this->options = $options; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $save_to |
|
58
|
|
|
* @throws RuntimeException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function downloadFile(string $save_to): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->sendRequest(array_merge($this->options, ['sink' => $save_to])); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $dir |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param array $headers |
|
69
|
|
|
* @throws RuntimeException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function uploadDirectory(string $dir, string $name, array $headers = []): void |
|
72
|
|
|
{ |
|
73
|
|
|
$multipart = []; |
|
74
|
|
|
|
|
75
|
|
|
foreach (scandir($dir) as $key => $filename) { |
|
76
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $filename; |
|
77
|
|
|
|
|
78
|
|
|
if (is_file($path)) { |
|
79
|
|
|
$multipart[$key]['name'] = $name; |
|
80
|
|
|
$multipart[$key]['contents'] = fopen($path, 'r'); |
|
81
|
|
|
if (!empty($headers)) { |
|
82
|
|
|
$multipart[$key]['headers'] = $headers; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$multipart[$key]['filename'] = $filename; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->sendRequest(array_merge($this->options, ['multipart' => array_values($multipart)])); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $options |
|
94
|
|
|
* @throws RuntimeException |
|
95
|
|
|
*/ |
|
96
|
|
|
private function sendRequest(array $options): void |
|
97
|
|
|
{ |
|
98
|
|
|
try { |
|
99
|
|
|
$this->client->request($this->method, $this->url, $options); |
|
100
|
|
|
} catch (GuzzleException $e) { |
|
101
|
|
|
|
|
102
|
|
|
$error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s", |
|
103
|
|
|
$this->url, |
|
104
|
|
|
$e->getCode(), |
|
105
|
|
|
$e->getMessage(), |
|
106
|
|
|
(method_exists($e->getResponse(), 'getBody')) ? $e->getResponse()->getBody()->getContents() : "" |
|
|
|
|
|
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
throw new RuntimeException($error); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $dirname |
|
115
|
|
|
* @param int $mode |
|
116
|
|
|
* @throws Exception |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function makeDir($dirname, $mode = 0777): void |
|
119
|
|
|
{ |
|
120
|
|
|
$filesystem = new Filesystem(); |
|
121
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
$filesystem->mkdir($dirname, $mode); |
|
124
|
|
|
} catch (IOExceptionInterface $exception) { |
|
125
|
|
|
throw new Exception("An error occurred while creating your directory at " . $exception->getPath()); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param $dir |
|
131
|
|
|
* @return int|null |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function directorySize($dir) |
|
134
|
|
|
{ |
|
135
|
|
|
if (is_dir($dir)) { |
|
136
|
|
|
$size = 0; |
|
137
|
|
|
foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) { |
|
138
|
|
|
$size += is_file($each) ? filesize($each) : static::directorySize($each); |
|
139
|
|
|
} |
|
140
|
|
|
return $size; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
* @throws Exception |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function tmpFile(): string |
|
151
|
|
|
{ |
|
152
|
|
|
return tempnam(static::tmpDirPath(), 'stream'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
* @throws Exception |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function tmpDir(): string |
|
160
|
|
|
{ |
|
161
|
|
|
$tmp_dir = static::tmpDirPath() . DIRECTORY_SEPARATOR . Helper::randomString() . DIRECTORY_SEPARATOR; |
|
162
|
|
|
static::makeDir($tmp_dir); |
|
163
|
|
|
|
|
164
|
|
|
return $tmp_dir; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return string |
|
169
|
|
|
* @throws Exception |
|
170
|
|
|
*/ |
|
171
|
|
|
private static function tmpDirPath(): string |
|
172
|
|
|
{ |
|
173
|
|
|
$tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming"; |
|
174
|
|
|
static::makeDir($tmp_path); |
|
175
|
|
|
|
|
176
|
|
|
return $tmp_path; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $source |
|
181
|
|
|
* @param string $destination |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function moveDir(string $source, string $destination) |
|
184
|
|
|
{ |
|
185
|
|
|
foreach (scandir($source) as $file) { |
|
186
|
|
|
if (in_array($file, [".", ".."])) continue; |
|
187
|
|
|
if (copy($source . $file, $destination . $file)) { |
|
188
|
|
|
unlink($source . $file); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param $dir |
|
195
|
|
|
* @return bool |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function deleteDirectory($dir) |
|
198
|
|
|
{ |
|
199
|
|
|
if (!file_exists($dir)) { |
|
200
|
|
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (!is_dir($dir)) { |
|
204
|
|
|
return @unlink($dir); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
foreach (scandir($dir) as $item) { |
|
208
|
|
|
if (in_array($item, [".", ".."])) continue; |
|
209
|
|
|
if (!static::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) { |
|
210
|
|
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return @rmdir($dir); |
|
215
|
|
|
} |
|
216
|
|
|
} |