Passed
Push — master ( d8d39b...c46f01 )
by Amin
03:19
created

Export::saveToTemporaryFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

182
        /** @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...
183
    }
184
185
    /**
186
     * @param $path
187
     * @return array
188
     * @throws Exception
189
     */
190
    private function saveToTemporaryFolder($path)
191
    {
192
        $basename = Helper::randomString();
193
194
        if (null !== $path) {
195
            $basename = pathinfo($path)["basename"];
196
        }
197
198
        $tmp_dir = Helper::tmpDir();
199
        $tmp_file = $tmp_dir . $basename;
200
201
        return [$this->save($tmp_file), $tmp_dir];
202
    }
203
204
    /**
205
     * @param string|null $path
206
     * @param $tmp_dir
207
     * @throws Exception
208
     */
209
    private function moveTmpFolder(?string $path, $tmp_dir)
210
    {
211
        if (null !== $path) {
212
            $destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
213
            Helper::makeDir($destination);
214
            Helper::moveDir($tmp_dir, $destination);
215
        } else {
216
            Helper::deleteDirectory($tmp_dir);
217
        }
218
    }
219
}