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'; protected string $type = 'audio';
/** public function initFileProperties(): void
* @param array<string, mixed>|null $data
*/
public function __construct(array $data = null)
{ {
parent::__construct($data); parent::initFileProperties();
if ($this->file_metadata) { if ($this->file_metadata !== null) {
$this->duration = (float) $this->file_metadata['playtime_seconds']; $this->duration = (float) $this->file_metadata['playtime_seconds'];
$this->header_size = (int) $this->file_metadata['avdataoffset']; $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(); $this->initFileProperties();
return $this;
} }
public function initFileProperties(): void public function initFileProperties(): void
{ {
if ($this->file_key !== '') { $pathInfo = pathinfo($this->file_key) + [
[ 'filename' => '',
'filename' => $filename, 'dirname' => '',
'dirname' => $dirname, 'extension' => '',
'extension' => $extension, ];
] = pathinfo($this->file_key);
$this->attributes['file_url'] = service('file_manager')->getUrl($this->file_key); $this->file_url = service('file_manager')
$this->attributes['file_name'] = $filename; ->getUrl($this->file_key);
$this->attributes['file_directory'] = $dirname; $this->file_name = $pathInfo['filename'];
$this->attributes['file_extension'] = $extension; $this->file_directory = $pathInfo['dirname'];
} $this->file_extension = $pathInfo['extension'];
} }
public function setFile(File $file): self public function setFile(File $file): self

View File

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

View File

@ -21,11 +21,11 @@ class Transcript extends BaseMedia
protected string $type = 'transcript'; 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'); helper('media');
$this->json_key = $this->file_metadata['json_key']; $this->json_key = $this->file_metadata['json_key'];

View File

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

View File

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