fix(media): init file properties in setAttributes' Model method + set defaults to pathinfo data

This commit is contained in:
Yassine Doghri 2023-08-05 10:06:49 +00:00
parent 26a714d9c2
commit 0775add678
6 changed files with 27 additions and 35 deletions

View File

@ -21,14 +21,11 @@ class Audio extends BaseMedia
{
protected string $type = 'audio';
/**
* @param array<string, mixed>|null $data
*/
public function __construct(array $data = null)
public function initFileProperties(): void
{
parent::__construct($data);
parent::initFileProperties();
if ($this->file_metadata) {
if ($this->file_metadata !== null) {
$this->duration = (float) $this->file_metadata['playtime_seconds'];
$this->header_size = (int) $this->file_metadata['avdataoffset'];
}

View File

@ -56,29 +56,30 @@ class BaseMedia extends Entity
];
/**
* @param array<string, mixed>|null $data
* @param array<string, mixed> $data
*/
public function __construct(array $data = null)
public function setAttributes(array $data): self
{
parent::__construct($data);
parent::setAttributes($data);
$this->initFileProperties();
return $this;
}
public function initFileProperties(): void
{
if ($this->file_key !== '') {
[
'filename' => $filename,
'dirname' => $dirname,
'extension' => $extension,
] = pathinfo($this->file_key);
$pathInfo = pathinfo($this->file_key) + [
'filename' => '',
'dirname' => '',
'extension' => '',
];
$this->attributes['file_url'] = service('file_manager')->getUrl($this->file_key);
$this->attributes['file_name'] = $filename;
$this->attributes['file_directory'] = $dirname;
$this->attributes['file_extension'] = $extension;
}
$this->file_url = service('file_manager')
->getUrl($this->file_key);
$this->file_name = $pathInfo['filename'];
$this->file_directory = $pathInfo['dirname'];
$this->file_extension = $pathInfo['extension'];
}
public function setFile(File $file): self

View File

@ -29,10 +29,7 @@ class Image extends BaseMedia
{
parent::initFileProperties();
if ($this->file_key !== '' && $this->file_metadata !== null && array_key_exists(
'sizes',
$this->file_metadata
)) {
if ($this->file_metadata && array_key_exists('sizes', $this->file_metadata)) {
$this->sizes = $this->file_metadata['sizes'];
$this->initSizeProperties();
}

View File

@ -21,11 +21,11 @@ class Transcript extends BaseMedia
protected string $type = 'transcript';
public function __construct(?array $data = null)
public function initFileProperties(): void
{
parent::__construct($data);
parent::initFileProperties();
if ($this->file_key && $this->file_metadata && array_key_exists('json_key', $this->file_metadata)) {
if ($this->file_metadata !== null && array_key_exists('json_key', $this->file_metadata)) {
helper('media');
$this->json_key = $this->file_metadata['json_key'];

View File

@ -102,14 +102,7 @@ class MediaModel extends Model
{
$cacheName = "media#{$mediaId}";
if (! ($found = cache($cacheName))) {
$builder = $this->where([
'id' => $mediaId,
]);
/** @var object $result */
$result = $builder->first();
$mediaClass = $this->returnType;
$found = new $mediaClass($result->toArray(false, true));
$found = $this->find($mediaId);
cache()
->save($cacheName, $found, DECADE);

View File

@ -168,6 +168,10 @@ class PodcastImport extends BaseCommand
$this->importTask->pass();
} catch (Exception $exception) {
$this->error($exception->getMessage());
log_message(
'critical',
'Error when importing ' . $this->importTask->feed_url . PHP_EOL . $exception->getTraceAsString()
);
}
}