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\Clouds; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use Streaming\Exception\InvalidArgumentException; |
17
|
|
|
use Streaming\FileManager; |
18
|
|
|
|
19
|
|
|
class CloudManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param array $clouds |
23
|
|
|
* @param string $tmp_dir |
24
|
|
|
*/ |
25
|
|
|
public static function saveToClouds(?array $clouds, ?string $tmp_dir): void |
26
|
|
|
{ |
27
|
|
|
if (!$clouds) { |
|
|
|
|
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!is_array(current($clouds))) { |
32
|
|
|
$clouds = [$clouds]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
sleep(1); |
36
|
|
|
|
37
|
|
|
foreach ($clouds as $cloud) { |
38
|
|
|
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) { |
39
|
|
|
$cloud_obj = $cloud['cloud']; |
40
|
|
|
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : []; |
41
|
|
|
|
42
|
|
|
$cloud_obj->uploadDirectory($tmp_dir, $options); |
|
|
|
|
43
|
|
|
} else { |
44
|
|
|
throw new InvalidArgumentException('You must pass an array of clouds to the save method. |
45
|
|
|
and the cloud must be instance of CloudInterface'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $cloud |
53
|
|
|
* @param string|null $save_to |
54
|
|
|
* @return array |
55
|
|
|
* @throws \Streaming\Exception\Exception |
56
|
|
|
*/ |
57
|
|
|
public static function downloadFromCloud(array $cloud, string $save_to = null): array |
58
|
|
|
{ |
59
|
|
|
list($is_tmp, $save_to) = static::isTmp($save_to); |
60
|
|
|
|
61
|
|
|
if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) { |
62
|
|
|
$cloud_obj = $cloud['cloud']; |
63
|
|
|
$options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : []; |
64
|
|
|
|
65
|
|
|
$cloud_obj->download($save_to, $options); |
66
|
|
|
} else { |
67
|
|
|
throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method. |
68
|
|
|
and the cloud must be instance of CloudInterface'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [$save_to, $is_tmp]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $path |
76
|
|
|
* @return array |
77
|
|
|
* @throws \Streaming\Exception\Exception |
78
|
|
|
*/ |
79
|
|
|
private static function isTmp($path) |
80
|
|
|
{ |
81
|
|
|
$is_tmp = false; |
82
|
|
|
|
83
|
|
|
if (null === $path) { |
84
|
|
|
$is_tmp = true; |
85
|
|
|
$path = FileManager::tmpFile(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [$is_tmp, $path]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.