1 | <?php |
||
2 | /** |
||
3 | * Transcoder plugin for Craft CMS |
||
4 | * |
||
5 | * Transcode videos to various formats, and provide thumbnails of the video |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\transcoder\variables; |
||
12 | |||
13 | use craft\helpers\UrlHelper; |
||
14 | use nystudio107\pluginvite\variables\ViteVariableInterface; |
||
15 | use nystudio107\pluginvite\variables\ViteVariableTrait; |
||
16 | use nystudio107\transcoder\Transcoder; |
||
17 | use yii\base\InvalidConfigException; |
||
18 | |||
19 | /** |
||
0 ignored issues
–
show
|
|||
20 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
21 | * @package Transcode |
||
0 ignored issues
–
show
|
|||
22 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
23 | */ |
||
0 ignored issues
–
show
|
|||
24 | class TranscoderVariable implements ViteVariableInterface |
||
25 | { |
||
26 | use ViteVariableTrait; |
||
27 | |||
28 | // Public Methods |
||
29 | // ========================================================================= |
||
30 | |||
31 | /** |
||
32 | * Returns a URL to the transcoded video or "" if it doesn't exist (at which |
||
33 | * time it will create it). |
||
34 | * |
||
35 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
36 | * @param $videoOptions |
||
0 ignored issues
–
show
|
|||
37 | * @param bool $generate |
||
0 ignored issues
–
show
|
|||
38 | * @return string |
||
0 ignored issues
–
show
|
|||
39 | * @throws InvalidConfigException |
||
0 ignored issues
–
show
|
|||
40 | * @bool $generate |
||
0 ignored issues
–
show
|
|||
41 | * |
||
42 | */ |
||
0 ignored issues
–
show
|
|||
43 | public function getVideoUrl($filePath, $videoOptions, bool $generate = true): string |
||
44 | { |
||
45 | return Transcoder::$plugin->transcode->getVideoUrl($filePath, $videoOptions, $generate); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns a URL to a video thumbnail |
||
50 | * |
||
51 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
52 | * @param $thumbnailOptions |
||
0 ignored issues
–
show
|
|||
53 | * |
||
54 | * @return string|false|null URL or path of the video thumbnail |
||
55 | * @throws InvalidConfigException |
||
56 | */ |
||
57 | public function getVideoThumbnailUrl($filePath, $thumbnailOptions): string|false|null |
||
58 | { |
||
59 | return Transcoder::$plugin->transcode->getVideoThumbnailUrl($filePath, $thumbnailOptions); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns a URL to the transcoded audio file or "" if it doesn't exist |
||
64 | * (at which time it will create it). |
||
65 | * |
||
66 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
67 | * @param $audioOptions |
||
0 ignored issues
–
show
|
|||
68 | * |
||
69 | * @return string |
||
70 | * @throws InvalidConfigException |
||
71 | */ |
||
72 | public function getAudioUrl($filePath, $audioOptions): string |
||
73 | { |
||
74 | return Transcoder::$plugin->transcode->getAudioUrl($filePath, $audioOptions); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Extract information from a video/audio file |
||
79 | * |
||
80 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
81 | * @param bool $summary |
||
0 ignored issues
–
show
|
|||
82 | * |
||
83 | * @return ?array |
||
84 | * @throws InvalidConfigException |
||
85 | */ |
||
86 | public function getFileInfo($filePath, bool $summary = false): ?array |
||
87 | { |
||
88 | return Transcoder::$plugin->transcode->getFileInfo($filePath, $summary); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get a video progress URL |
||
93 | * |
||
94 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
95 | * @param $videoOptions |
||
0 ignored issues
–
show
|
|||
96 | * |
||
97 | * @return string |
||
98 | * @throws InvalidConfigException |
||
99 | */ |
||
100 | public function getVideoProgressUrl($filePath, $videoOptions): string |
||
101 | { |
||
102 | $result = ''; |
||
103 | $filename = Transcoder::$plugin->transcode->getVideoFilename($filePath, $videoOptions); |
||
104 | if (!empty($filename)) { |
||
105 | $urlParams = [ |
||
106 | 'filename' => $filename, |
||
107 | ]; |
||
108 | $result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
||
109 | } |
||
110 | |||
111 | return $result; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get an audio progress URL |
||
116 | * |
||
117 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
118 | * @param $audioOptions |
||
0 ignored issues
–
show
|
|||
119 | * |
||
120 | * @return string |
||
121 | * @throws InvalidConfigException |
||
122 | */ |
||
123 | public function getAudioProgressUrl($filePath, $audioOptions): string |
||
124 | { |
||
125 | $result = ''; |
||
126 | $filename = Transcoder::$plugin->transcode->getAudioFilename($filePath, $audioOptions); |
||
127 | if (!empty($filename)) { |
||
128 | $urlParams = [ |
||
129 | 'filename' => $filename, |
||
130 | ]; |
||
131 | $result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
||
132 | } |
||
133 | |||
134 | return $result; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get a GIF progress URL |
||
139 | * |
||
140 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
141 | * @param $gifOptions |
||
0 ignored issues
–
show
|
|||
142 | * |
||
143 | * @return string |
||
144 | * @throws InvalidConfigException |
||
145 | */ |
||
146 | public function getGifProgressUrl($filePath, $gifOptions): string |
||
147 | { |
||
148 | $result = ''; |
||
149 | $filename = Transcoder::$plugin->transcode->getGifFilename($filePath, $gifOptions); |
||
150 | if (!empty($filename)) { |
||
151 | $urlParams = [ |
||
152 | 'filename' => $filename, |
||
153 | ]; |
||
154 | $result = UrlHelper::actionUrl('transcoder/default/progress', $urlParams); |
||
155 | } |
||
156 | |||
157 | return $result; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get a download URL |
||
162 | * |
||
163 | * @param $url |
||
0 ignored issues
–
show
|
|||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getDownloadUrl($url): string |
||
168 | { |
||
169 | $result = ''; |
||
170 | $filePath = parse_url($url, PHP_URL_PATH); |
||
171 | $filePath = $_SERVER['DOCUMENT_ROOT'] . $filePath; |
||
172 | if (file_exists($filePath)) { |
||
173 | $urlParams = [ |
||
174 | 'url' => $url, |
||
175 | ]; |
||
176 | $result = UrlHelper::actionUrl('transcoder/default/download-file', $urlParams); |
||
177 | } |
||
178 | |||
179 | return $result; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Returns a URL to a GIF file |
||
184 | * |
||
185 | * @param $filePath |
||
0 ignored issues
–
show
|
|||
186 | * @param $gifOptions |
||
0 ignored issues
–
show
|
|||
187 | * |
||
188 | * @return string|false|null |
||
189 | * @throws InvalidConfigException |
||
190 | */ |
||
191 | public function getGifUrl($filePath, $gifOptions): string|false|null |
||
192 | { |
||
193 | return Transcoder::$plugin->transcode->getGifUrl($filePath, $gifOptions); |
||
194 | } |
||
195 | } |
||
196 |