Completed
Push — master ( 8c2386...3648da )
by Amin
03:00
created

FileManager::uploadDirectory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 4
nop 3
dl 0
loc 19
rs 9.9332
c 1
b 0
f 0
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
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
     * @param string $url
42
     * @param string $method
43
     * @param array $options
44
     */
45
    public function __construct(string $url,  string $method = "GET", $options = [])
46
    {
47
        $this->client = new Client();
48
        $this->url = $url;
49
        $this->method = $method;
50
        $this->options = $options;
51
    }
52
53
54
    /**
55
     * @param string $save_to
56
     * @throws Exception
57
     */
58
    public function downloadFile(string $save_to): void
59
    {
60
        $this->sendRequest(array_merge($this->options, ['sink' => $save_to]));
61
    }
62
63
    /**
64
     * @param string $dir
65
     * @param string $name
66
     * @param array $headers
67
     * @throws Exception
68
     */
69
    public function uploadDirectory(string $dir, string $name, array $headers = []): void
70
    {
71
        $multipart = [];
72
73
        foreach (scandir($dir) as $key => $filename) {
74
            $path = $dir . DIRECTORY_SEPARATOR . $filename;
75
76
            if (is_file($path)) {
77
                $multipart[$key]['name'] = $name;
78
                $multipart[$key]['contents'] = fopen($path, 'r');
79
                if (!empty($headers)) {
80
                    $multipart[$key]['headers'] = $headers;
81
                }
82
83
                $multipart[$key]['filename'] = $filename;
84
            }
85
        }
86
87
        $this->sendRequest(array_merge($this->options, ['multipart' => array_values($multipart)]));
88
    }
89
90
    /**
91
     * @param array $options
92
     * @throws Exception
93
     */
94
    private function sendRequest(array $options): void
95
    {
96
        try {
97
            $this->client->request($this->method, $this->url, $options);
98
        } catch (GuzzleException $e) {
99
100
            $error = sprintf('The url("%s") is not downloadable:\n' . "\n\nExit Code: %s(%s)\n\nbody:\n: %s",
101
                $this->url,
102
                $e->getCode(),
103
                $e->getMessage(),
104
                $e->getResponse()->getBody()->getContents()
0 ignored issues
show
Bug introduced by
The method getResponse() does not exist on GuzzleHttp\Exception\GuzzleException. It seems like you code against a sub-type of GuzzleHttp\Exception\GuzzleException such as GuzzleHttp\Exception\RequestException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
                $e->/** @scrutinizer ignore-call */ 
105
                    getResponse()->getBody()->getContents()
Loading history...
105
            );
106
107
            throw new Exception($error);
108
        }
109
    }
110
111
    /**
112
     * @param $dirname
113
     * @param int $mode
114
     * @throws Exception
115
     */
116
    public static function makeDir($dirname, $mode = 0777): void
117
    {
118
        $filesystem = new Filesystem();
119
120
        try {
121
            $filesystem->mkdir($dirname, $mode);
122
        } catch (IOExceptionInterface $exception) {
123
            throw new Exception("An error occurred while creating your directory at " . $exception->getPath());
124
        }
125
    }
126
127
    /**
128
     * @param $dir
129
     * @return int|null
130
     */
131
    public static function directorySize($dir)
132
    {
133
        if (is_dir($dir)) {
134
            $size = 0;
135
            foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
136
                $size += is_file($each) ? filesize($each) : static::directorySize($each);
137
            }
138
            return $size;
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @param string $ext
146
     * @return string
147
     * @throws Exception
148
     */
149
    public static function tmpFile(string $ext = ""): string
150
    {
151
        if ("" !== $ext) {
152
            $ext = "." . $ext;
153
        }
154
155
        $tmp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "php_ffmpeg_video_streaming";
156
        static::makeDir($tmp_path);
157
158
        return $tmp_path . DIRECTORY_SEPARATOR . Helper::randomString() . $ext;
159
    }
160
161
    /**
162
     * @return string
163
     * @throws Exception
164
     */
165
    public static function tmpDir(): string
166
    {
167
        return static::tmpFile() . DIRECTORY_SEPARATOR;
168
    }
169
170
171
    public static function moveDir(string $source, string $destination)
172
    {
173
        foreach (scandir($source) as $file) {
174
            if (in_array($file, [".", ".."])) continue;
175
            if (copy($source . $file, $destination . $file)) {
176
                unlink($source . $file);
177
            }
178
        }
179
    }
180
181
    /**
182
     * @param $dir
183
     * @return bool
184
     */
185
    public static function deleteDirectory($dir)
186
    {
187
        if (!file_exists($dir)) {
188
            return true;
189
        }
190
191
        if (!is_dir($dir)) {
192
            return @unlink($dir);
193
        }
194
195
        foreach (scandir($dir) as $item) {
196
            if (in_array($item, [".", ".."])) continue;
197
            if (!static::deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
198
                return false;
199
            }
200
        }
201
202
        return @rmdir($dir);
203
    }
204
205
}