Passed
Push — master ( fb5355...cbc887 )
by Amin
07:37
created

Export::clouds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
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 FFMpeg\Exception\ExceptionInterface;
15
use Streaming\Clouds\Cloud;
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\Exception\RuntimeException;
18
use Streaming\Filters\Filter;
19
use Streaming\Traits\Formats;
20
21
22
abstract class Export
23
{
24
    use Formats;
25
26
    /** @var object */
27
    protected $media;
28
29
    /** @var array */
30
    protected $path_info;
31
32
    /** @var string */
33
    protected $strict = "-2";
34
35
    /** @var string */
36
    protected $tmp_dir;
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
     * @return object|Media
50
     */
51
    public function getMedia(): Media
52
    {
53
        return $this->media;
54
    }
55
56
    /**
57
     * @param string $strict
58
     * @return Export
59
     */
60
    public function setStrict(string $strict): Export
61
    {
62
        $this->strict = $strict;
63
        return $this;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getStrict(): string
70
    {
71
        return $this->strict;
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isTmpDir(): bool
78
    {
79
        return (bool)$this->tmp_dir;
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getPathInfo(): array
86
    {
87
        return $this->path_info;
88
    }
89
90
    /**
91
     * @param string|null $path
92
     */
93
    private function moveTmp(?string $path): void
94
    {
95
        if ($this->isTmpDir() && !is_null($path)) {
96
            File::moveDir($this->tmp_dir, dirname($path));
97
            $this->path_info = pathinfo($path);
98
            $this->tmp_dir = '';
99
        }
100
    }
101
102
    /**
103
     * @param array $clouds
104
     * @param string $path
105
     */
106
    private function clouds(array $clouds, ?string $path): void
107
    {
108
        if (!empty($clouds)) {
109
            Cloud::uploadDirectory($clouds, $this->tmp_dir);
110
            $this->moveTmp($path);
111
        }
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    protected function getFilePath(): string
118
    {
119
        return str_replace("\\", "/", $this->path_info["dirname"] . "/" . $this->path_info["filename"]);
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    abstract protected function getPath(): string;
126
127
    /**
128
     * @return Filter
129
     */
130
    abstract protected function getFilter(): Filter;
131
132
    /**
133
     * Run FFmpeg to package media content
134
     */
135
    private function run(): void
136
    {
137
        try {
138
            $this->media
139
                ->addFilter($this->getFilter())
140
                ->save($this->getFormat(), $this->getPath());
141
        } catch (ExceptionInterface $e) {
142
            throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
143
        }
144
    }
145
146
    /**
147
     * @param string|null $path
148
     */
149
    private function tmpDirectory(?string $path): void
150
    {
151
        $basename = $path ? basename($path) : Utilities::randomString();
152
153
        $this->tmp_dir = File::tmpDir();
154
        $this->path_info = pathinfo($this->tmp_dir . $basename);
155
    }
156
157
    /**
158
     * @param $path
159
     * @param $clouds
160
     */
161
    private function makePaths(?string $path, array $clouds): void
162
    {
163
        if ($clouds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $clouds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
164
            $this->tmpDirectory($path);
165
        } elseif (!is_null($path)) {
166
            if (strlen($path) > PHP_MAXPATHLEN) {
167
                throw new InvalidArgumentException("The path is too long");
168
            }
169
170
            File::makeDir(dirname($path));
171
            $this->path_info = pathinfo($path);
172
        } elseif ($this->media->isTmp()) {
173
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
174
        }
175
    }
176
177
    /**
178
     * @param string $path
179
     * @param array $clouds
180
     * @param bool $metadata
181
     * @return mixed
182
     */
183
    public function save(string $path = null, array $clouds = [], bool $metadata = true)
184
    {
185
        $this->makePaths($path, $clouds);
186
        $this->run();
187
        $this->clouds($clouds, $path);
188
189
        return $metadata ? (new Metadata($this))->extract() : $this;
190
    }
191
192
    /**
193
     * clear tmp files
194
     */
195
    public function __destruct()
196
    {
197
        sleep(1);
198
199
        if ($this->media->isTmp()) {
200
            @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

200
            /** @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...
201
        }
202
203
        if ($this->tmp_dir) {
204
            File::deleteDirectory($this->tmp_dir);
205
        }
206
207
        if ($this instanceof HLS && $this->tmp_key_info_file) {
208
            @unlink($this->getHlsKeyInfoFile());
209
        }
210
    }
211
}