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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\transcoder; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Plugin; |
15
|
|
|
use craft\console\Application as ConsoleApplication; |
16
|
|
|
use craft\elements\Asset; |
17
|
|
|
use craft\events\AssetThumbEvent; |
18
|
|
|
use craft\events\PluginEvent; |
19
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
20
|
|
|
use craft\events\RegisterUrlRulesEvent; |
21
|
|
|
use craft\helpers\Assets as AssetsHelper; |
22
|
|
|
use craft\helpers\FileHelper; |
23
|
|
|
use craft\helpers\UrlHelper; |
24
|
|
|
use craft\services\Assets; |
25
|
|
|
use craft\services\Plugins; |
26
|
|
|
use craft\utilities\ClearCaches; |
27
|
|
|
use craft\web\twig\variables\CraftVariable; |
28
|
|
|
use craft\web\UrlManager; |
29
|
|
|
use nystudio107\transcoder\models\Settings; |
30
|
|
|
use nystudio107\transcoder\services\ServicesTrait; |
31
|
|
|
use nystudio107\transcoder\variables\TranscoderVariable; |
32
|
|
|
use yii\base\ErrorException; |
33
|
|
|
use yii\base\Event; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class Transcode |
37
|
|
|
* |
38
|
|
|
* @author nystudio107 |
|
|
|
|
39
|
|
|
* @package Transcode |
|
|
|
|
40
|
|
|
* @since 1.0.0 |
|
|
|
|
41
|
|
|
*/ |
|
|
|
|
42
|
|
|
class Transcoder extends Plugin |
43
|
|
|
{ |
44
|
|
|
// Traits |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
use ServicesTrait; |
48
|
|
|
|
49
|
|
|
// Static Properties |
50
|
|
|
// ========================================================================= |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @var Transcoder |
54
|
|
|
*/ |
55
|
|
|
public static $plugin; |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @var Settings |
59
|
|
|
*/ |
60
|
|
|
public static $settings; |
61
|
|
|
|
62
|
|
|
// Public Properties |
63
|
|
|
// ========================================================================= |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
public $schemaVersion = '1.0.0'; |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
public $hasCpSection = false; |
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
public $hasCpSettings = false; |
79
|
|
|
|
80
|
|
|
// Public Methods |
81
|
|
|
// ========================================================================= |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
|
|
|
|
86
|
|
|
public function init() |
87
|
|
|
{ |
88
|
|
|
parent::init(); |
89
|
|
|
self::$plugin = $this; |
90
|
|
|
// Initialize properties |
91
|
|
|
self::$settings = self::$plugin->getSettings(); |
92
|
|
|
// Handle console commands |
93
|
|
|
if (Craft::$app instanceof ConsoleApplication) { |
94
|
|
|
$this->controllerNamespace = 'nystudio107\transcoder\console\controllers'; |
95
|
|
|
} |
96
|
|
|
// Add in our Craft components |
97
|
|
|
$this->addComponents(); |
98
|
|
|
// Install our global event handlers |
99
|
|
|
$this->installEventHandlers(); |
100
|
|
|
// We've loaded! |
101
|
|
|
Craft::info( |
102
|
|
|
Craft::t( |
103
|
|
|
'transcoder', |
104
|
|
|
'{name} plugin loaded', |
105
|
|
|
['name' => $this->name] |
106
|
|
|
), |
107
|
|
|
__METHOD__ |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Clear all the caches! |
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
public function clearAllCaches() |
115
|
|
|
{ |
116
|
|
|
$transcoderPaths = self::$plugin->getSettings()->transcoderPaths; |
117
|
|
|
|
118
|
|
|
foreach ($transcoderPaths as $key => $value) { |
119
|
|
|
$dir = Craft::parseEnv($value); |
|
|
|
|
120
|
|
|
try { |
121
|
|
|
FileHelper::clearDirectory($dir); |
|
|
|
|
122
|
|
|
Craft::info( |
123
|
|
|
Craft::t( |
124
|
|
|
'transcoder', |
125
|
|
|
'{name} cache directory cleared', |
126
|
|
|
['name' => $key] |
127
|
|
|
), |
128
|
|
|
__METHOD__ |
129
|
|
|
); |
130
|
|
|
} catch (ErrorException $e) { |
131
|
|
|
// the directory doesn't exist |
132
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Protected Methods |
138
|
|
|
// ========================================================================= |
139
|
|
|
|
140
|
|
|
/** |
|
|
|
|
141
|
|
|
* @inheritdoc |
142
|
|
|
*/ |
|
|
|
|
143
|
|
|
protected function createSettingsModel() |
144
|
|
|
{ |
145
|
|
|
return new Settings(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add in our Craft components |
150
|
|
|
*/ |
|
|
|
|
151
|
|
|
protected function addComponents() |
152
|
|
|
{ |
153
|
|
|
// Register our variables |
154
|
|
|
Event::on( |
155
|
|
|
CraftVariable::class, |
156
|
|
|
CraftVariable::EVENT_INIT, |
157
|
|
|
function (Event $event) { |
158
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
159
|
|
|
$variable = $event->sender; |
160
|
|
|
$variable->set('transcoder', [ |
|
|
|
|
161
|
|
|
'class' => TranscoderVariable::class, |
162
|
|
|
'viteService' => $this->vite, |
163
|
|
|
]); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Install our event handlers |
170
|
|
|
*/ |
|
|
|
|
171
|
|
|
protected function installEventHandlers() |
172
|
|
|
{ |
173
|
|
|
$settings = $this->getSettings(); |
174
|
|
|
// Handler: Assets::EVENT_GET_THUMB_PATH |
175
|
|
|
Event::on( |
176
|
|
|
Assets::class, |
177
|
|
|
Assets::EVENT_GET_THUMB_PATH, |
178
|
|
|
function (AssetThumbEvent $event) { |
179
|
|
|
Craft::debug( |
180
|
|
|
'Assets::EVENT_GET_THUMB_PATH', |
181
|
|
|
__METHOD__ |
182
|
|
|
); |
183
|
|
|
/** @var Asset $asset */ |
|
|
|
|
184
|
|
|
$asset = $event->asset; |
185
|
|
|
if (AssetsHelper::getFileKindByExtension($asset->filename) === Asset::KIND_VIDEO) { |
|
|
|
|
186
|
|
|
$path = Transcoder::$plugin->transcode->handleGetAssetThumbPath($event); |
187
|
|
|
if (!empty($path)) { |
188
|
|
|
$event->path = $path; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
); |
193
|
|
|
if ($settings->clearCaches) { |
194
|
|
|
// Add the Transcode path to the list of things the Clear Caches tool can delete. |
195
|
|
|
Event::on( |
196
|
|
|
ClearCaches::class, |
197
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
198
|
|
|
function (RegisterCacheOptionsEvent $event) { |
199
|
|
|
$event->options[] = [ |
200
|
|
|
'key' => 'transcoder', |
201
|
|
|
'label' => Craft::t('transcoder', 'Transcoder caches'), |
202
|
|
|
'action' => [$this, 'clearAllCaches'], |
203
|
|
|
]; |
204
|
|
|
} |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
208
|
|
|
Event::on( |
209
|
|
|
Plugins::class, |
210
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
211
|
|
|
function (PluginEvent $event) { |
212
|
|
|
if ($event->plugin === $this) { |
213
|
|
|
$request = Craft::$app->getRequest(); |
214
|
|
|
if ($request->isCpRequest) { |
215
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('transcoder/welcome'))->send(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
); |
220
|
|
|
$request = Craft::$app->getRequest(); |
221
|
|
|
// Install only for non-console site requests |
222
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
223
|
|
|
$this->installSiteEventListeners(); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Install site event listeners for site requests only |
229
|
|
|
*/ |
|
|
|
|
230
|
|
|
protected function installSiteEventListeners() |
231
|
|
|
{ |
232
|
|
|
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
233
|
|
|
Event::on( |
234
|
|
|
UrlManager::class, |
235
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
236
|
|
|
function (RegisterUrlRulesEvent $event) { |
237
|
|
|
Craft::debug( |
238
|
|
|
'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
239
|
|
|
__METHOD__ |
240
|
|
|
); |
241
|
|
|
// Register our Control Panel routes |
242
|
|
|
$event->rules = array_merge( |
243
|
|
|
$event->rules, |
244
|
|
|
$this->customFrontendRoutes() |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Return the custom frontend routes |
252
|
|
|
* |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
|
|
protected function customFrontendRoutes(): array |
256
|
|
|
{ |
257
|
|
|
return [ |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|