Commit 2f6fdf90 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(clips): setup clip entities and model + save video clip to have it generated in the background

parent 05755918
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -51,10 +51,15 @@ class AddClips extends Migration
            'media_id' => [
                'type' => 'INT',
                'unsigned' => true,
                'null' => true,
            ],
            'metadata' => [
                'type' => 'JSON',
                'null' => true,
            ],
            'status' => [
                'type' => 'ENUM',
                'constraint' => ['queued', 'pending', 'generating', 'passed', 'failed'],
                'constraint' => ['queued', 'pending', 'running', 'passed', 'failed'],
            ],
            'logs' => [
                'type' => 'TEXT',

app/Entities/BaseEntity.php

deleted100644 → 0
+0 −11
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class BaseEntity extends Entity
{
}

app/Entities/Clip.php

deleted100644 → 0
+0 −44
Original line number Diff line number Diff line
<?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 App\Entities;

use CodeIgniter\Entity\Entity;

/**
 * @property int $id
 * @property int $podcast_id
 * @property int $episode_id
 * @property double $start_time
 * @property double $duration
 * @property string|null $label
 * @property int $created_by
 * @property int $updated_by
 */
class Clip extends Entity
{
    /**
     * @var array<string, string>
     */
    protected $casts = [
        'id' => 'integer',
        'podcast_id' => 'integer',
        'episode_id' => 'integer',
        'start_time' => 'double',
        'duration' => 'double',
        'type' => 'string',
        'label' => '?string',
        'media_id' => 'integer',
        'status' => 'string',
        'logs' => 'string',
        'created_by' => 'integer',
        'updated_by' => 'integer',
    ];
}
+119 −0
Original line number Diff line number Diff line
<?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 App\Entities\Clip;

use App\Entities\Episode;
use App\Entities\Media\Audio;
use App\Entities\Media\Video;
use App\Entities\Podcast;
use App\Models\EpisodeModel;
use App\Models\MediaModel;
use App\Models\PodcastModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;

/**
 * @property int $id
 * @property int $podcast_id
 * @property Podcast $podcast
 * @property int $episode_id
 * @property Episode $episode
 * @property string $label
 * @property double $start_time
 * @property double $end_time
 * @property double $duration
 * @property string $type
 * @property int $media_id
 * @property Video|Audio $media
 * @property string $status
 * @property string $logs
 * @property int $created_by
 * @property int $updated_by
 */
class BaseClip extends Entity
{
    /**
     * @var array<string, string>
     */
    protected $casts = [
        'id' => 'integer',
        'podcast_id' => 'integer',
        'episode_id' => 'integer',
        'label' => 'string',
        'start_time' => 'double',
        'duration' => 'double',
        'type' => 'string',
        'media_id' => '?integer',
        'metadata' => 'json-array',
        'status' => 'string',
        'logs' => 'string',
        'created_by' => 'integer',
        'updated_by' => 'integer',
    ];

    public function __construct($data)
    {
        parent::__construct($data);

        if ($this->start_time && $this->duration) {
            $this->end_time = $this->start_time + $this->duration;
        } elseif ($this->start_time && $this->end_time) {
            $this->duration = $this->end_time - $this->duration;
        }
    }

    public function getPodcast(): ?Podcast
    {
        return (new PodcastModel())->getPodcastById($this->podcast_id);
    }

    public function getEpisode(): ?Episode
    {
        return (new EpisodeModel())->getEpisodeById($this->episode_id);
    }

    public function setMedia(string $filePath = null): static
    {
        if ($filePath === null || ($file = new File($filePath)) === null) {
            return $this;
        }

        if ($this->media_id !== 0) {
            $this->getMedia()
                ->setFile($file);
            $this->getMedia()
                ->updated_by = (int) user_id();
            (new MediaModel('audio'))->updateMedia($this->getMedia());
        } else {
            $media = new Audio([
                'file_path' => $filePath,
                'language_code' => $this->getPodcast()
                    ->language_code,
                'uploaded_by' => user_id(),
                'updated_by' => user_id(),
            ]);
            $media->setFile($file);

            $this->attributes['media_id'] = (new MediaModel())->saveMedia($media);
        }

        return $this;
    }

    public function getMedia(): Audio | Video
    {
        if ($this->media_id !== null && $this->media === null) {
            $this->media = (new MediaModel($this->type))->getMediaById($this->media_id);
        }

        return $this->media;
    }
}
+16 −0
Original line number Diff line number Diff line
<?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 App\Entities\Clip;

class Soundbite extends BaseClip
{
    protected string $type = 'audio';
}
Loading