mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-12 03:06:43 +02:00
feat(media): add s3 to manage media files
Users may choose between filesystem (FS) or S3 to store and manage their media files
This commit is contained in:
parent
9fc49a7430
commit
d93fc98469
85 changed files with 2169 additions and 977 deletions
104
modules/Media/Entities/Transcript.php
Normal file
104
modules/Media/Entities/Transcript.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?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;
|
||||
use Modules\Media\TranscriptParser;
|
||||
|
||||
class Transcript extends BaseMedia
|
||||
{
|
||||
public ?string $json_key = null;
|
||||
|
||||
public ?string $json_url = null;
|
||||
|
||||
protected string $type = 'transcript';
|
||||
|
||||
public function __construct(?array $data = null)
|
||||
{
|
||||
parent::__construct($data);
|
||||
|
||||
if ($this->file_key && $this->file_metadata && array_key_exists('json_key', $this->file_metadata)) {
|
||||
helper('media');
|
||||
|
||||
$this->json_key = $this->file_metadata['json_key'];
|
||||
$this->json_url = $this->fileManager
|
||||
->getUrl($this->json_key);
|
||||
}
|
||||
}
|
||||
|
||||
public function setFile(File $file): self
|
||||
{
|
||||
parent::setFile($file);
|
||||
|
||||
$metadata = lstat((string) $file) ?? [];
|
||||
|
||||
helper('filesystem');
|
||||
|
||||
$fileKeyWithoutExt = path_without_ext($this->file_key);
|
||||
|
||||
$jsonfileKey = $fileKeyWithoutExt . '.json';
|
||||
|
||||
// set metadata (generated json file path)
|
||||
$this->json_key = $jsonfileKey;
|
||||
$metadata['json_key'] = $this->json_key;
|
||||
|
||||
$this->attributes['file_metadata'] = json_encode($metadata, JSON_INVALID_UTF8_IGNORE);
|
||||
|
||||
$this->file = $file;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function saveFile(): bool
|
||||
{
|
||||
$this->saveJsonTranscript();
|
||||
|
||||
return parent::saveFile();
|
||||
}
|
||||
|
||||
public function deleteFile(): bool
|
||||
{
|
||||
if (! parent::deleteFile()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->json_key) {
|
||||
return $this->fileManager->delete($this->json_key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function saveJsonTranscript(): bool
|
||||
{
|
||||
$srtContent = file_get_contents($this->file->getRealPath());
|
||||
|
||||
$transcriptParser = new TranscriptParser();
|
||||
|
||||
if ($srtContent === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $transcriptJson = $transcriptParser->loadString($srtContent)->parseSrt()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tempFilePath = WRITEPATH . 'uploads/' . $this->file->getRandomName();
|
||||
file_put_contents($tempFilePath, $transcriptJson);
|
||||
|
||||
$newTranscriptJson = new File($tempFilePath, true);
|
||||
|
||||
$this->fileManager
|
||||
->save($newTranscriptJson, $this->json_key);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue