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