Passed
Push — master ( 63c9ea...e1ec1e )
by Amin
03:49
created

Export::saveToS3()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 3
dl 0
loc 26
rs 9.7333
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 . "/" . $filename . ".m3u8", $this->getRepresentations(), $filename);
108
        }
109
110
        return $path;
111
    }
112
113
    /**
114
     * @param array $config
115
     * @param string $dest
116
     * @param string|null $path
117
     * @return mixed
118
     * @throws Exception
119
     */
120
    public function saveToS3(array $config, string $dest, string $path = null)
121
    {
122
        $basename = Helper::randomString();
123
124
        if (null !== $path){
125
            $basename = pathinfo($path)["basename"];
126
        }
127
128
        $tmp_dir = Helper::tmpDir();
129
        $tmp_file = $tmp_dir . $basename;
130
131
        $results = $this->save($tmp_file);
132
        sleep(1);
133
134
        $aws = new AWS($config);
135
        $aws->uploadAndDownloadDirectory($tmp_dir, $dest);
136
137
        if(null !== $path){
138
            $destination = pathinfo($path)["dirname"] . DIRECTORY_SEPARATOR;
139
            Helper::makeDir($destination);
140
            Helper::moveDir($tmp_dir, $destination);
141
        }else{
142
            Helper::deleteDirectory($tmp_dir);
143
        }
144
145
        return $results;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function getPathInfo(): array
152
    {
153
        return $this->path_info;
154
    }
155
156
    /**
157
     * @return object|Media
158
     */
159
    public function getMedia()
160
    {
161
        return $this->media;
162
    }
163
164
    private function deleteOriginalFile()
165
    {
166
        sleep(1);
167
        @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

167
        /** @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...
168
    }
169
}