*/ protected $casts = [ 'id' => 'integer', 'file_extension' => 'string', 'file_path' => 'string', 'file_size' => 'int', 'file_mimetype' => 'string', 'file_metadata' => '?json-array', 'type' => 'string', 'description' => '?string', 'language_code' => '?string', 'uploaded_by' => 'integer', 'updated_by' => 'integer', ]; /** * @param array|null $data */ public function __construct(array $data = null) { parent::__construct($data); $this->initFileProperties(); } public function initFileProperties(): void { if ($this->file_path !== '') { helper('media'); [ 'filename' => $filename, 'dirname' => $dirname, 'extension' => $extension, ] = pathinfo($this->file_path); $this->attributes['file_url'] = media_base_url($this->file_path); $this->attributes['file_name'] = $filename; $this->attributes['file_directory'] = $dirname; $this->attributes['file_extension'] = $extension; } } public function setFile(File $file): self { helper('media'); $this->attributes['type'] = $this->type; $this->attributes['file_mimetype'] = $file->getMimeType(); $this->attributes['file_metadata'] = json_encode(lstat((string) $file), JSON_INVALID_UTF8_IGNORE); $this->attributes['file_path'] = save_media( $file, $this->attributes['file_directory'], $this->attributes['file_name'] ); if ($filesize = filesize(media_path($this->file_path))) { $this->attributes['file_size'] = $filesize; } return $this; } public function deleteFile(): void { helper('media'); unlink(media_path($this->file_path)); } }