Skip to content

Commit

Permalink
Let File implement the Responsable interface
Browse files Browse the repository at this point in the history
which allows it being returned from a controller directly.
  • Loading branch information
nWidart committed Nov 28, 2017
1 parent 6151aa7 commit f9e3e87
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Modules/Media/Entities/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,7 @@
* @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;
/**
Expand All @@ -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()
Expand All @@ -66,7 +67,7 @@ public function getMediaTypeAttribute()
return FileHelper::getTypeByMimetype($this->mimetype);
}

public function isFolder() : bool
public function isFolder(): bool
{
return $this->is_folder;
}
Expand All @@ -84,4 +85,17 @@ public function getThumbnail($type)

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,
]);
}
}

0 comments on commit f9e3e87

Please sign in to comment.