Passed
Push — master ( 32f6b3...6bd9ce )
by Amin
02:21
created

Export::getMedia()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 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 $tmp_dir;
34
35
    /**
36
     * Export constructor.
37
     * @param Media $media
38
     */
39
    public function __construct(Media $media)
40
    {
41
        $this->media = $media;
42
        $this->path_info = pathinfo($media->getPath());
43
    }
44
45
    /**
46
     * @return object|Media
47
     */
48
    public function getMedia(): Media
49
    {
50
        return $this->media;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isTmpDir(): bool
57
    {
58
        return (bool)$this->tmp_dir;
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getPathInfo(): array
65
    {
66
        return $this->path_info;
67
    }
68
69
    /**
70
     * @param string|null $path
71
     */
72
    private function moveTmp(?string $path): void
73
    {
74
        if ($this->isTmpDir() && !is_null($path)) {
75
            File::moveDir($this->tmp_dir, dirname($path));
76
            $this->path_info = pathinfo($path);
77
            $this->tmp_dir = '';
78
        }
79
    }
80
81
    /**
82
     * @param array $clouds
83
     * @param string $path
84
     */
85
    private function clouds(array $clouds, ?string $path): void
86
    {
87
        if (!empty($clouds)) {
88
            Cloud::uploadDirectory($clouds, $this->tmp_dir);
89
            $this->moveTmp($path);
90
        }
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    protected function getFilePath(): string
97
    {
98
        return str_replace("\\", "/", $this->path_info["dirname"] . "/" . $this->path_info["filename"]);
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    abstract protected function getPath(): string;
105
106
    /**
107
     * @return Filter
108
     */
109
    abstract protected function getFilter(): Filter;
110
111
    /**
112
     * Run FFmpeg to package media content
113
     */
114
    private function run(): void
115
    {
116
        try {
117
            $this->media
118
                ->addFilter($this->getFilter())
119
                ->save($this->getFormat(), $this->getPath());
120
        } catch (ExceptionInterface $e) {
121
            throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
122
        }
123
    }
124
125
    /**
126
     * @param string|null $path
127
     */
128
    private function tmpDirectory(?string $path): void
129
    {
130
        $basename = $path ? basename($path) : $this->path_info['basename'];
131
132
        $this->tmp_dir = File::tmpDir();
133
        $this->path_info = pathinfo($this->tmp_dir . $basename);
134
    }
135
136
    /**
137
     * @param $path
138
     * @param $clouds
139
     */
140
    private function makePaths(?string $path, array $clouds): void
141
    {
142
        if (!empty($clouds)) {
143
            $this->tmpDirectory($path);
144
        } elseif (!is_null($path)) {
145
            if (strlen($path) > PHP_MAXPATHLEN) {
146
                throw new InvalidArgumentException("The path is too long");
147
            }
148
149
            File::makeDir(dirname($path));
150
            $this->path_info = pathinfo($path);
151
        } elseif ($this->media->isTmp()) {
152
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
153
        }
154
    }
155
156
    /**
157
     * @param string $path
158
     * @param array $clouds
159
     * @param bool $metadata
160
     * @return mixed
161
     */
162
    public function save(string $path = null, array $clouds = [], bool $metadata = true)
163
    {
164
        $this->makePaths($path, $clouds);
165
        $this->run();
166
        $this->clouds($clouds, $path);
167
168
        return $metadata ? (new Metadata($this))->extract() : $this;
169
    }
170
171
    /**
172
     * clear tmp files
173
     */
174
    public function __destruct()
175
    {
176
        sleep(1);
177
178
        if ($this->media->isTmp()) {
179
            File::remove($this->media->getPath());
180
        }
181
182
        if ($this->tmp_dir) {
183
            File::remove($this->tmp_dir);
184
        }
185
186
        if ($this instanceof HLS && $this->tmp_key_info_file) {
187
            File::remove($this->getHlsKeyInfoFile());
188
        }
189
    }
190
}