2021-12-02 16:45:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2021-12-02 16:45:41 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Modules\Admin\Controllers;
|
|
|
|
|
2021-12-21 16:25:03 +00:00
|
|
|
use App\Entities\Clip\VideoClip;
|
2021-12-02 16:45:41 +00:00
|
|
|
use App\Entities\Episode;
|
|
|
|
use App\Entities\Podcast;
|
2021-12-21 16:25:03 +00:00
|
|
|
use App\Models\ClipModel;
|
2021-12-02 16:45:41 +00:00
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
use App\Models\PodcastModel;
|
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2023-08-27 13:26:06 +00:00
|
|
|
use Config\Colors;
|
2023-04-14 11:11:53 +00:00
|
|
|
use Modules\Media\Entities\Transcript;
|
2023-03-16 13:00:05 +00:00
|
|
|
use Modules\Media\Models\MediaModel;
|
2023-08-27 13:26:06 +00:00
|
|
|
use Modules\MediaClipper\Config\MediaClipper;
|
2021-12-02 16:45:41 +00:00
|
|
|
|
2021-12-07 16:58:12 +00:00
|
|
|
class VideoClipsController extends BaseController
|
2021-12-02 16:45:41 +00:00
|
|
|
{
|
|
|
|
protected Podcast $podcast;
|
|
|
|
|
|
|
|
protected Episode $episode;
|
|
|
|
|
|
|
|
public function _remap(string $method, string ...$params): mixed
|
|
|
|
{
|
|
|
|
if (
|
2023-04-14 11:11:53 +00:00
|
|
|
! ($podcast = (new PodcastModel())->getPodcastById((int) $params[0])) instanceof Podcast
|
2021-12-02 16:45:41 +00:00
|
|
|
) {
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->podcast = $podcast;
|
|
|
|
|
|
|
|
if (count($params) > 1) {
|
|
|
|
if (
|
|
|
|
! ($episode = (new EpisodeModel())
|
|
|
|
->where([
|
2023-06-12 14:47:38 +00:00
|
|
|
'id' => $params[1],
|
2021-12-02 16:45:41 +00:00
|
|
|
'podcast_id' => $params[0],
|
|
|
|
])
|
|
|
|
->first())
|
|
|
|
) {
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->episode = $episode;
|
|
|
|
|
|
|
|
unset($params[1]);
|
|
|
|
unset($params[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->{$method}(...$params);
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:58:12 +00:00
|
|
|
public function list(): string
|
|
|
|
{
|
2021-12-24 09:49:34 +00:00
|
|
|
$videoClipsBuilder = (new ClipModel('video'))
|
2021-12-21 16:25:03 +00:00
|
|
|
->where([
|
|
|
|
'podcast_id' => $this->podcast->id,
|
|
|
|
'episode_id' => $this->episode->id,
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'video',
|
2021-12-21 16:25:03 +00:00
|
|
|
])
|
|
|
|
->orderBy('created_at', 'desc');
|
|
|
|
|
2021-12-24 09:49:34 +00:00
|
|
|
$clips = $videoClipsBuilder->paginate(10);
|
|
|
|
|
|
|
|
$videoClips = [];
|
|
|
|
foreach ($clips as $clip) {
|
|
|
|
$videoClips[] = new VideoClip($clip->toArray());
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:58:12 +00:00
|
|
|
$data = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
2021-12-24 09:49:34 +00:00
|
|
|
'videoClips' => $videoClips,
|
2023-06-12 14:47:38 +00:00
|
|
|
'pager' => $videoClipsBuilder->pager,
|
2021-12-07 16:58:12 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
replace_breadcrumb_params([
|
2022-10-15 11:22:08 +00:00
|
|
|
0 => $this->podcast->at_handle,
|
2021-12-07 16:58:12 +00:00
|
|
|
1 => $this->episode->title,
|
|
|
|
]);
|
|
|
|
return view('episode/video_clips_list', $data);
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:56:06 +00:00
|
|
|
public function view($videoClipId): string
|
|
|
|
{
|
|
|
|
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
|
|
|
|
|
|
|
|
$data = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
2021-12-23 16:56:06 +00:00
|
|
|
'videoClip' => $videoClip,
|
|
|
|
];
|
|
|
|
|
|
|
|
replace_breadcrumb_params([
|
2022-10-15 11:22:08 +00:00
|
|
|
0 => $this->podcast->at_handle,
|
2021-12-23 16:56:06 +00:00
|
|
|
1 => $this->episode->title,
|
2022-01-03 13:52:07 +00:00
|
|
|
2 => $videoClip->title,
|
2021-12-23 16:56:06 +00:00
|
|
|
]);
|
|
|
|
return view('episode/video_clip', $data);
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
public function create(): string
|
2021-12-02 16:45:41 +00:00
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
|
|
|
];
|
|
|
|
|
|
|
|
replace_breadcrumb_params([
|
2022-10-15 11:22:08 +00:00
|
|
|
0 => $this->podcast->at_handle,
|
2021-12-07 16:58:12 +00:00
|
|
|
1 => $this->episode->title,
|
2021-12-02 16:45:41 +00:00
|
|
|
]);
|
2021-12-29 17:40:59 +00:00
|
|
|
|
2022-01-02 14:11:05 +00:00
|
|
|
// First, check that requirements to create a video clip are met
|
2022-03-04 15:40:38 +00:00
|
|
|
$ffmpeg = shell_exec('which ffmpeg');
|
2022-01-02 14:11:05 +00:00
|
|
|
$checks = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'ffmpeg' => $ffmpeg !== null,
|
|
|
|
'gd' => extension_loaded('gd'),
|
|
|
|
'freetype' => extension_loaded('gd') && gd_info()['FreeType Support'],
|
2023-04-14 11:11:53 +00:00
|
|
|
'transcript' => $this->episode->transcript instanceof Transcript,
|
2022-01-02 14:11:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (in_array(false, $checks, true)) {
|
|
|
|
$data['checks'] = $checks;
|
|
|
|
|
|
|
|
return view('episode/video_clips_requirements', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
helper('form');
|
2021-12-07 16:58:12 +00:00
|
|
|
return view('episode/video_clips_new', $data);
|
2021-12-02 16:45:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
public function attemptCreate(): RedirectResponse
|
2021-12-02 16:45:41 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'title' => 'required',
|
2022-01-03 13:52:07 +00:00
|
|
|
'start_time' => 'required|greater_than_equal_to[0]',
|
2023-06-12 14:47:38 +00:00
|
|
|
'duration' => 'required|greater_than[0]',
|
2023-08-27 13:26:06 +00:00
|
|
|
'format' => 'required|in_list[' . implode(',', array_keys(config(MediaClipper::class)->formats)) . ']',
|
|
|
|
'theme' => 'required|in_list[' . implode(',', array_keys(config(Colors::class)->themes)) . ']',
|
2021-12-02 16:45:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (! $this->validate($rules)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
|
|
|
$themeName = $validData['theme'];
|
2023-08-27 13:26:06 +00:00
|
|
|
$themeColors = config(MediaClipper::class)
|
2021-12-24 09:49:34 +00:00
|
|
|
->themes[$themeName];
|
|
|
|
$theme = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'name' => $themeName,
|
2021-12-24 09:49:34 +00:00
|
|
|
'preview' => $themeColors['preview'],
|
|
|
|
];
|
|
|
|
|
2021-12-21 16:25:03 +00:00
|
|
|
$videoClip = new VideoClip([
|
2023-08-29 15:42:52 +00:00
|
|
|
'title' => $validData['title'],
|
|
|
|
'start_time' => (float) $validData['start_time'],
|
|
|
|
'duration' => (float) $validData['duration'],
|
2023-06-12 14:47:38 +00:00
|
|
|
'theme' => $theme,
|
2023-08-29 15:42:52 +00:00
|
|
|
'format' => $validData['format'],
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'video',
|
|
|
|
'status' => 'queued',
|
2021-12-21 16:25:03 +00:00
|
|
|
'podcast_id' => $this->podcast->id,
|
|
|
|
'episode_id' => $this->episode->id,
|
|
|
|
'created_by' => user_id(),
|
|
|
|
'updated_by' => user_id(),
|
|
|
|
]);
|
|
|
|
|
2022-01-21 17:25:27 +00:00
|
|
|
// 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'));
|
|
|
|
}
|
|
|
|
|
2021-12-21 16:25:03 +00:00
|
|
|
(new ClipModel())->insert($videoClip);
|
2021-12-02 16:45:41 +00:00
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
return redirect()->route('video-clips-list', [$this->podcast->id, $this->episode->id])->with(
|
2021-12-02 16:45:41 +00:00
|
|
|
'message',
|
2022-01-21 17:25:27 +00:00
|
|
|
lang('VideoClip.messages.addToQueueSuccess')
|
2021-12-02 16:45:41 +00:00
|
|
|
);
|
|
|
|
}
|
2021-12-24 17:55:56 +00:00
|
|
|
|
2022-01-02 14:11:05 +00:00
|
|
|
public function retry(string $videoClipId): RedirectResponse
|
|
|
|
{
|
|
|
|
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
|
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
if (! $videoClip instanceof VideoClip) {
|
2022-01-02 14:11:05 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
(new ClipModel())->update($videoClip->id, [
|
2023-06-12 14:47:38 +00:00
|
|
|
'status' => 'queued',
|
2022-01-02 14:11:05 +00:00
|
|
|
'job_started_at' => null,
|
2023-06-12 14:47:38 +00:00
|
|
|
'job_ended_at' => null,
|
2022-01-02 14:11:05 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
public function delete(string $videoClipId): RedirectResponse
|
|
|
|
{
|
|
|
|
$videoClip = (new ClipModel())->getVideoClipById((int) $videoClipId);
|
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
if (! $videoClip instanceof VideoClip) {
|
2021-12-24 17:55:56 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($videoClip->media === null) {
|
|
|
|
// delete Clip directly
|
2022-01-10 16:52:12 +00:00
|
|
|
(new ClipModel())->deleteVideoClip($this->podcast->id, $this->episode->id, $videoClip->id);
|
2021-12-24 17:55:56 +00:00
|
|
|
} else {
|
2022-01-10 16:52:12 +00:00
|
|
|
(new ClipModel())->clearVideoClipCache($videoClip->id);
|
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
$mediaModel = new MediaModel();
|
2022-01-10 16:52:12 +00:00
|
|
|
// delete the videoClip file, the clip will be deleted on cascade
|
2021-12-24 17:55:56 +00:00
|
|
|
if (! $mediaModel->deleteMedia($videoClip->media)) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $mediaModel->errors());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
2021-12-02 16:45:41 +00:00
|
|
|
}
|