2023-03-16 14:00:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2021 Ad Aures
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Modules\Media\Entities;
|
|
|
|
|
|
|
|
use CodeIgniter\Files\File;
|
2023-08-06 14:14:05 +02:00
|
|
|
use Exception;
|
2023-03-16 14:00:05 +01:00
|
|
|
use Modules\Media\TranscriptParser;
|
|
|
|
|
|
|
|
class Transcript extends BaseMedia
|
|
|
|
{
|
|
|
|
public ?string $json_key = null;
|
|
|
|
|
|
|
|
public ?string $json_url = null;
|
|
|
|
|
|
|
|
protected string $type = 'transcript';
|
|
|
|
|
2023-08-05 12:06:49 +02:00
|
|
|
public function initFileProperties(): void
|
2023-03-16 14:00:05 +01:00
|
|
|
{
|
2023-08-05 12:06:49 +02:00
|
|
|
parent::initFileProperties();
|
2023-03-16 14:00:05 +01:00
|
|
|
|
2023-08-05 12:06:49 +02:00
|
|
|
if ($this->file_metadata !== null && array_key_exists('json_key', $this->file_metadata)) {
|
2023-03-16 14:00:05 +01:00
|
|
|
helper('media');
|
|
|
|
|
|
|
|
$this->json_key = $this->file_metadata['json_key'];
|
2023-04-13 13:45:03 +02:00
|
|
|
$this->json_url = service('file_manager')
|
2023-03-16 14:00:05 +01:00
|
|
|
->getUrl($this->json_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFile(File $file): self
|
|
|
|
{
|
|
|
|
parent::setFile($file);
|
|
|
|
|
|
|
|
$metadata = lstat((string) $file) ?? [];
|
|
|
|
|
|
|
|
helper('filesystem');
|
|
|
|
|
|
|
|
// set metadata (generated json file path)
|
2023-04-13 13:45:03 +02:00
|
|
|
$this->json_key = change_file_path($this->file_key, '', 'json');
|
2023-03-16 14:00:05 +01:00
|
|
|
$metadata['json_key'] = $this->json_key;
|
|
|
|
|
|
|
|
$this->attributes['file_metadata'] = json_encode($metadata, JSON_INVALID_UTF8_IGNORE);
|
|
|
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-08-06 14:14:05 +02:00
|
|
|
public function saveFile(): void
|
2023-03-16 14:00:05 +01:00
|
|
|
{
|
|
|
|
$this->saveJsonTranscript();
|
|
|
|
|
2023-08-06 14:14:05 +02:00
|
|
|
parent::saveFile();
|
2023-03-16 14:00:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteFile(): bool
|
|
|
|
{
|
|
|
|
if (! parent::deleteFile()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->json_key) {
|
2023-04-13 13:45:03 +02:00
|
|
|
return service('file_manager')->delete($this->json_key);
|
2023-03-16 14:00:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-06 14:14:05 +02:00
|
|
|
private function saveJsonTranscript(): void
|
2023-03-16 14:00:05 +01:00
|
|
|
{
|
2024-02-09 17:34:50 +01:00
|
|
|
$transcriptContent = file_get_contents($this->file->getRealPath());
|
2023-03-16 14:00:05 +01:00
|
|
|
|
|
|
|
$transcriptParser = new TranscriptParser();
|
|
|
|
|
2024-02-09 17:34:50 +01:00
|
|
|
if ($transcriptContent === false) {
|
2023-08-06 14:14:05 +02:00
|
|
|
throw new Exception('Could not read transcript file at ' . $this->file->getRealPath());
|
2023-03-16 14:00:05 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:34:50 +01:00
|
|
|
$transcript_format = $this->file->getExtension();
|
|
|
|
switch ($transcript_format) {
|
|
|
|
case 'vtt':
|
|
|
|
$transcriptJson = $transcriptParser->loadString($transcriptContent)
|
|
|
|
->parseVtt();
|
|
|
|
break;
|
|
|
|
case 'srt':
|
|
|
|
default:
|
|
|
|
$transcriptJson = $transcriptParser->loadString($transcriptContent)
|
|
|
|
->parseSrt();
|
|
|
|
}
|
2023-03-16 14:00:05 +01:00
|
|
|
|
|
|
|
$tempFilePath = WRITEPATH . 'uploads/' . $this->file->getRandomName();
|
|
|
|
file_put_contents($tempFilePath, $transcriptJson);
|
|
|
|
|
|
|
|
$newTranscriptJson = new File($tempFilePath, true);
|
|
|
|
|
2023-04-13 13:45:03 +02:00
|
|
|
service('file_manager')
|
2023-03-16 14:00:05 +01:00
|
|
|
->save($newTranscriptJson, $this->json_key);
|
|
|
|
}
|
|
|
|
}
|