Let File implement the Responsable interface

which allows it being returned from a controller directly.
parent 6151aa74
......@@ -3,6 +3,7 @@
namespace Modules\Media\Entities;
use Dimsav\Translatable\Translatable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Database\Eloquent\Model;
use Modules\Core\Traits\NamespacedEntity;
use Modules\Media\Helpers\FileHelper;
......@@ -16,7 +17,7 @@ use Modules\Tag\Traits\TaggableTrait;
* @package Modules\Media\Entities
* @property \Modules\Media\ValueObjects\MediaPath path
*/
class File extends Model implements TaggableInterface
class File extends Model implements TaggableInterface, Responsable
{
use Translatable, NamespacedEntity, TaggableTrait;
/**
......@@ -43,7 +44,7 @@ class File extends Model implements TaggableInterface
'folder_id',
];
protected $appends = ['path_string', 'media_type'];
protected $casts = ['is_folder' => 'boolean', ];
protected $casts = ['is_folder' => 'boolean',];
protected static $entityNamespace = 'asgardcms/media';
public function parent_folder()
......@@ -66,7 +67,7 @@ class File extends Model implements TaggableInterface
return FileHelper::getTypeByMimetype($this->mimetype);
}
public function isFolder() : bool
public function isFolder(): bool
{
return $this->is_folder;
}
......@@ -84,4 +85,17 @@ class File extends Model implements TaggableInterface
return false;
}
/**
* Create an HTTP response that represents the object.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function toResponse($request)
{
return response()
->file(public_path($this->path->getRelativeUrl()), [
'Content-Type' => $this->mimetype,
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment