Completed
Push — master ( e18231...8c2386 )
by Amin
02:57
created

Export   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 64
c 1
b 1
f 1
dl 0
loc 217
rs 10
wmc 20

11 Methods

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

188
        /** @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...
189
    }
190
191
    /**
192
     * @param $path
193
     * @param $analyse
194
     * @return array
195
     * @throws Exception
196
     */
197
    private function saveToTemporaryFolder($path, $analyse)
198
    {
199
        $basename = Helper::randomString();
200
201
        if (null !== $path) {
202
            $basename = pathinfo($path)["basename"];
203
        }
204
205
        $tmp_dir = Helper::tmpDir();
206
        $tmp_file = $tmp_dir . $basename;
207
208
        return [$this->save($tmp_file, $analyse), $tmp_dir];
209
    }
210
211
    /**
212
     * @param string|null $path
213
     * @param $tmp_dir
214
     * @throws Exception
215
     */
216
    private function moveTmpFolder(?string $path, $tmp_dir)
217
    {
218
        if (null !== $path) {
219
            $destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
220
            Helper::makeDir($destination);
221
            Helper::moveDir($tmp_dir, $destination);
222
        } else {
223
            Helper::deleteDirectory($tmp_dir);
224
        }
225
    }
226
227
    /**
228
     * @param string $mediaInfoBinary
229
     * @return Export
230
     */
231
    public function setMediaInfoBinary(string $mediaInfoBinary)
232
    {
233
        $this->mediaInfoBinary = $mediaInfoBinary;
234
        return $this;
235
    }
236
}