Completed
Push — master ( 0eb1a8...12216b )
by Amin
03:20
created

Export::saveToS3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
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
use Streaming\Exception\Exception;
15
use Streaming\Exception\InvalidArgumentException;
16
use Streaming\Filters\Filter;
17
use Streaming\Traits\Formats;
18
19
abstract class Export
20
{
21
    use Formats;
22
23
    /** @var object */
24
    protected $media;
25
26
    /** @var Filter */
27
    protected $filter;
28
29
    /** @var array */
30
    protected $path_info;
31
32
    /** @var string */
33
    protected $strict = "-2";
34
35
    /** @var string */
36
    protected $mediaInfoBinary = 'mediainfo';
37
38
    /**
39
     * Export constructor.
40
     * @param Media $media
41
     */
42
    public function __construct(Media $media)
43
    {
44
        $this->media = $media;
45
        $this->path_info = pathinfo($media->getPath());
46
    }
47
48
    /**
49
     * @param string $path
50
     * @param bool $analyse
51
     * @return mixed
52
     * @throws Exception
53
     */
54
    public function save(string $path = null, $analyse = true)
55
    {
56
        $path = $this->getPath($path);
57
58
        $this->setFilter();
59
60
        $this->media->addFilter(
61
            $this->getFilter()
62
        );
63
64
        $this->media->save(
65
            $this->getFormat(),
66
            $path
67
        );
68
69
        $response = ($analyse) ? (new StreamingAnalytics($this, $this->mediaInfoBinary))->analyse() : $this;
70
71
        if ($this->media->isTmp()) {
72
            $this->deleteOriginalFile();
73
        }
74
75
        return $response;
76
    }
77
78
    /**
79
     * @return Filter
80
     */
81
    abstract protected function getFilter(): Filter;
82
83
    /**
84
     * @return mixed
85
     */
86
    abstract protected function setFilter();
87
88
    /**
89
     * @param $path
90
     * @return string
91
     * @throws Exception
92
     */
93
    private function getPath($path): string
94
    {
95
        if (null !== $path) {
96
            $this->path_info = pathinfo($path);
97
        }
98
99
        if (null === $path && $this->media->isTmp()) {
100
            $this->deleteOriginalFile();
101
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to the tmp directory");
102
        }
103
104
        $dirname = str_replace("\\", "/", $this->path_info["dirname"]);
105
        $filename = substr($this->path_info["filename"], -50);
106
107
        FileManager::makeDir($dirname);
108
109
        if ($this instanceof DASH) {
110
            $path = $dirname . "/" . $filename . ".mpd";
111
        } elseif ($this instanceof HLS) {
112
            $representations = $this->getRepresentations();
113
            $path = $dirname . "/" . $filename . "_" . end($representations)->getHeight() . "p.m3u8";
114
            ExportHLSPlaylist::savePlayList($dirname . DIRECTORY_SEPARATOR . $filename . ".m3u8", $this->getRepresentations(), $filename);
115
        }
116
117
        return $path;
118
    }
119
120
    /**
121
     * @param string $url
122
     * @param string $name
123
     * @param string|null $path
124
     * @param string $method
125
     * @param array $headers
126
     * @param array $options
127
     * @param bool $analyse
128
     * @return mixed
129
     * @throws Exception
130
     */
131
    public function saveToCloud(
132
        string $url,
133
        string $name,
134
        string $path = null,
135
        string $method = 'GET',
136
        array $headers = [],
137
        array $options = [],
138
        bool $analyse = true
139
    )
140
    {
141
        if($this instanceof HLS && $this->getTsSubDirectory()){
142
            throw new InvalidArgumentException("It is not possible to create subdirectory in cloud saving");
143
        }
144
        list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse);
145
        sleep(1);
146
147
        $file_manager = new FileManager($url, $method, $options);
148
        $file_manager->uploadDirectory($tmp_dir, $name, $headers);
149
150
        $this->moveTmpFolder($path, $tmp_dir);
151
152
        return $results;
153
    }
154
155
    /**
156
     * @param array $config
157
     * @param string $dest
158
     * @param string|null $path
159
     * @param bool $analyse
160
     * @return mixed
161
     * @throws Exception
162
     */
163
    public function saveToS3(array $config, string $dest, string $path = null, bool $analyse =true)
164
    {
165
        list($results, $tmp_dir) = $this->saveToTemporaryFolder($path, $analyse);
166
        sleep(1);
167
168
        $aws = new AWS($config);
169
        $aws->uploadAndDownloadDirectory($tmp_dir, $dest);
170
171
        $this->moveTmpFolder($path, $tmp_dir);
172
173
        return $results;
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function getPathInfo(): array
180
    {
181
        return $this->path_info;
182
    }
183
184
    /**
185
     * @return object|Media
186
     */
187
    public function getMedia()
188
    {
189
        return $this->media;
190
    }
191
192
    private function deleteOriginalFile()
193
    {
194
        sleep(1);
195
        @unlink($this->media->getPath());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

195
        /** @scrutinizer ignore-unhandled */ @unlink($this->media->getPath());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
196
    }
197
198
    /**
199
     * @param $path
200
     * @param $analyse
201
     * @return array
202
     * @throws Exception
203
     */
204
    private function saveToTemporaryFolder($path, $analyse)
205
    {
206
        $basename = Helper::randomString();
207
208
        if (null !== $path) {
209
            $basename = pathinfo($path)["basename"];
210
        }
211
212
        $tmp_dir = FileManager::tmpDir();
213
        $tmp_file = $tmp_dir . $basename;
214
215
        return [$this->save($tmp_file, $analyse), $tmp_dir];
216
    }
217
218
    /**
219
     * @param string|null $path
220
     * @param $tmp_dir
221
     * @throws Exception
222
     */
223
    private function moveTmpFolder(?string $path, $tmp_dir)
224
    {
225
        if (null !== $path) {
226
            $destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
227
            FileManager::makeDir($destination);
228
            FileManager::moveDir($tmp_dir, $destination);
229
        } else {
230
            FileManager::deleteDirectory($tmp_dir);
231
        }
232
    }
233
234
    /**
235
     * @param string $mediaInfoBinary
236
     * @return Export
237
     */
238
    public function setMediaInfoBinary(string $mediaInfoBinary)
239
    {
240
        $this->mediaInfoBinary = $mediaInfoBinary;
241
        return $this;
242
    }
243
244
    /**
245
     * @param string $strict
246
     * @return Export
247
     */
248
    public function setStrict(string $strict): Export
249
    {
250
        $this->strict = $strict;
251
        return $this;
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    public function getStrict(): string
258
    {
259
        return $this->strict;
260
    }
261
}