Newer
Older
<?php
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace Modules\Admin\Controllers;

Yassine Doghri
committed
use App\Entities\Clip\VideoClip;
use App\Entities\Episode;
use App\Entities\Podcast;

Yassine Doghri
committed
use App\Models\ClipModel;
use App\Models\EpisodeModel;

Yassine Doghri
committed
use App\Models\MediaModel;
use App\Models\PodcastModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;
class VideoClipsController extends BaseController
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
{
protected Podcast $podcast;
protected Episode $episode;
public function _remap(string $method, string ...$params): mixed
{
if (
($podcast = (new PodcastModel())->getPodcastById((int) $params[0])) === null
) {
throw PageNotFoundException::forPageNotFound();
}
$this->podcast = $podcast;
if (count($params) > 1) {
if (
! ($episode = (new EpisodeModel())
->where([
'id' => $params[1],
'podcast_id' => $params[0],
])
->first())
) {
throw PageNotFoundException::forPageNotFound();
}
$this->episode = $episode;
unset($params[1]);
unset($params[0]);
}
return $this->{$method}(...$params);
}
public function list(): string
{

Yassine Doghri
committed
$videoClipsBuilder = (new ClipModel('video'))

Yassine Doghri
committed
->where([
'podcast_id' => $this->podcast->id,
'episode_id' => $this->episode->id,
'type' => 'video',
])
->orderBy('created_at', 'desc');

Yassine Doghri
committed
$clips = $videoClipsBuilder->paginate(10);
$videoClips = [];
foreach ($clips as $clip) {
$videoClips[] = new VideoClip($clip->toArray());
}
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,

Yassine Doghri
committed
'videoClips' => $videoClips,
'pager' => $videoClipsBuilder->pager,
];
replace_breadcrumb_params([
0 => $this->podcast->title,
1 => $this->episode->title,
]);
return view('episode/video_clips_list', $data);
}
public function view($videoClipId): string
{
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,
'videoClip' => $videoClip,
];
replace_breadcrumb_params([
0 => $this->podcast->title,
1 => $this->episode->title,

Yassine Doghri
committed
2 => $videoClip->title,
]);
return view('episode/video_clip', $data);
}

Yassine Doghri
committed
public function create(): string
{
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,
];
replace_breadcrumb_params([
0 => $this->podcast->title,
1 => $this->episode->title,

Yassine Doghri
committed

Yassine Doghri
committed
// First, check that requirements to create a video clip are met
$ffmpeg = trim(shell_exec('type -P ffmpeg'));
$checks = [
'ffmpeg' => ! empty($ffmpeg),
'gd' => extension_loaded('gd'),
'freetype' => extension_loaded('gd') && gd_info()['FreeType Support'],
'transcript' => $this->episode->transcript !== null,
];
if (in_array(false, $checks, true)) {
$data['checks'] = $checks;
return view('episode/video_clips_requirements', $data);
}
helper('form');
return view('episode/video_clips_new', $data);

Yassine Doghri
committed
public function attemptCreate(): RedirectResponse

Yassine Doghri
committed
'title' => 'required',
'start_time' => 'required|greater_than_equal_to[0]',

Yassine Doghri
committed
'duration' => 'required|greater_than[0]',
'format' => 'required|in_list[' . implode(',', array_keys(config('MediaClipper')->formats)) . ']',
'theme' => 'required|in_list[' . implode(',', array_keys(config('Colors')->themes)) . ']',
];
if (! $this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}

Yassine Doghri
committed
$themeName = $this->request->getPost('theme');
$themeColors = config('MediaClipper')
->themes[$themeName];
$theme = [
'name' => $themeName,
'preview' => $themeColors['preview'],
];

Yassine Doghri
committed
$videoClip = new VideoClip([

Yassine Doghri
committed
'title' => $this->request->getPost('title'),

Yassine Doghri
committed
'start_time' => (float) $this->request->getPost('start_time'),

Yassine Doghri
committed
'duration' => (float) $this->request->getPost('duration',),

Yassine Doghri
committed
'theme' => $theme,
'format' => $this->request->getPost('format'),

Yassine Doghri
committed
'type' => 'video',
'status' => 'queued',
'podcast_id' => $this->podcast->id,
'episode_id' => $this->episode->id,
'created_by' => user_id(),
'updated_by' => user_id(),
]);

Yassine Doghri
committed
// Check if video clip exists before inserting a new line
if ((new ClipModel())->doesVideoClipExist($videoClip)) {
// video clip already exists
return redirect()
->back()
->withInput()
->with('error', lang('VideoClip.messages.alreadyExistingError'));
}

Yassine Doghri
committed
(new ClipModel())->insert($videoClip);

Yassine Doghri
committed
return redirect()->route('video-clips-list', [$this->podcast->id, $this->episode->id])->with(

Yassine Doghri
committed
lang('VideoClip.messages.addToQueueSuccess')

Yassine Doghri
committed

Yassine Doghri
committed
public function retry(string $videoClipId): RedirectResponse
{
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
if ($videoClip === null) {
throw PageNotFoundException::forPageNotFound();
}
(new ClipModel())->update($videoClip->id, [
'status' => 'queued',
'job_started_at' => null,
'job_ended_at' => null,
]);
return redirect()->back();
}

Yassine Doghri
committed
public function delete(string $videoClipId): RedirectResponse
{
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
if ($videoClip === null) {
throw PageNotFoundException::forPageNotFound();
}
if ($videoClip->media === null) {
// delete Clip directly
(new ClipModel())->deleteVideoClip($this->podcast->id, $this->episode->id, $videoClip->id);

Yassine Doghri
committed
} else {
(new ClipModel())->clearVideoClipCache($videoClip->id);

Yassine Doghri
committed
$mediaModel = new MediaModel();
// delete the videoClip file, the clip will be deleted on cascade

Yassine Doghri
committed
if (! $mediaModel->deleteMedia($videoClip->media)) {
return redirect()
->back()
->withInput()
->with('errors', $mediaModel->errors());
}
}
return redirect()->back();
}