Commit 58e2a00a authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(media): save audio, images, transcripts and chapters to media for episode and persons

parent fafaa7e6
......@@ -126,6 +126,10 @@ class Images extends BaseConfig
],
];
public string $avatarDefaultPath = 'castopod-avatar-default.jpg';
public string $avatarDefaultMimeType = 'image/jpg';
public string $podcastBannerDefaultPath = 'castopod-banner-default.jpg';
public string $podcastBannerDefaultMimeType = 'image/jpeg';
......
......@@ -37,7 +37,7 @@ class AddMedia extends Migration
],
'file_metadata' => [
'type' => 'JSON',
'nullable' => true,
'null' => true,
],
'type' => [
'type' => 'ENUM',
......@@ -46,6 +46,7 @@ class AddMedia extends Migration
],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'language_code' => [
'type' => 'VARCHAR',
......
......@@ -54,7 +54,6 @@ class AddPodcasts extends Migration
'type' => 'INT',
'unsigned' => true,
'null' => true,
'default' => null,
],
'language_code' => [
'type' => 'VARCHAR',
......@@ -69,7 +68,6 @@ class AddPodcasts extends Migration
'type' => 'ENUM',
'constraint' => ['clean', 'explicit'],
'null' => true,
'default' => null,
],
'owner_name' => [
'type' => 'VARCHAR',
......
......@@ -79,7 +79,6 @@ class AddEpisodes extends Migration
'type' => 'ENUM',
'constraint' => ['clean', 'explicit'],
'null' => true,
'default' => null,
],
'number' => [
'type' => 'INT',
......
......@@ -39,7 +39,6 @@ class AddPlatforms extends Migration
'type' => 'VARCHAR',
'constraint' => 512,
'null' => true,
'default' => null,
],
]);
$this->forge->addField('`created_at` timestamp NOT NULL DEFAULT NOW()');
......
......@@ -15,14 +15,13 @@ use App\Entities\Media\Chapters;
use App\Entities\Media\Image;
use App\Entities\Media\Transcript;
use App\Libraries\SimpleRSSElement;
use App\Models\ClipsModel;
use App\Models\ClipModel;
use App\Models\EpisodeCommentModel;
use App\Models\MediaModel;
use App\Models\PersonModel;
use App\Models\PodcastModel;
use App\Models\PostModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use CodeIgniter\I18n\Time;
use League\CommonMark\CommonMarkConverter;
......@@ -181,7 +180,7 @@ class Episode extends Entity
} else {
$cover = new Image([
'file_name' => $this->attributes['slug'],
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
'sizes' => config('Images')
->podcastCoverSizes,
'uploaded_by' => user_id(),
......@@ -197,10 +196,19 @@ class Episode extends Entity
public function getCover(): Image
{
if (! $this->cover instanceof Image) {
$this->cover = (new MediaModel('image'))->getMediaById($this->cover_id);
if ($this->cover instanceof Image) {
return $this->cover;
}
if ($this->cover_id === null) {
$this->cover = $this->getPodcast()
->getCover();
return $this->cover;
}
$this->cover = (new MediaModel('image'))->getMediaById($this->cover_id);
return $this->cover;
}
......@@ -210,22 +218,22 @@ class Episode extends Entity
return $this;
}
if ($this->audio_id !== null) {
if ($this->audio_id !== 0) {
$this->getAudio()
->setFile($file);
$this->getAudio()
->updated_by = (int) user_id();
(new MediaModel('audio'))->updateMedia($this->getAudio());
} else {
$transcript = new Audio([
$audio = new Audio([
'file_name' => $this->attributes['slug'],
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
$transcript->setFile($file);
$audio->setFile($file);
$this->attributes['transcript_id'] = (new MediaModel())->saveMedia($transcript);
$this->attributes['audio_id'] = (new MediaModel())->saveMedia($audio);
}
return $this;
......@@ -255,7 +263,7 @@ class Episode extends Entity
} else {
$transcript = new Transcript([
'file_name' => $this->attributes['slug'] . '-transcript',
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
......@@ -291,7 +299,7 @@ class Episode extends Entity
} else {
$chapters = new Chapters([
'file_name' => $this->attributes['slug'] . '-chapters',
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'file_directory' => 'podcasts/' . $this->getPodcast()->handle,
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
......@@ -306,7 +314,7 @@ class Episode extends Entity
public function getChapters(): ?Chapters
{
if ($this->chapters_id !== null && $this->chapters === null) {
$this->chapters = (new MediaModel('document'))->getMediaById($this->chapters_id);
$this->chapters = (new MediaModel('chapters'))->getMediaById($this->chapters_id);
}
return $this->chapters;
......@@ -324,7 +332,7 @@ class Episode extends Entity
helper('analytics');
// remove 'podcasts/' from audio file path
$strippedAudioFilePath = substr($this->audio->file_path, 9);
$strippedAudioFilePath = substr($this->getAudio()->file_path, 9);
return generate_episode_analytics_url(
$this->podcast_id,
......@@ -400,7 +408,7 @@ class Episode extends Entity
}
if ($this->clips === null) {
$this->clips = (new ClipsModel())->getEpisodeClips($this->getPodcast() ->id, $this->id);
$this->clips = (new ClipModel())->getEpisodeClips($this->getPodcast() ->id, $this->id);
}
return $this->clips;
......
......@@ -39,12 +39,13 @@ class Audio extends BaseMedia
parent::setFile($file);
$getID3 = new GetID3();
$audioMetadata = $getID3->analyze((string) $file);
$audioMetadata = $getID3->analyze(media_path($this->file_path));
$this->attributes['file_mimetype'] = $audioMetadata['mimetype'];
$this->attributes['file_mimetype'] = $audioMetadata['mime_type'];
$this->attributes['file_size'] = $audioMetadata['filesize'];
$this->attributes['description'] = $audioMetadata['comments']['comment'];
$this->attributes['file_metadata'] = $audioMetadata;
// @phpstan-ignore-next-line
$this->attributes['description'] = @$audioMetadata['id3v2']['comments']['comment'];
$this->attributes['file_metadata'] = json_encode($audioMetadata);
return $this;
}
......
......@@ -20,11 +20,12 @@ use CodeIgniter\Files\File;
* @property string $file_directory
* @property string $file_extension
* @property string $file_name
* @property string $file_name_with_extension
* @property int $file_size
* @property string $file_mimetype
* @property array $file_metadata
* @property array|null $file_metadata
* @property 'image'|'audio'|'video'|'document' $type
* @property string $description
* @property string|null $description
* @property string|null $language_code
* @property int $uploaded_by
* @property int $updated_by
......@@ -33,8 +34,6 @@ class BaseMedia extends Entity
{
protected File $file;
protected string $type = 'document';
/**
* @var string[]
*/
......@@ -49,9 +48,9 @@ class BaseMedia extends Entity
'file_path' => 'string',
'file_size' => 'int',
'file_mimetype' => 'string',
'file_metadata' => 'json-array',
'file_metadata' => '?json-array',
'type' => 'string',
'description' => 'string',
'description' => '?string',
'language_code' => '?string',
'uploaded_by' => 'integer',
'updated_by' => 'integer',
......@@ -81,6 +80,7 @@ class BaseMedia extends Entity
$this->attributes['file_name'] = $filename;
$this->attributes['file_directory'] = $dirname;
$this->attributes['file_extension'] = $extension;
$this->attributes['file_name_with_extension'] = "{$filename}.{$extension}";
}
}
......@@ -101,4 +101,10 @@ class BaseMedia extends Entity
return $this;
}
public function deleteFile(): void
{
helper('media');
unlink(media_path($this->file_path));
}
}
......@@ -70,9 +70,7 @@ class Image extends BaseMedia
public function deleteFile(): void
{
helper('media');
unlink(media_path($this->file_path));
parent::deleteFile();
$this->deleteSizes();
}
......
......@@ -14,6 +14,7 @@ use App\Entities\Media\Image;
use App\Models\MediaModel;
use App\Models\PersonModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use RuntimeException;
......@@ -55,20 +56,20 @@ class Person extends Entity
/**
* Saves the person avatar in `public/media/persons/`
*/
public function setAvatar(?UploadedFile $file = null): static
public function setAvatar(UploadedFile | File $file = null): static
{
if ($file === null || ! $file->isValid()) {
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
return $this;
}
if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) {
if (array_key_exists('avatar_id', $this->attributes) && $this->attributes['avatar_id'] !== null) {
$this->getAvatar()
->setFile($file);
$this->getAvatar()
->updated_by = (int) user_id();
(new MediaModel('image'))->updateMedia($this->getAvatar());
} else {
$cover = new Image([
$avatar = new Image([
'file_name' => $this->attributes['unique_name'],
'file_directory' => 'persons',
'sizes' => config('Images')
......@@ -76,9 +77,9 @@ class Person extends Entity
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
$cover->setFile($file);
$avatar->setFile($file);
$this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover);
$this->attributes['avatar_id'] = (new MediaModel('image'))->saveMedia($avatar);
}
return $this;
......@@ -89,10 +90,15 @@ class Person extends Entity
if ($this->attributes['avatar_id'] === null) {
helper('media');
return new Image([
'file_path' => media_path('castopod-avatar-default.jpg'),
'file_mimetype' => 'image/jpeg',
'sizes' => config('Images')
->personAvatarSizes,
'file_path' => config('Images')
->avatarDefaultPath,
'file_mimetype' => config('Images')
->avatarDefaultMimeType,
'file_size' => 0,
'file_metadata' => [
'sizes' => config('Images')
->personAvatarSizes,
],
]);
}
......
......@@ -19,6 +19,7 @@ use App\Models\PersonModel;
use App\Models\PlatformModel;
use App\Models\UserModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use CodeIgniter\I18n\Time;
use League\CommonMark\CommonMarkConverter;
......@@ -194,9 +195,9 @@ class Podcast extends Entity
return $this->actor;
}
public function setCover(?UploadedFile $file = null): self
public function setCover(UploadedFile | File $file = null): self
{
if ($file === null || ! $file->isValid()) {
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
return $this;
}
......@@ -232,9 +233,9 @@ class Podcast extends Entity
return $this->cover;
}
public function setBanner(?UploadedFile $file): self
public function setBanner(UploadedFile | File $file = null): self
{
if ($file === null || ! $file->isValid()) {
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
return $this;
}
......
......@@ -144,7 +144,7 @@ if (! function_exists('publication_button')) {
case 'scheduled':
$label = lang('Episode.publish_edit');
$route = route_to('episode-publish_edit', $podcastId, $episodeId);
$variant = 'accent';
$variant = 'warning';
$iconLeft = 'upload-cloud';
break;
case 'published':
......
......@@ -267,7 +267,7 @@ if (! function_exists('get_rss_feed')) {
$transcriptElement->addAttribute('language', $podcast->language_code);
}
if ($episode->chapters->file_url !== '') {
if ($episode->getChapters() !== null) {
$chaptersElement = $item->addChild('chapters', null, $podcastNamespace);
$chaptersElement->addAttribute('url', $episode->chapters->file_url);
$chaptersElement->addAttribute('type', 'application/json+chapters');
......
......@@ -16,7 +16,7 @@ use App\Entities\Clip;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Model;
class ClipsModel extends Model
class ClipModel extends Model
{
/**
* @var string
......
......@@ -128,8 +128,10 @@ class MediaModel extends Model
return $this->update($media->id, $media);
}
public function deleteMedia(int $mediaId): bool
public function deleteMedia(object $media): bool
{
return $this->delete($mediaId, true);
$media->deleteFile();
return $this->delete($media->id, true);
}
}
......@@ -15,9 +15,10 @@ use App\Entities\EpisodeComment;
use App\Entities\Location;
use App\Entities\Podcast;
use App\Entities\Post;
use App\Models\ClipsModel;
use App\Models\ClipModel;
use App\Models\EpisodeCommentModel;
use App\Models\EpisodeModel;
use App\Models\MediaModel;
use App\Models\PodcastModel;
use App\Models\PostModel;
use CodeIgniter\Exceptions\PageNotFoundException;
......@@ -125,6 +126,9 @@ class EpisodeController extends BaseController
->with('errors', $this->validator->getErrors());
}
$db = db_connect();
$db->transStart();
$newEpisode = new Episode([
'podcast_id' => $this->podcast->id,
'title' => $this->request->getPost('title'),
......@@ -143,10 +147,10 @@ class EpisodeController extends BaseController
? $this->request->getPost('parental_advisory')
: null,
'number' => $this->request->getPost('episode_number')
? $this->request->getPost('episode_number')
? (int) $this->request->getPost('episode_number')
: null,
'season_number' => $this->request->getPost('season_number')
? $this->request->getPost('season_number')
? (int) $this->request->getPost('season_number')
: null,
'type' => $this->request->getPost('type'),
'is_blocked' => $this->request->getPost('block') === 'yes',
......@@ -156,9 +160,6 @@ class EpisodeController extends BaseController
'published_at' => null,
]);
$db = db_connect();
$db->transStart();
$transcriptChoice = $this->request->getPost('transcript-choice');
if ($transcriptChoice === 'upload-file') {
$newEpisode->setTranscript($this->request->getFile('transcript_file'));
......@@ -178,8 +179,8 @@ class EpisodeController extends BaseController
}
$episodeModel = new EpisodeModel();
if (! ($newEpisodeId = $episodeModel->insert($newEpisode, true))) {
$db->transRollback();
return redirect()
->back()
->withInput()
......@@ -195,6 +196,7 @@ class EpisodeController extends BaseController
$podcastModel = new PodcastModel();
if (! $podcastModel->update($this->podcast->id, $this->podcast)) {
$db->transRollback();
return redirect()
->back()
->withInput()
......@@ -202,6 +204,8 @@ class EpisodeController extends BaseController
}
}
$db->transComplete();
return redirect()->route('episode-view', [$this->podcast->id, $newEpisodeId]);
}
......@@ -268,36 +272,34 @@ class EpisodeController extends BaseController
if ($transcriptChoice === 'upload-file') {
$transcriptFile = $this->request->getFile('transcript_file');
if ($transcriptFile !== null && $transcriptFile->isValid()) {
$this->episode->transcript_file = $transcriptFile;
$this->episode->setTranscript($transcriptFile);
$this->episode->transcript_remote_url = null;
}
} elseif ($transcriptChoice === 'remote-url') {
if (
($transcriptFileRemoteUrl = $this->request->getPost('transcript_remote_url')) &&
(($transcriptFile = $this->episode->transcript_file) !== null)
($transcriptRemoteUrl = $this->request->getPost('transcript_remote_url')) &&
(($transcriptFile = $this->episode->transcript_id) !== null)
) {
unlink((string) $transcriptFile);
$this->episode->transcript->file_path = null;
(new MediaModel())->deleteMedia($this->episode->transcript);
}
$this->episode->transcript_remote_url = $transcriptFileRemoteUrl === '' ? null : $transcriptFileRemoteUrl;
$this->episode->transcript_remote_url = $transcriptRemoteUrl === '' ? null : $transcriptRemoteUrl;
}
$chaptersChoice = $this->request->getPost('chapters-choice');
if ($chaptersChoice === 'upload-file') {
$chaptersFile = $this->request->getFile('chapters_file');
if ($chaptersFile !== null && $chaptersFile->isValid()) {
$this->episode->chapters = $chaptersFile;
$this->episode->setChapters($chaptersFile);
$this->episode->chapters_remote_url = null;
}
} elseif ($chaptersChoice === 'remote-url') {
if (
($chaptersFileRemoteUrl = $this->request->getPost('chapters_remote_url')) &&
(($chaptersFile = $this->episode->chapters_file) !== null)
($chaptersRemoteUrl = $this->request->getPost('chapters_remote_url')) &&
(($chaptersFile = $this->episode->chapters) !== null)
) {
unlink((string) $chaptersFile);
$this->episode->chapters->file_path = null;
(new MediaModel())->deleteMedia($this->episode->chapters);
}
$this->episode->chapters_remote_url = $chaptersFileRemoteUrl === '' ? null : $chaptersFileRemoteUrl;
$this->episode->chapters_remote_url = $chaptersRemoteUrl === '' ? null : $chaptersRemoteUrl;
}
$db = db_connect();
......@@ -338,16 +340,12 @@ class EpisodeController extends BaseController
public function transcriptDelete(): RedirectResponse
{
unlink((string) $this->episode->transcript_file);
$this->episode->transcript->file_path = null;
$episodeModel = new EpisodeModel();
if (! $episodeModel->update($this->episode->id, $this->episode)) {
$mediaModel = new MediaModel();
if (! $mediaModel->deleteMedia($this->episode->transcript)) {
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
->with('errors', $mediaModel->errors());
}
return redirect()->back();
......@@ -355,16 +353,12 @@ class EpisodeController extends BaseController
public function chaptersDelete(): RedirectResponse
{
unlink((string) $this->episode->chapters_file);
$this->episode->chapters->file_path = null;
$episodeModel = new EpisodeModel();
if (! $episodeModel->update($this->episode->id, $this->episode)) {
$mediaModel = new MediaModel();
if (! $mediaModel->deleteMedia($this->episode->chapters)) {
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
->with('errors', $mediaModel->errors());
}
return redirect()->back();
......@@ -797,7 +791,7 @@ class EpisodeController extends BaseController
public function soundbiteDelete(string $clipId): RedirectResponse
{
(new ClipsModel())->deleteClip($this->podcast->id, $this->episode->id, (int) $clipId);
(new ClipModel())->deleteClip($this->podcast->id, $this->episode->id, (int) $clipId);
return redirect()->route('clips-edit', [$this->podcast->id, $this->episode->id]);
}
......
......@@ -76,24 +76,29 @@ class PersonController extends BaseController
->with('errors', $this->validator->getErrors());
}
$db = db_connect();
$db->transStart();
$person = new Person([
'avatar' => $this->request->getFile('avatar'),
'full_name' => $this->request->getPost('full_name'),
'unique_name' => $this->request->getPost('unique_name'),
'information_url' => $this->request->getPost('information_url'),
'avatar' => $this->request->getFile('avatar'),
'created_by' => user_id(),
'updated_by' => user_id(),
]);
$personModel = new PersonModel();
if (! $personModel->insert($person)) {
$db->transRollback();
return redirect()
->back()
->withInput()
->with('errors', $personModel->errors());
}
$db->transComplete();
return redirect()->route('person-list');
}
......@@ -128,11 +133,7 @@ class PersonController extends BaseController
$this->person->full_name = $this->request->getPost('full_name');