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
|
|
|
|
17
|
|
|
use GuzzleHttp\Client; |
18
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
19
|
|
|
use Streaming\Exception\Exception; |
20
|
|
|
|
21
|
|
|
class FileManager |
22
|
|
|
{ |
23
|
|
|
private $client; |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $url; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $method; |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $options; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* FileManager constructor. |
39
|
|
|
* @param string $url |
40
|
|
|
* @param string $method |
41
|
|
|
* @param array $options |
42
|
|
|
*/ |
43
|
|
|
public function __construct(string $url, string $method = "GET", $options = []) |
44
|
|
|
{ |
45
|
|
|
$this->client = new Client(); |
46
|
|
|
$this->url = $url; |
47
|
|
|
$this->method = $method; |
48
|
|
|
$this->options = $options; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $save_to |
54
|
|
|
* @throws Exception |
55
|
|
|
*/ |
56
|
|
|
public function downloadFile(string $save_to): void |
57
|
|
|
{ |
58
|
|
|
$this->sendRequest(array_merge($this->options, ['sink' => $save_to])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $dir |
63
|
|
|
* @param string $name |
64
|
|
|
* @param array $headers |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function uploadDirectory(string $dir, string $name, array $headers = []): void |
68
|
|
|
{ |
69
|
|
|
$multipart = []; |
70
|
|
|
|
71
|
|
|
foreach (scandir($dir) as $key => $filename) { |
72
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $filename; |
73
|
|
|
|
74
|
|
|
if (is_file($path)) { |
75
|
|
|
$multipart[$key]['name'] = $name; |
76
|
|
|
$multipart[$key]['contents'] = fopen($path, 'r'); |
77
|
|
|
if (!empty($headers)) { |
78
|
|
|
$multipart[$key]['headers'] = $headers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$multipart[$key]['filename'] = $filename; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->sendRequest(array_merge($this->options, ['multipart' => array_values($multipart)])); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $options |
90
|
|
|
* @throws Exception |
91
|
|
|
*/ |
92
|
|
|
private function sendRequest(array $options): void |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
$this->client->request($this->method, $this->url, $options); |
96
|
|
|
} catch (GuzzleException $e) { |
97
|
|
|
|
98
|
|
|
$error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s", |
99
|
|
|
$this->url, |
100
|
|
|
$e->getCode(), |
101
|
|
|
$e->getMessage(), |
102
|
|
|
$e->getResponse()->getBody()->getContents() |
|
|
|
|
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
throw new Exception($error); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |