Commit 00987610 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat(transcript): parse srt subtitles into json file + add max file size info...

feat(transcript): parse srt subtitles into json file + add max file size info below audio file input

remove episode form warning + add javascript validation when uploading a file to check if it's too
big to upload
parent 16705584
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ performance improvements ⚡.
### Where can I find my _Castopod Host_ version?

Go to your _Castopod Host_ admin panel, the version is displayed on the bottom
right corner.
left corner.

Alternatively, you can find the version in the `app > Config > Constants.php`
file.
+2 −2
Original line number Diff line number Diff line
@@ -275,7 +275,7 @@ class Episode extends Entity
            ]);
            $transcript->setFile($file);

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

        return $this;
@@ -313,7 +313,7 @@ class Episode extends Entity
            ]);
            $chapters->setFile($file);

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

        return $this;
+0 −2
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ 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|null $file_metadata
@@ -80,7 +79,6 @@ 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}";
        }
    }

+58 −0
Original line number Diff line number Diff line
@@ -10,7 +10,65 @@ declare(strict_types=1);

namespace App\Entities\Media;

use App\Libraries\TranscriptParser;
use CodeIgniter\Files\File;

class Transcript extends BaseMedia
{
    protected string $type = 'transcript';

    protected ?string $json_path = null;

    protected ?string $json_url = null;

    public function initFileProperties(): void
    {
        parent::initFileProperties();

        if ($this->file_path && $this->file_metadata && array_key_exists('json_path', $this->file_metadata)) {
            helper('media');

            $this->json_path = media_path($this->file_metadata['json_path']);
            $this->json_url = media_base_url($this->file_metadata['json_path']);
        }
    }

    public function setFile(File $file): self
    {
        parent::setFile($file);

        $content = file_get_contents(media_path($this->attributes['file_path']));

        if ($content === false) {
            return $this;
        }

        $metadata = [];
        if ($fileMetadata = lstat((string) $file)) {
            $metadata = $fileMetadata;
        }

        $transcriptParser = new TranscriptParser();
        $jsonFilePath = $this->attributes['file_directory'] . '/' . $this->attributes['file_name'] . '.json';
        if (($transcriptJson = $transcriptParser->loadString($content)->parseSrt()) && file_put_contents(
            media_path($jsonFilePath),
            $transcriptJson
        )) {
            // set metadata (generated json file path)
            $metadata['json_path'] = $jsonFilePath;
        }

        $this->attributes['file_metadata'] = json_encode($metadata);

        return $this;
    }

    public function deleteFile(): void
    {
        parent::deleteFile();

        if ($this->json_path) {
            unlink($this->json_path);
        }
    }
}
+61 −0
Original line number Diff line number Diff line
@@ -206,3 +206,64 @@ if (! function_exists('podcast_uuid')) {
}

//--------------------------------------------------------------------


if (! function_exists('file_upload_max_size')) {

    /**
     * Returns a file size limit in bytes based on the PHP upload_max_filesize and post_max_size Adapted from:
     * https://stackoverflow.com/a/25370978
     */
    function file_upload_max_size(): float
    {
        static $max_size = -1;

        if ($max_size < 0) {
            // Start with post_max_size.
            $post_max_size = parse_size((string) ini_get('post_max_size'));
            if ($post_max_size > 0) {
                $max_size = $post_max_size;
            }

            // If upload_max_size is less, then reduce. Except if upload_max_size is
            // zero, which indicates no limit.
            $upload_max = parse_size((string) ini_get('upload_max_filesize'));
            if ($upload_max > 0 && $upload_max < $max_size) {
                $max_size = $upload_max;
            }
        }
        return $max_size;
    }
}

if (! function_exists('parse_size')) {
    function parse_size(string $size): float
    {
        $unit = (string) preg_replace('~[^bkmgtpezy]~i', '', $size); // Remove the non-unit characters from the size.
        $size = (float) preg_replace('~[^0-9\.]~', '', $size); // Remove the non-numeric characters from the size.
        if ($unit !== '') {
            // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
            return round($size * pow(1024, (float) stripos('bkmgtpezy', $unit[0])));
        }

        return round($size);
    }
}

if (! function_exists('format_bytes')) {
    /**
     * Adapted from https://stackoverflow.com/a/2510459
     */
    function formatBytes(float $bytes, int $precision = 2): string
    {
        $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];

        $bytes = max($bytes, 0);
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
        $pow = min($pow, count($units) - 1);

        $bytes /= pow(1024, $pow);

        return round($bytes, $precision) . $units[$pow];
    }
}
Loading