From fafaa7e689b17f09a2b056081fa1f4fc53bf716b Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Fri, 17 Dec 2021 17:14:37 +0000 Subject: [PATCH] feat(media): clean media api + create an entity per media type --- .../2020-05-29-120000_add_media.php | 4 +- .../2020-05-30-101500_add_podcasts.php | 2 +- .../2020-06-05-170000_add_episodes.php | 6 +- .../2020-12-25-120000_add_persons.php | 2 +- app/Entities/BaseEntity.php | 11 ++ app/Entities/Episode.php | 148 ++++++++++++++---- app/Entities/ImageOLD.php | 123 --------------- app/Entities/{ => Media}/Audio.php | 6 +- .../{Media.php => Media/BaseMedia.php} | 27 ++-- app/Entities/Media/Chapters.php | 16 ++ app/Entities/Media/Document.php | 16 ++ app/Entities/{ => Media}/Image.php | 42 +++-- app/Entities/Media/Transcript.php | 16 ++ app/Entities/Media/Video.php | 16 ++ app/Entities/MediaOLD.php | 93 ----------- app/Entities/Person.php | 48 ++++-- app/Entities/Podcast.php | 60 +++++++ app/Helpers/rss_helper.php | 14 +- app/Helpers/seo_helper.php | 2 +- app/Libraries/MediaClipper/VideoClip.php | 2 +- app/Libraries/PodcastEpisode.php | 4 +- app/Models/EpisodeModel.php | 16 +- app/Models/MediaModel.php | 52 ++++-- app/Models/MediaModelOLD.php | 112 ------------- app/Models/PersonModel.php | 3 +- app/Models/PodcastModel.php | 4 +- .../Admin/Controllers/EpisodeController.php | 103 +++--------- .../Admin/Controllers/PersonController.php | 7 +- .../Admin/Controllers/PodcastController.php | 69 ++------ .../Controllers/PodcastImportController.php | 1 - modules/Admin/Language/en/Episode.php | 4 +- modules/Admin/Language/fr/Episode.php | 4 +- .../Analytics/Helpers/analytics_helper.php | 4 +- phpstan.neon | 3 +- themes/cp_admin/episode/create.php | 8 +- themes/cp_admin/episode/edit.php | 20 +-- themes/cp_admin/episode/publish.php | 2 +- themes/cp_admin/episode/publish_edit.php | 2 +- themes/cp_admin/episode/soundbites.php | 2 +- themes/cp_admin/episode/view.php | 2 +- themes/cp_admin/podcast/edit.php | 2 +- themes/cp_app/embed.php | 2 +- themes/cp_app/episode/_layout.php | 2 +- themes/cp_app/episode/_partials/card.php | 2 +- .../cp_app/episode/_partials/preview_card.php | 2 +- themes/cp_app/podcast/follow.php | 2 +- 46 files changed, 480 insertions(+), 608 deletions(-) create mode 100644 app/Entities/BaseEntity.php delete mode 100644 app/Entities/ImageOLD.php rename app/Entities/{ => Media}/Audio.php (89%) rename app/Entities/{Media.php => Media/BaseMedia.php} (74%) create mode 100644 app/Entities/Media/Chapters.php create mode 100644 app/Entities/Media/Document.php rename app/Entities/{ => Media}/Image.php (72%) create mode 100644 app/Entities/Media/Transcript.php create mode 100644 app/Entities/Media/Video.php delete mode 100644 app/Entities/MediaOLD.php delete mode 100644 app/Models/MediaModelOLD.php diff --git a/app/Database/Migrations/2020-05-29-120000_add_media.php b/app/Database/Migrations/2020-05-29-120000_add_media.php index 711ba069..6f80aad1 100644 --- a/app/Database/Migrations/2020-05-29-120000_add_media.php +++ b/app/Database/Migrations/2020-05-29-120000_add_media.php @@ -31,7 +31,7 @@ class AddMedia extends Migration 'unsigned' => true, 'comment' => 'File size in bytes', ], - 'file_content_type' => [ + 'file_mimetype' => [ 'type' => 'VARCHAR', 'constraint' => 45, ], @@ -42,6 +42,7 @@ class AddMedia extends Migration 'type' => [ 'type' => 'ENUM', 'constraint' => ['image', 'audio', 'video', 'transcript', 'chapters', 'document'], + 'default' => 'document', ], 'description' => [ 'type' => 'TEXT', @@ -71,6 +72,7 @@ class AddMedia extends Migration ]); $this->forge->addKey('id', true); + $this->forge->addUniqueKey('file_path'); $this->forge->addForeignKey('uploaded_by', 'users', 'id'); $this->forge->addForeignKey('updated_by', 'users', 'id'); $this->forge->createTable('media'); diff --git a/app/Database/Migrations/2020-05-30-101500_add_podcasts.php b/app/Database/Migrations/2020-05-30-101500_add_podcasts.php index ff2f913c..436c741b 100644 --- a/app/Database/Migrations/2020-05-30-101500_add_podcasts.php +++ b/app/Database/Migrations/2020-05-30-101500_add_podcasts.php @@ -198,7 +198,7 @@ class AddPodcasts extends Migration $this->forge->addUniqueKey('actor_id'); $this->forge->addForeignKey('actor_id', config('Fediverse')->tablesPrefix . 'actors', 'id', '', 'CASCADE'); $this->forge->addForeignKey('cover_id', 'media', 'id'); - $this->forge->addForeignKey('banner_id', 'media', 'id'); + $this->forge->addForeignKey('banner_id', 'media', 'id', '', 'SET NULL'); $this->forge->addForeignKey('category_id', 'categories', 'id'); $this->forge->addForeignKey('language_code', 'languages', 'code'); $this->forge->addForeignKey('created_by', 'users', 'id'); diff --git a/app/Database/Migrations/2020-06-05-170000_add_episodes.php b/app/Database/Migrations/2020-06-05-170000_add_episodes.php index 04656978..9e0efaa2 100644 --- a/app/Database/Migrations/2020-06-05-170000_add_episodes.php +++ b/app/Database/Migrations/2020-06-05-170000_add_episodes.php @@ -157,9 +157,9 @@ class AddEpisodes extends Migration $this->forge->addUniqueKey(['podcast_id', 'slug']); $this->forge->addForeignKey('podcast_id', 'podcasts', 'id', '', 'CASCADE'); $this->forge->addForeignKey('audio_id', 'media', 'id'); - $this->forge->addForeignKey('cover_id', 'media', 'id'); - $this->forge->addForeignKey('transcript_id', 'media', 'id'); - $this->forge->addForeignKey('chapters_id', 'media', 'id'); + $this->forge->addForeignKey('cover_id', 'media', 'id', '', 'SET NULL'); + $this->forge->addForeignKey('transcript_id', 'media', 'id', '', 'SET NULL'); + $this->forge->addForeignKey('chapters_id', 'media', 'id', '', 'SET NULL'); $this->forge->addForeignKey('created_by', 'users', 'id'); $this->forge->addForeignKey('updated_by', 'users', 'id'); $this->forge->createTable('episodes'); diff --git a/app/Database/Migrations/2020-12-25-120000_add_persons.php b/app/Database/Migrations/2020-12-25-120000_add_persons.php index 66b53ba1..f7515ed4 100644 --- a/app/Database/Migrations/2020-12-25-120000_add_persons.php +++ b/app/Database/Migrations/2020-12-25-120000_add_persons.php @@ -64,7 +64,7 @@ class AddPersons extends Migration ]); $this->forge->addKey('id', true); - $this->forge->addForeignKey('avatar_id', 'media', 'id'); + $this->forge->addForeignKey('avatar_id', 'media', 'id', '', 'SET NULL'); $this->forge->addForeignKey('created_by', 'users', 'id'); $this->forge->addForeignKey('updated_by', 'users', 'id'); $this->forge->createTable('persons'); diff --git a/app/Entities/BaseEntity.php b/app/Entities/BaseEntity.php new file mode 100644 index 00000000..b9f888a4 --- /dev/null +++ b/app/Entities/BaseEntity.php @@ -0,0 +1,11 @@ + 'integer', ]; - /** - * Saves an episode cover - */ - public function setCover(?Image $cover = null): static + public function setCover(?UploadedFile $file): self { - if ($cover === null) { + if ($file === null || ! $file->isValid()) { return $this; } - // Save image - $cover->saveImage( - config('Images') - ->podcastCoverSizes, - 'podcasts/' . $this->getPodcast()->handle, - $this->attributes['slug'] - ); + if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) { + $this->getCover() + ->setFile($file); + $this->getCover() + ->updated_by = (int) user_id(); + (new MediaModel('image'))->updateMedia($this->getCover()); + } else { + $cover = new Image([ + 'file_name' => $this->attributes['slug'], + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'sizes' => config('Images') + ->podcastCoverSizes, + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $cover->setFile($file); - $this->attributes['cover_mimetype'] = $cover->mimetype; - $this->attributes['cover_path'] = $cover->path; + $this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover); + } return $this; } @@ -193,25 +204,106 @@ class Episode extends Entity return $this->cover; } + public function setAudio(?UploadedFile $file): self + { + if ($file === null || ! $file->isValid()) { + return $this; + } + + if ($this->audio_id !== null) { + $this->getAudio() + ->setFile($file); + $this->getAudio() + ->updated_by = (int) user_id(); + (new MediaModel('audio'))->updateMedia($this->getAudio()); + } else { + $transcript = new Audio([ + 'file_name' => $this->attributes['slug'], + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $transcript->setFile($file); + + $this->attributes['transcript_id'] = (new MediaModel())->saveMedia($transcript); + } + + return $this; + } + public function getAudio(): Audio { - if (! $this->audio) { + if (! $this->audio instanceof Audio) { $this->audio = (new MediaModel('audio'))->getMediaById($this->audio_id); } return $this->audio; } - public function getTranscript(): ?Media + public function setTranscript(?UploadedFile $file): self + { + if ($file === null || ! $file->isValid()) { + return $this; + } + + if ($this->getTranscript() !== null) { + $this->getTranscript() + ->setFile($file); + $this->getTranscript() + ->updated_by = (int) user_id(); + (new MediaModel('transcript'))->updateMedia($this->getTranscript()); + } else { + $transcript = new Transcript([ + 'file_name' => $this->attributes['slug'] . '-transcript', + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $transcript->setFile($file); + + $this->attributes['transcript_id'] = (new MediaModel())->saveMedia($transcript); + } + + return $this; + } + + public function getTranscript(): ?Transcript { if ($this->transcript_id !== null && $this->transcript === null) { - $this->transcript = (new MediaModel('document'))->getMediaById($this->transcript_id); + $this->transcript = (new MediaModel('transcript'))->getMediaById($this->transcript_id); } return $this->transcript; } - public function getChaptersFile(): ?Media + public function setChapters(?UploadedFile $file): self + { + if ($file === null || ! $file->isValid()) { + return $this; + } + + if ($this->getChapters() !== null) { + $this->getChapters() + ->setFile($file); + $this->getChapters() + ->updated_by = (int) user_id(); + (new MediaModel('chapters'))->updateMedia($this->getChapters()); + } else { + $chapters = new Chapters([ + 'file_name' => $this->attributes['slug'] . '-chapters', + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $chapters->setFile($file); + + $this->attributes['chapters_id'] = (new MediaModel())->saveMedia($chapters); + } + + return $this; + } + + public function getChapters(): ?Chapters { if ($this->chapters_id !== null && $this->chapters === null) { $this->chapters = (new MediaModel('document'))->getMediaById($this->chapters_id); @@ -261,7 +353,7 @@ class Episode extends Entity public function getTranscriptUrl(): ?string { if ($this->transcript !== null) { - return $this->transcript->url; + return $this->transcript->file_url; } return $this->transcript_remote_url; } @@ -271,8 +363,8 @@ class Episode extends Entity */ public function getChaptersFileUrl(): ?string { - if ($this->chapters) { - return $this->chapters->url; + if ($this->chapters !== null) { + return $this->chapters->file_url; } return $this->chapters_remote_url; diff --git a/app/Entities/ImageOLD.php b/app/Entities/ImageOLD.php deleted file mode 100644 index d46b29e1..00000000 --- a/app/Entities/ImageOLD.php +++ /dev/null @@ -1,123 +0,0 @@ -> - */ - public array $sizes = []; - - protected Images $config; - - protected string $type = 'image'; - - public function __get($property) - { - if (str_ends_with($property, '_url') || str_ends_with($property, '_path') || str_ends_with( - $property, - '_mimetype' - )) { - $this->initSizeProperties(); - } - - parent::__get($property); - } - - public function setFileMetadata(string $metadata): self - { - $this->attributes['file_metadata'] = $metadata; - - $metadataArray = json_decode($metadata, true); - if (! array_key_exists('sizes', $metadataArray)) { - return $this; - } - - $this->sizes = $metadataArray['sizes']; - - return $this; - } - - public function initSizeProperties(): bool - { - if ($this->file_path === '') { - return false; - } - - if ($this->sizes === []) { - $this->sizes = $this->file_metadata['sizes']; - } - - helper('media'); - - $extension = $this->file_extension; - $mimetype = $this->mimetype; - foreach ($this->sizes as $name => $size) { - if (array_key_exists('extension', $size)) { - $extension = $size['extension']; - } - if (array_key_exists('mimetype', $size)) { - $mimetype = $size['mimetype']; - } - $this->{$name . '_path'} = $this->file_directory . '/' . $this->file_name . '_' . $name . '.' . $extension; - $this->{$name . '_url'} = media_base_url($this->{$name . '_path'}); - $this->{$name . '_mimetype'} = $mimetype; - } - - return true; - } - - public function saveInDisk(File $file, string $dirname, string $filename): void - { - // save original - parent::saveInDisk($file, $dirname, $filename); - - $this->initSizeProperties(); - - // save derived sizes - $imageService = service('image'); - foreach ($this->sizes as $name => $size) { - $pathProperty = $name . '_path'; - $imageService - ->withFile(media_path($this->file_path)) - ->resize($size['width'], $size['height']); - $imageService->save(media_path($this->{$pathProperty})); - } - } - - public function injectFileData(File $file): void - { - $metadata = exif_read_data(media_path($this->file_path), null, true); - - if ($metadata) { - $metadata['sizes'] = $this->sizes; - $this->file_size = $metadata['FILE']['FileSize']; - $this->file_metadata = $metadata; - } - } - - /** - * @param array $sizes - */ - public function delete(array $sizes): void - { - helper('media'); - - foreach (array_keys($sizes) as $name) { - $pathProperty = $name . '_path'; - unlink(media_path($this->{$pathProperty})); - } - } -} diff --git a/app/Entities/Audio.php b/app/Entities/Media/Audio.php similarity index 89% rename from app/Entities/Audio.php rename to app/Entities/Media/Audio.php index 4a342d41..288f9550 100644 --- a/app/Entities/Audio.php +++ b/app/Entities/Media/Audio.php @@ -8,7 +8,7 @@ declare(strict_types=1); * @link https://castopod.org/ */ -namespace App\Entities; +namespace App\Entities\Media; use CodeIgniter\Files\File; use JamesHeinrich\GetID3\GetID3; @@ -17,7 +17,7 @@ use JamesHeinrich\GetID3\GetID3; * @property float $duration * @property int $header_size */ -class Audio extends Media +class Audio extends BaseMedia { protected string $type = 'audio'; @@ -41,7 +41,7 @@ class Audio extends Media $getID3 = new GetID3(); $audioMetadata = $getID3->analyze((string) $file); - $this->attributes['file_content_type'] = $audioMetadata['mimetype']; + $this->attributes['file_mimetype'] = $audioMetadata['mimetype']; $this->attributes['file_size'] = $audioMetadata['filesize']; $this->attributes['description'] = $audioMetadata['comments']['comment']; $this->attributes['file_metadata'] = $audioMetadata; diff --git a/app/Entities/Media.php b/app/Entities/Media/BaseMedia.php similarity index 74% rename from app/Entities/Media.php rename to app/Entities/Media/BaseMedia.php index b979edbc..36b672b9 100644 --- a/app/Entities/Media.php +++ b/app/Entities/Media/BaseMedia.php @@ -8,7 +8,7 @@ declare(strict_types=1); * @link https://castopod.org/ */ -namespace App\Entities; +namespace App\Entities\Media; use CodeIgniter\Entity\Entity; use CodeIgniter\Files\File; @@ -16,11 +16,12 @@ use CodeIgniter\Files\File; /** * @property int $id * @property string $file_path + * @property string $file_url * @property string $file_directory * @property string $file_extension * @property string $file_name * @property int $file_size - * @property string $file_content_type + * @property string $file_mimetype * @property array $file_metadata * @property 'image'|'audio'|'video'|'document' $type * @property string $description @@ -28,7 +29,7 @@ use CodeIgniter\Files\File; * @property int $uploaded_by * @property int $updated_by */ -class Media extends Entity +class BaseMedia extends Entity { protected File $file; @@ -44,9 +45,10 @@ class Media extends Entity */ protected $casts = [ 'id' => 'integer', + 'file_extension' => 'string', 'file_path' => 'string', 'file_size' => 'int', - 'file_content_type' => 'string', + 'file_mimetype' => 'string', 'file_metadata' => 'json-array', 'type' => 'string', 'description' => 'string', @@ -62,16 +64,23 @@ class Media extends Entity { parent::__construct($data); - if ($this->file_path) { + $this->initFileProperties(); + } + + public function initFileProperties(): void + { + if ($this->file_path !== '') { + helper('media'); [ 'filename' => $filename, 'dirname' => $dirname, 'extension' => $extension, ] = pathinfo($this->file_path); - $this->file_name = $filename; - $this->file_directory = $dirname; - $this->file_extension = $extension; + $this->attributes['file_url'] = media_base_url($this->file_path); + $this->attributes['file_name'] = $filename; + $this->attributes['file_directory'] = $dirname; + $this->attributes['file_extension'] = $extension; } } @@ -79,7 +88,7 @@ class Media extends Entity { helper('media'); - $this->attributes['file_content_type'] = $file->getMimeType(); + $this->attributes['file_mimetype'] = $file->getMimeType(); $this->attributes['file_metadata'] = json_encode(lstat((string) $file)); $this->attributes['file_path'] = save_media( $file, diff --git a/app/Entities/Media/Chapters.php b/app/Entities/Media/Chapters.php new file mode 100644 index 00000000..48e7fc74 --- /dev/null +++ b/app/Entities/Media/Chapters.php @@ -0,0 +1,16 @@ +|null $data - */ - public function __construct(array $data = null) + public function initFileProperties(): void { - parent::__construct($data); + parent::initFileProperties(); if ($this->file_path && $this->file_metadata) { $this->sizes = $this->file_metadata['sizes']; @@ -34,7 +34,7 @@ class Image extends Media helper('media'); $extension = $this->file_extension; - $mimetype = $this->mimetype; + $mimetype = $this->file_mimetype; foreach ($this->sizes as $name => $size) { if (array_key_exists('extension', $size)) { $extension = $size['extension']; @@ -62,6 +62,23 @@ class Image extends Media $this->attributes['file_metadata'] = json_encode($metadata); } + $this->initFileProperties(); + $this->saveSizes(); + + return $this; + } + + public function deleteFile(): void + { + helper('media'); + + unlink(media_path($this->file_path)); + + $this->deleteSizes(); + } + + private function saveSizes(): void + { // save derived sizes $imageService = service('image'); foreach ($this->sizes as $name => $size) { @@ -71,7 +88,14 @@ class Image extends Media ->resize($size['width'], $size['height']); $imageService->save(media_path($this->{$pathProperty})); } + } - return $this; + private function deleteSizes(): void + { + // delete all derived sizes + foreach (array_keys($this->sizes) as $name) { + $pathProperty = $name . '_path'; + unlink(media_path($this->{$pathProperty})); + } } } diff --git a/app/Entities/Media/Transcript.php b/app/Entities/Media/Transcript.php new file mode 100644 index 00000000..2a06ef1d --- /dev/null +++ b/app/Entities/Media/Transcript.php @@ -0,0 +1,16 @@ + - */ - protected $casts = [ - 'id' => 'integer', - 'file_path' => 'string', - 'file_size' => 'string', - 'file_content_type' => 'string', - 'file_metadata' => 'json-array', - 'type' => 'string', - 'description' => 'string', - 'language_code' => '?string', - 'uploaded_by' => 'integer', - 'updated_by' => 'integer', - ]; - - public function setFilePath(string $path): self - { - $this->attributes['file_path'] = $path; - - [ - 'filename' => $filename, - 'dirname' => $dirname, - 'extension' => $extension, - ] = pathinfo($path); - - $this->file_name = $filename; - $this->file_directory = $dirname; - $this->file_extension = $extension; - - return $this; - } - - public function saveInDisk(File $file, string $dirname, string $filename): void - { - helper('media'); - - $this->file_content_type = $file->getMimeType(); - - $filePath = save_media($file, $dirname, $filename); - - $this->file_path = $filePath; - } - - public function injectFileData(File $file): void - { - $this->file_content_type = $file->getMimeType(); - $this->type = 'document'; - - if ($filesize = filesize(media_path($this->file_path))) { - $this->file_size = $filesize; - } - } -} diff --git a/app/Entities/Person.php b/app/Entities/Person.php index 10e9fd22..c8b84789 100644 --- a/app/Entities/Person.php +++ b/app/Entities/Person.php @@ -10,8 +10,11 @@ declare(strict_types=1); namespace App\Entities; +use App\Entities\Media\Image; +use App\Models\MediaModel; use App\Models\PersonModel; use CodeIgniter\Entity\Entity; +use CodeIgniter\HTTP\Files\UploadedFile; use RuntimeException; /** @@ -52,31 +55,52 @@ class Person extends Entity /** * Saves the person avatar in `public/media/persons/` */ - public function setAvatar(?Image $avatar = null): static + public function setAvatar(?UploadedFile $file = null): static { - if ($avatar === null) { + if ($file === null || ! $file->isValid()) { return $this; } - helper('media'); + if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) { + $this->getAvatar() + ->setFile($file); + $this->getAvatar() + ->updated_by = (int) user_id(); + (new MediaModel('image'))->updateMedia($this->getAvatar()); + } else { + $cover = new Image([ + 'file_name' => $this->attributes['unique_name'], + 'file_directory' => 'persons', + 'sizes' => config('Images') + ->personAvatarSizes, + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $cover->setFile($file); - $avatar->saveImage(config('Images')->personAvatarSizes, 'persons', $this->attributes['unique_name']); - - $this->attributes['avatar_mimetype'] = $avatar->mimetype; - $this->attributes['avatar_path'] = $avatar->path; + $this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover); + } return $this; } public function getAvatar(): Image { - if ($this->attributes['avatar_path'] === null) { - return new Image(null, '/castopod-avatar-default.jpg', 'image/jpeg', config('Images')->personAvatarSizes); + 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, + ]); } - return new Image(null, $this->attributes['avatar_path'], $this->attributes['avatar_mimetype'], config( - 'Images' - )->personAvatarSizes); + if ($this->avatar === null) { + $this->avatar = (new MediaModel('image'))->getMediaById($this->avatar_id); + } + + return $this->avatar; } /** diff --git a/app/Entities/Podcast.php b/app/Entities/Podcast.php index 1663a6b3..d2e5a04b 100644 --- a/app/Entities/Podcast.php +++ b/app/Entities/Podcast.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace App\Entities; +use App\Entities\Media\Image; use App\Libraries\SimpleRSSElement; use App\Models\CategoryModel; use App\Models\EpisodeModel; @@ -18,6 +19,7 @@ use App\Models\PersonModel; use App\Models\PlatformModel; use App\Models\UserModel; use CodeIgniter\Entity\Entity; +use CodeIgniter\HTTP\Files\UploadedFile; use CodeIgniter\I18n\Time; use League\CommonMark\CommonMarkConverter; use Modules\Auth\Entities\User; @@ -192,6 +194,35 @@ class Podcast extends Entity return $this->actor; } + public function setCover(?UploadedFile $file = null): self + { + if ($file === null || ! $file->isValid()) { + return $this; + } + + if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) { + $this->getCover() + ->setFile($file); + $this->getCover() + ->updated_by = (int) user_id(); + (new MediaModel('image'))->updateMedia($this->getCover()); + } else { + $cover = new Image([ + 'file_name' => 'cover', + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'sizes' => config('Images') + ->podcastCoverSizes, + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $cover->setFile($file); + + $this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover); + } + + return $this; + } + public function getCover(): Image { if (! $this->cover instanceof Image) { @@ -201,6 +232,35 @@ class Podcast extends Entity return $this->cover; } + public function setBanner(?UploadedFile $file): self + { + if ($file === null || ! $file->isValid()) { + return $this; + } + + if (array_key_exists('banner_id', $this->attributes) && $this->attributes['banner_id'] !== null) { + $this->getBanner() + ->setFile($file); + $this->getBanner() + ->updated_by = (int) user_id(); + (new MediaModel('image'))->updateMedia($this->getBanner()); + } else { + $banner = new Image([ + 'file_name' => 'banner', + 'file_directory' => 'podcasts/' . $this->attributes['handle'], + 'sizes' => config('Images') + ->podcastBannerSizes, + 'uploaded_by' => user_id(), + 'updated_by' => user_id(), + ]); + $banner->setFile($file); + + $this->attributes['banner_id'] = (new MediaModel('image'))->saveMedia($banner); + } + + return $this; + } + public function getBanner(): Image { if ($this->banner_id === null) { diff --git a/app/Helpers/rss_helper.php b/app/Helpers/rss_helper.php index fbb845d8..93e40cf2 100644 --- a/app/Helpers/rss_helper.php +++ b/app/Helpers/rss_helper.php @@ -212,7 +212,7 @@ if (! function_exists('get_rss_feed')) { : '?_from=' . urlencode($serviceSlug)), ); $enclosure->addAttribute('length', (string) $episode->audio->file_size); - $enclosure->addAttribute('type', $episode->audio->file_content_type); + $enclosure->addAttribute('type', $episode->audio->file_mimetype); $item->addChild('guid', $episode->guid); $item->addChild('pubDate', $episode->published_at->format(DATE_RFC1123)); @@ -255,25 +255,25 @@ if (! function_exists('get_rss_feed')) { $comments->addAttribute('uri', url_to('episode-comments', $podcast->handle, $episode->slug)); $comments->addAttribute('contentType', 'application/podcast-activity+json'); - if ($episode->transcript->file_url) { + if ($episode->transcript->file_url !== '') { $transcriptElement = $item->addChild('transcript', null, $podcastNamespace); - $transcriptElement->addAttribute('url', $episode->transcript_file_url); + $transcriptElement->addAttribute('url', $episode->transcript->file_url); $transcriptElement->addAttribute( 'type', Mimes::guessTypeFromExtension( - pathinfo($episode->transcript_file_url, PATHINFO_EXTENSION) + pathinfo($episode->transcript->file_url, PATHINFO_EXTENSION) ) ?? 'text/html', ); $transcriptElement->addAttribute('language', $podcast->language_code); } - if ($episode->chapters->file_url) { + if ($episode->chapters->file_url !== '') { $chaptersElement = $item->addChild('chapters', null, $podcastNamespace); - $chaptersElement->addAttribute('url', $episode->chapters_file_url); + $chaptersElement->addAttribute('url', $episode->chapters->file_url); $chaptersElement->addAttribute('type', 'application/json+chapters'); } - foreach ($episode->clip as $clip) { + foreach ($episode->clips as $clip) { // TODO: differentiate video from soundbites? $soundbiteElement = $item->addChild('soundbite', $clip->label, $podcastNamespace); $soundbiteElement->addAttribute('start_time', (string) $clip->start_time); diff --git a/app/Helpers/seo_helper.php b/app/Helpers/seo_helper.php index 72b0c790..6c4d9e83 100644 --- a/app/Helpers/seo_helper.php +++ b/app/Helpers/seo_helper.php @@ -87,7 +87,7 @@ if (! function_exists('get_episode_metatags')) { ->og('image:height', (string) config('Images')->podcastCoverSizes['large']['height']) ->og('locale', $episode->podcast->language_code) ->og('audio', $episode->audio_file_opengraph_url) - ->og('audio:type', $episode->audio->file_content_type) + ->og('audio:type', $episode->audio->file_mimetype) ->meta('article:published_time', $episode->published_at->format(DATE_ISO8601)) ->meta('article:modified_time', $episode->updated_at->format(DATE_ISO8601)) ->twitter('audio:partner', $episode->podcast->publisher ?? '') diff --git a/app/Libraries/MediaClipper/VideoClip.php b/app/Libraries/MediaClipper/VideoClip.php index 647e8545..ad309fef 100644 --- a/app/Libraries/MediaClipper/VideoClip.php +++ b/app/Libraries/MediaClipper/VideoClip.php @@ -80,7 +80,7 @@ class VideoClip helper(['media']); $this->audioInput = media_path($this->episode->audio->file_path); - $this->episodeCoverPath = media_path($this->episode->cover->path); + $this->episodeCoverPath = media_path($this->episode->cover->file_path); if ($this->episode->transcript !== null) { $this->subtitlesInput = media_path($this->episode->transcript->file_path); } diff --git a/app/Libraries/PodcastEpisode.php b/app/Libraries/PodcastEpisode.php index 5d7aece5..a118315d 100644 --- a/app/Libraries/PodcastEpisode.php +++ b/app/Libraries/PodcastEpisode.php @@ -52,7 +52,7 @@ class PodcastEpisode extends ObjectType $this->image = [ 'type' => 'Image', - 'mediaType' => $episode->cover->file_content_type, + 'mediaType' => $episode->cover->file_mimetype, 'url' => $episode->cover->feed_url, ]; @@ -66,7 +66,7 @@ class PodcastEpisode extends ObjectType 'url' => [ 'href' => $episode->audio->file_url, 'type' => 'Link', - 'mediaType' => $episode->audio->file_content_type, + 'mediaType' => $episode->audio->file_mimetype, ], 'transcript' => $episode->transcript->file_url, 'chapters' => $episode->chapters->file_url, diff --git a/app/Models/EpisodeModel.php b/app/Models/EpisodeModel.php index 043e4fb6..1152adee 100644 --- a/app/Models/EpisodeModel.php +++ b/app/Models/EpisodeModel.php @@ -68,14 +68,14 @@ class EpisodeModel extends Model 'guid', 'title', 'slug', - 'audio_file_id', + 'audio_id', 'description_markdown', 'description_html', 'cover_id', - 'transcript_file_id', - 'transcript_file_remote_url', - 'chapters_file_id', - 'chapters_file_remote_url', + 'transcript_id', + 'transcript_remote_url', + 'chapters_id', + 'chapters_remote_url', 'parental_advisory', 'number', 'season_number', @@ -114,13 +114,13 @@ class EpisodeModel extends Model 'podcast_id' => 'required', 'title' => 'required', 'slug' => 'required|regex_match[/^[a-zA-Z0-9\-]{1,191}$/]', - 'audio_file_id' => 'required', + 'audio_id' => 'required', 'description_markdown' => 'required', 'number' => 'is_natural_no_zero|permit_empty', 'season_number' => 'is_natural_no_zero|permit_empty', 'type' => 'required', - 'transcript_file_remote_url' => 'valid_url|permit_empty', - 'chapters_file_remote_url' => 'valid_url|permit_empty', + 'transcript_remote_url' => 'valid_url|permit_empty', + 'chapters_remote_url' => 'valid_url|permit_empty', 'published_at' => 'valid_date|permit_empty', 'created_by' => 'required', 'updated_by' => 'required', diff --git a/app/Models/MediaModel.php b/app/Models/MediaModel.php index 6d760524..ceea6a95 100644 --- a/app/Models/MediaModel.php +++ b/app/Models/MediaModel.php @@ -10,9 +10,12 @@ declare(strict_types=1); namespace App\Models; -use App\Entities\Audio; -use App\Entities\Image; -use App\Entities\Media; +use App\Entities\Media\Audio; +use App\Entities\Media\Chapters; +use App\Entities\Media\Document; +use App\Entities\Media\Image; +use App\Entities\Media\Transcript; +use App\Entities\Media\Video; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Model; use CodeIgniter\Validation\ValidationInterface; @@ -27,7 +30,7 @@ class MediaModel extends Model /** * @var string */ - protected $returnType = Media::class; + protected $returnType = Document::class; /** * @var string[] @@ -36,7 +39,7 @@ class MediaModel extends Model 'id', 'file_path', 'file_size', - 'file_content_type', + 'file_mimetype', 'file_metadata', 'type', 'description', @@ -52,29 +55,36 @@ class MediaModel extends Model * @param ValidationInterface|null $validation Validation */ public function __construct( - protected string $fileType, + protected string $fileType = 'document', ConnectionInterface &$db = null, ValidationInterface $validation = null ) { + // @phpstan-ignore-next-line switch ($fileType) { case 'audio': $this->returnType = Audio::class; break; + case 'video': + $this->returnType = Video::class; + break; case 'image': $this->returnType = Image::class; break; + case 'transcript': + $this->returnType = Transcript::class; + break; + case 'chapters': + $this->returnType = Chapters::class; + break; default: - // do nothing, keep Media class as default + // do nothing, keep Document class as default break; } parent::__construct($db, $validation); } - /** - * @return Media|Image|Audio - */ - public function getMediaById(int $mediaId): object + public function getMediaById(int $mediaId): Document | Audio | Video | Image | Transcript | Chapters { $cacheName = "media#{$mediaId}"; if (! ($found = cache($cacheName))) { @@ -94,7 +104,9 @@ class MediaModel extends Model } /** - * @param Media|Image|Audio $media + * @param Document|Audio|Video|Image|Transcript|Chapters $media + * + * @noRector ReturnTypeDeclarationRector */ public function saveMedia(object $media): int | false { @@ -103,7 +115,21 @@ class MediaModel extends Model return false; } - // @phpstan-ignore-next-line return $mediaId; } + + /** + * @param Document|Audio|Video|Image|Transcript|Chapters $media + * + * @noRector ReturnTypeDeclarationRector + */ + public function updateMedia(object $media): bool + { + return $this->update($media->id, $media); + } + + public function deleteMedia(int $mediaId): bool + { + return $this->delete($mediaId, true); + } } diff --git a/app/Models/MediaModelOLD.php b/app/Models/MediaModelOLD.php deleted file mode 100644 index fcdc5660..00000000 --- a/app/Models/MediaModelOLD.php +++ /dev/null @@ -1,112 +0,0 @@ -returnType = Audio::class; - break; - case 'image': - $this->returnType = Image::class; - break; - default: - // do nothing, keep Media class as default - break; - } - - parent::__construct($db, $validation); - } - - /** - * @return Media|Image|Audio - */ - public function getMediaById(int $mediaId): object - { - $cacheName = "media#{$mediaId}"; - if (! ($found = cache($cacheName))) { - $builder = $this->where([ - 'id' => $mediaId, - ]); - - $found = $builder->first(); - - cache() - ->save($cacheName, $found, DECADE); - } - - return $found; - } - - /** - * @param Media|Image $media - */ - public function saveMedia(object $media): int | false - { - // insert record in database - if (! $mediaId = $this->insert($media, true)) { - return false; - } - - // @phpstan-ignore-next-line - return $mediaId; - } - - public function deleteFile(int $mediaId): void - { - // TODO: get file, delete it from disk & from database - } -} diff --git a/app/Models/PersonModel.php b/app/Models/PersonModel.php index 9e568882..9a64585e 100644 --- a/app/Models/PersonModel.php +++ b/app/Models/PersonModel.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace App\Models; -use App\Entities\Image; use App\Entities\Person; use CodeIgniter\Database\Query; use CodeIgniter\Model; @@ -196,7 +195,7 @@ class PersonModel extends Model 'full_name' => $fullName, 'unique_name' => slugify($fullName), 'information_url' => $informationUrl, - 'image' => new Image(download_file($image)), + 'image' => download_file($image), 'created_by' => user_id(), 'updated_by' => user_id(), ]); diff --git a/app/Models/PodcastModel.php b/app/Models/PodcastModel.php index 02a913bb..9b639d69 100644 --- a/app/Models/PodcastModel.php +++ b/app/Models/PodcastModel.php @@ -484,9 +484,9 @@ class PodcastModel extends Model $actor->display_name = $podcast->title; $actor->summary = $podcast->description_html; $actor->avatar_image_url = $podcast->cover->federation_url; - $actor->avatar_image_mimetype = $podcast->cover->mimetype; + $actor->avatar_image_mimetype = $podcast->cover->file_mimetype; $actor->cover_image_url = $podcast->banner->federation_url; - $actor->cover_image_mimetype = $podcast->banner->mimetype; + $actor->cover_image_mimetype = $podcast->banner->file_mimetype; if ($actor->hasChanged()) { $actorModel->update($actor->id, $actor); diff --git a/modules/Admin/Controllers/EpisodeController.php b/modules/Admin/Controllers/EpisodeController.php index b0847e1c..637f84da 100644 --- a/modules/Admin/Controllers/EpisodeController.php +++ b/modules/Admin/Controllers/EpisodeController.php @@ -12,15 +12,12 @@ namespace Modules\Admin\Controllers; use App\Entities\Episode; use App\Entities\EpisodeComment; -use App\Entities\Image; use App\Entities\Location; -use App\Entities\Media; use App\Entities\Podcast; use App\Entities\Post; use App\Models\ClipsModel; use App\Models\EpisodeCommentModel; use App\Models\EpisodeModel; -use App\Models\MediaModel; use App\Models\PodcastModel; use App\Models\PostModel; use CodeIgniter\Exceptions\PageNotFoundException; @@ -133,7 +130,8 @@ class EpisodeController extends BaseController 'title' => $this->request->getPost('title'), 'slug' => $this->request->getPost('slug'), 'guid' => null, - 'audio_file' => $this->request->getFile('audio_file'), + 'audio' => $this->request->getFile('audio_file'), + 'cover' => $this->request->getFile('cover'), 'description_markdown' => $this->request->getPost('description'), 'location' => $this->request->getPost('location_name') === '' ? null : new Location($this->request->getPost( 'location_name' @@ -161,69 +159,22 @@ class EpisodeController extends BaseController $db = db_connect(); $db->transStart(); - $coverFile = $this->request->getFile('cover'); - if ($coverFile !== null && $coverFile->isValid()) { - $cover = new Image([ - 'file_name' => $newEpisode->slug, - 'file_directory' => 'podcasts/' . $this->podcast->handle, - 'sizes' => config('Images') - ->podcastBannerSizes, - 'file' => $this->request->getFile('banner'), - 'uploaded_by' => user_id(), - 'updated_by' => user_id(), - ]); - $mediaModel = new MediaModel('image'); - if (! ($newCoverId = $mediaModel->saveMedia($cover))) { - $db->transRollback(); - return redirect() - ->back() - ->withInput() - ->with('errors', $mediaModel->errors()); - } - - $newEpisode->cover_id = $newCoverId; - } - $transcriptChoice = $this->request->getPost('transcript-choice'); - if ( - $transcriptChoice === 'upload-file' - && ($transcriptFile = $this->request->getFile('transcript_file')) - && $transcriptFile->isValid() - ) { - $transcript = new Media([ - 'file_name' => $newEpisode->slug . '-transcript', - 'file_directory' => 'podcasts/' . $this->podcast->handle, - 'file' => $transcriptFile, - 'uploaded_by' => user_id(), - 'updated_by' => user_id(), - ]); - $mediaModel = new MediaModel('image'); - if (! ($newTranscriptId = $mediaModel->saveMedia($transcript))) { - $db->transRollback(); - return redirect() - ->back() - ->withInput() - ->with('errors', $mediaModel->errors()); - } - - $newEpisode->transcript_id = $newTranscriptId; + if ($transcriptChoice === 'upload-file') { + $newEpisode->setTranscript($this->request->getFile('transcript_file')); } elseif ($transcriptChoice === 'remote-url') { $newEpisode->transcript_remote_url = $this->request->getPost( 'transcript_remote_url' - ) === '' ? null : $this->request->getPost('transcript_file_remote_url'); + ) === '' ? null : $this->request->getPost('transcript_remote_url'); } $chaptersChoice = $this->request->getPost('chapters-choice'); - if ( - $chaptersChoice === 'upload-file' - && ($chaptersFile = $this->request->getFile('chapters_file')) - && $chaptersFile->isValid() - ) { - $newEpisode->chapters_file = $chaptersFile; + if ($chaptersChoice === 'upload-file') { + $newEpisode->setChapters($this->request->getFile('chapters_file')); } elseif ($chaptersChoice === 'remote-url') { - $newEpisode->chapters_file_remote_url = $this->request->getPost( - 'chapters_file_remote_url' - ) === '' ? null : $this->request->getPost('chapters_file_remote_url'); + $newEpisode->chapters_remote_url = $this->request->getPost( + 'chapters_remote_url' + ) === '' ? null : $this->request->getPost('chapters_remote_url'); } $episodeModel = new EpisodeModel(); @@ -310,51 +261,43 @@ class EpisodeController extends BaseController $this->episode->custom_rss_string = $this->request->getPost('custom_rss'); $this->episode->updated_by = (int) user_id(); - - $audioFile = $this->request->getFile('audio_file'); - if ($audioFile !== null && $audioFile->isValid()) { - $this->episode->audio_file = $audioFile; - } - - $coverFile = $this->request->getFile('cover'); - if ($coverFile !== null && $coverFile->isValid()) { - $this->episode->cover = new Image($coverFile); - } + $this->episode->setAudio($this->request->getFile('audio_file')); + $this->episode->setCover($this->request->getFile('cover')); $transcriptChoice = $this->request->getPost('transcript-choice'); if ($transcriptChoice === 'upload-file') { $transcriptFile = $this->request->getFile('transcript_file'); if ($transcriptFile !== null && $transcriptFile->isValid()) { $this->episode->transcript_file = $transcriptFile; - $this->episode->transcript_file_remote_url = null; + $this->episode->transcript_remote_url = null; } } elseif ($transcriptChoice === 'remote-url') { if ( - ($transcriptFileRemoteUrl = $this->request->getPost('transcript_file_remote_url')) && + ($transcriptFileRemoteUrl = $this->request->getPost('transcript_remote_url')) && (($transcriptFile = $this->episode->transcript_file) !== null) ) { unlink((string) $transcriptFile); - $this->episode->transcript_file_path = null; + $this->episode->transcript->file_path = null; } - $this->episode->transcript_file_remote_url = $transcriptFileRemoteUrl === '' ? null : $transcriptFileRemoteUrl; + $this->episode->transcript_remote_url = $transcriptFileRemoteUrl === '' ? null : $transcriptFileRemoteUrl; } $chaptersChoice = $this->request->getPost('chapters-choice'); if ($chaptersChoice === 'upload-file') { $chaptersFile = $this->request->getFile('chapters_file'); if ($chaptersFile !== null && $chaptersFile->isValid()) { - $this->episode->chapters_file = $chaptersFile; - $this->episode->chapters_file_remote_url = null; + $this->episode->chapters = $chaptersFile; + $this->episode->chapters_remote_url = null; } } elseif ($chaptersChoice === 'remote-url') { if ( - ($chaptersFileRemoteUrl = $this->request->getPost('chapters_file_remote_url')) && + ($chaptersFileRemoteUrl = $this->request->getPost('chapters_remote_url')) && (($chaptersFile = $this->episode->chapters_file) !== null) ) { unlink((string) $chaptersFile); - $this->episode->chapters_file_path = null; + $this->episode->chapters->file_path = null; } - $this->episode->chapters_file_remote_url = $chaptersFileRemoteUrl === '' ? null : $chaptersFileRemoteUrl; + $this->episode->chapters_remote_url = $chaptersFileRemoteUrl === '' ? null : $chaptersFileRemoteUrl; } $db = db_connect(); @@ -396,7 +339,7 @@ class EpisodeController extends BaseController public function transcriptDelete(): RedirectResponse { unlink((string) $this->episode->transcript_file); - $this->episode->transcript_file_path = null; + $this->episode->transcript->file_path = null; $episodeModel = new EpisodeModel(); @@ -413,7 +356,7 @@ class EpisodeController extends BaseController public function chaptersDelete(): RedirectResponse { unlink((string) $this->episode->chapters_file); - $this->episode->chapters_file_path = null; + $this->episode->chapters->file_path = null; $episodeModel = new EpisodeModel(); diff --git a/modules/Admin/Controllers/PersonController.php b/modules/Admin/Controllers/PersonController.php index 7825e6f8..a2020c02 100644 --- a/modules/Admin/Controllers/PersonController.php +++ b/modules/Admin/Controllers/PersonController.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace Modules\Admin\Controllers; -use App\Entities\Image; use App\Entities\Person; use App\Models\PersonModel; use CodeIgniter\Exceptions\PageNotFoundException; @@ -78,6 +77,7 @@ class PersonController extends BaseController } $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'), @@ -85,11 +85,6 @@ class PersonController extends BaseController 'updated_by' => user_id(), ]); - $avatarFile = $this->request->getFile('avatar'); - if ($avatarFile !== null && $avatarFile->isValid()) { - $person->avatar = new Image($avatarFile); - } - $personModel = new PersonModel(); if (! $personModel->insert($person)) { diff --git a/modules/Admin/Controllers/PodcastController.php b/modules/Admin/Controllers/PodcastController.php index e3e0162e..09cf6693 100644 --- a/modules/Admin/Controllers/PodcastController.php +++ b/modules/Admin/Controllers/PodcastController.php @@ -10,7 +10,6 @@ declare(strict_types=1); namespace Modules\Admin\Controllers; -use App\Entities\Image; use App\Entities\Location; use App\Entities\Podcast; use App\Models\CategoryModel; @@ -196,6 +195,8 @@ class PodcastController extends BaseController $newPodcast = new Podcast([ 'title' => $this->request->getPost('title'), 'handle' => $this->request->getPost('handle'), + 'cover' => $this->request->getFile('cover'), + 'banner' => $this->request->getFile('banner'), 'description_markdown' => $this->request->getPost('description'), 'language_code' => $this->request->getPost('language'), 'category_id' => $this->request->getPost('category'), @@ -228,48 +229,6 @@ class PodcastController extends BaseController $db = db_connect(); $db->transStart(); - $cover = new Image([ - 'file_name' => 'cover', - 'file_directory' => 'podcasts/' . $newPodcast->handle, - 'sizes' => config('Images') - ->podcastCoverSizes, - 'file' => $this->request->getFile('cover'), - 'uploaded_by' => user_id(), - 'updated_by' => user_id(), - ]); - $mediaModel = new MediaModel('image'); - if (! ($newCoverId = $mediaModel->saveMedia($cover))) { - $db->transRollback(); - return redirect() - ->back() - ->withInput() - ->with('errors', $mediaModel->errors()); - } - $newPodcast->cover_id = $newCoverId; - - $bannerFile = $this->request->getFile('banner'); - if ($bannerFile !== null && $bannerFile->isValid()) { - $banner = new Image([ - 'file_name' => 'banner', - 'file_directory' => 'podcasts/' . $newPodcast->handle, - 'sizes' => config('Images') - ->podcastBannerSizes, - 'file' => $this->request->getFile('banner'), - 'uploaded_by' => user_id(), - 'updated_by' => user_id(), - ]); - $mediaModel = new MediaModel('image'); - if (! ($newBannerId = $mediaModel->saveMedia($banner))) { - $db->transRollback(); - return redirect() - ->back() - ->withInput() - ->with('errors', $mediaModel->errors()); - } - - $newPodcast->banner_id = $newBannerId; - } - $podcastModel = new PodcastModel(); if (! ($newPodcastId = $podcastModel->insert($newPodcast, true))) { $db->transRollback(); @@ -344,15 +303,9 @@ class PodcastController extends BaseController $this->podcast->title = $this->request->getPost('title'); $this->podcast->description_markdown = $this->request->getPost('description'); + $this->podcast->setCover($this->request->getFile('cover')); + $this->podcast->setBanner($this->request->getFile('banner')); - $coverFile = $this->request->getFile('cover'); - if ($coverFile !== null && $coverFile->isValid()) { - $this->podcast->cover->setFile($coverFile); - } - $bannerFile = $this->request->getFile('banner'); - if ($bannerFile !== null && $bannerFile->isValid()) { - $this->podcast->banner = new Image($bannerFile); - } $this->podcast->language_code = $this->request->getPost('language'); $this->podcast->category_id = $this->request->getPost('category'); $this->podcast->parental_advisory = @@ -381,6 +334,7 @@ class PodcastController extends BaseController $this->podcast->updated_by = (int) user_id(); $db = db_connect(); + $db->transStart(); $podcastModel = new PodcastModel(); @@ -400,7 +354,7 @@ class PodcastController extends BaseController $db->transComplete(); - return redirect()->route('podcast-view', [$this->podcast->id]); + return redirect()->back(); } public function deleteBanner(): RedirectResponse @@ -409,17 +363,14 @@ class PodcastController extends BaseController return redirect()->back(); } - $this->podcast->banner->delete(config('Images')->podcastBannerSizes); + $this->podcast->banner->deleteFile(); - $this->podcast->banner_path = null; - $this->podcast->banner_mimetype = null; - - $podcastModel = new PodcastModel(); - if (! $podcastModel->update($this->podcast->id, $this->podcast)) { + $mediaModel = new MediaModel(); + if (! $mediaModel->deleteMedia((int) $this->podcast->banner_id)) { return redirect() ->back() ->withInput() - ->with('errors', $podcastModel->errors()); + ->with('errors', $mediaModel->errors()); } return redirect()->back(); diff --git a/modules/Admin/Controllers/PodcastImportController.php b/modules/Admin/Controllers/PodcastImportController.php index cddb3989..a990cc0a 100644 --- a/modules/Admin/Controllers/PodcastImportController.php +++ b/modules/Admin/Controllers/PodcastImportController.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace Modules\Admin\Controllers; use App\Entities\Episode; -use App\Entities\Image; use App\Entities\Location; use App\Entities\Person; use App\Entities\Podcast; diff --git a/modules/Admin/Language/en/Episode.php b/modules/Admin/Language/en/Episode.php index 87552b95..ea510aad 100644 --- a/modules/Admin/Language/en/Episode.php +++ b/modules/Admin/Language/en/Episode.php @@ -91,12 +91,12 @@ return [ 'transcript' => 'Transcript or closed captions', 'transcript_hint' => 'Allowed formats are txt, html, srt or json.', 'transcript_file' => 'Transcript file', - 'transcript_file_remote_url' => 'Remote url for transcript', + 'transcript_remote_url' => 'Remote url for transcript', 'transcript_file_delete' => 'Delete transcript file', 'chapters' => 'Chapters', 'chapters_hint' => 'File must be in JSON Chapters format.', 'chapters_file' => 'Chapters file', - 'chapters_file_remote_url' => 'Remote url for chapters file', + 'chapters_remote_url' => 'Remote url for chapters file', 'chapters_file_delete' => 'Delete chapters file', 'advanced_section_title' => 'Advanced Parameters', 'advanced_section_subtitle' => diff --git a/modules/Admin/Language/fr/Episode.php b/modules/Admin/Language/fr/Episode.php index ad3cf971..1c5cbe44 100644 --- a/modules/Admin/Language/fr/Episode.php +++ b/modules/Admin/Language/fr/Episode.php @@ -93,13 +93,13 @@ return [ 'transcript_hint' => 'Les formats autorisés sont txt, html, srt ou json.', 'transcript_file' => 'Fichier de transcription', - 'transcript_file_remote_url' => + 'transcript_remote_url' => 'URL distante pour le fichier de transcription', 'transcript_file_delete' => 'Supprimer le fichier de transcription', 'chapters' => 'Chapitrage', 'chapters_hint' => 'Le fichier doit être en format “JSON Chapters”.', 'chapters_file' => 'Fichier de chapitrage', - 'chapters_file_remote_url' => + 'chapters_remote_url' => 'URL distante pour le fichier de chapitrage', 'chapters_file_delete' => 'Supprimer le fichier de chapitrage', 'advanced_section_title' => 'Paramètres avancés', diff --git a/modules/Analytics/Helpers/analytics_helper.php b/modules/Analytics/Helpers/analytics_helper.php index ae215d52..026e6554 100644 --- a/modules/Analytics/Helpers/analytics_helper.php +++ b/modules/Analytics/Helpers/analytics_helper.php @@ -59,8 +59,8 @@ if (! function_exists('generate_episode_analytics_url')) { $podcastId, $episodeId, // bytes_threshold: number of bytes that must be downloaded for an episode to be counted in download analytics - // - if file is shorter than 60sec, then it's audio_file_size - // - if file is longer than 60 seconds then it's audio_file_header_size + 60 seconds + // - if audio is less than or equal to 60s, then take the audio file_size + // - if audio is more than 60s, then take the audio file_header_size + 60s $audioFileDuration <= 60 ? $audioFileSize : $audioFileHeaderSize + diff --git a/phpstan.neon b/phpstan.neon index 25346598..f96db3c1 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -20,6 +20,7 @@ parameters: ignoreErrors: - '#This property type might be inlined to PHP. Do you have confidence it is correct\? Put it here#' - '#^Cognitive complexity for#' + - '#^Class cognitive complexity#' - '#Do not use chained method calls. Put each on separated lines.#' - '#Do not inherit from abstract class, better use composition#' - '#Cannot access property [\$a-z_]+ on ((array\|)?object)#' @@ -28,7 +29,7 @@ parameters: - '#Function \"preg_.*\(\)\" cannot be used/left in the code#' - '#Function "property_exists\(\)" cannot be used/left in the code#' - '#Instead of "instanceof/is_a\(\)" use ReflectionProvider service or "\(new ObjectType\(\)\)\-\>isSuperTypeOf\(\)" for static reflection to work#' - - '#^Access to an undefined property App\\Entities\\Image#' + - '#^Access to an undefined property App\\Entities\\Media\\Image#' - message: '#Function "function_exists\(\)" cannot be used/left in the code#' paths: diff --git a/themes/cp_admin/episode/create.php b/themes/cp_admin/episode/create.php index 41ac3e15..93b92c70 100644 --- a/themes/cp_admin/episode/create.php +++ b/themes/cp_admin/episode/create.php @@ -156,8 +156,8 @@
- - + +
@@ -183,8 +183,8 @@
- - + +
diff --git a/themes/cp_admin/episode/edit.php b/themes/cp_admin/episode/edit.php index d26d832e..035b8d63 100644 --- a/themes/cp_admin/episode/edit.php +++ b/themes/cp_admin/episode/edit.php @@ -153,10 +153,10 @@ ')' . hint_tooltip(lang('Episode.form.transcript_hint'), 'ml-1') ?>
- transcript_file_remote_url ? '' : 'checked' ?> /> + transcript_remote_url ? '' : 'checked' ?> /> - transcript_file_remote_url ? 'checked' : '' ?> /> + transcript_remote_url ? 'checked' : '' ?> />
@@ -164,7 +164,7 @@ transcript_file) : ?>
transcript_file_url, + $episode->transcript->file_url, icon('file', 'mr-2 text-skin-muted') . $episode->transcript_file, [ @@ -195,8 +195,8 @@
- - + +
@@ -210,10 +210,10 @@ ')' . hint_tooltip(lang('Episode.form.chapters_hint'), 'ml-1') ?>
- chapters_file_remote_url ? '' : 'checked' ?> /> + chapters_remote_url ? '' : 'checked' ?> /> - chapters_file_remote_url ? 'checked' : '' ?> /> + chapters_remote_url ? 'checked' : '' ?> />
@@ -221,7 +221,7 @@ chapters_file) : ?>
chapters_file_url, + $episode->chapters->file_url, icon('file', 'mr-2') . $episode->chapters_file, [ 'class' => 'inline-flex items-center text-xs', @@ -251,8 +251,8 @@
- - + +
diff --git a/themes/cp_admin/episode/publish.php b/themes/cp_admin/episode/publish.php index f3e99324..eacfa9db 100644 --- a/themes/cp_admin/episode/publish.php +++ b/themes/cp_admin/episode/publish.php @@ -59,7 +59,7 @@
- audio->file_url, $episode->audio->file_content_type, 'mt-auto') ?> + audio->file_url, $episode->audio->file_mimetype, 'mt-auto') ?>