forked from robinscholz/kirby-mux
/
index.php
107 lines (91 loc) · 3.21 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
include_once __DIR__ . '/vendor/autoload.php';
use \Kirby\Cms\App as Kirby;
use \Kirby\Cms\File as File;
use \Kirby\Filesystem\File as FileSystem;
Dotenv\Dotenv::createImmutable(kirby()->root('index'))->load();
Kirby::plugin('robinscholz/kirby-mux', [
'translations' => [
'en' => [
'field.blocks.mux-video.thumbnail' => 'Generate thumbnail from frame',
'field.blocks.mux-video.thumbnail.help' => 'In seconds',
],
'nl' => [
'field.blocks.mux-video.thumbnail' => 'Maak thumbnail van frame',
'field.blocks.mux-video.thumbnail.help' => 'In seconden',
],
],
'blueprints' => [
'files/mux-video' => __DIR__ . '/blueprints/files/mux-video.yml',
'blocks/mux-video' => __DIR__ . '/blueprints/blocks/mux-video.yml'
],
'snippets' => [
'blocks/mux-video' => __DIR__ . '/snippets/blocks/mux-video.php'
],
'hooks' => [
'file.create:after' => function (File $file) {
if ($file->type() !== 'video') {
return;
}
// Authenticate
$assetsApi = KirbyMux\Auth::assetsApi();
// Upload the file to mux
$result = KirbyMux\Methods::upload($assetsApi, $file->url());
// Save mux data
try {
$file = $file->update([
'mux' => $result->getData()
]);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
},
'file.delete:before' => function (File $file) {
if ($file->type() !== 'video') {
return;
}
// Authentication setup
$assetsApi = KirbyMux\Auth::assetsApi();
// Get mux Id
$muxId = json_decode($file->mux())->id;
// Delete Asset
try {
$assetsApi->deleteAsset($muxId);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
},
'file.replace:before' => function (File $file, FileSystem $upload) {
if ($upload->type() !== 'video') {
return;
}
// Authentication setup
$assetsApi = KirbyMux\Auth::assetsApi();
// Get old mux Id
$muxId = json_decode($file->mux())->id;
// Delete old asset
try {
$assetsApi->deleteAsset($muxId);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
},
'file.replace:after' => function (File $newFile, File $oldFile) {
if ($newFile->type() !== 'video') {
return;
}
// Authentication setup
$assetsApi = KirbyMux\Auth::assetsApi();
// Upload new file to mux
$result = KirbyMux\Methods::upload($assetsApi, $newFile->url());
// Save playback Id
try {
$newFile = $newFile->update([
'mux' => $result->getData()
]);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
]
]);