2018-04-02 07:02:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Interfaces\Model;
|
|
|
|
use App\Models\Traits\ReferenceTrait;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File property
|
|
|
|
* @property string $name
|
|
|
|
* @property string $description
|
2018-04-03 00:04:32 +08:00
|
|
|
* @property string $disk
|
2018-04-02 07:02:12 +08:00
|
|
|
* @property string $path
|
|
|
|
* @property boolean $public
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
|
|
|
class File extends Model
|
|
|
|
{
|
|
|
|
use ReferenceTrait;
|
|
|
|
|
|
|
|
protected $table = 'files';
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'description',
|
2018-04-03 00:04:32 +08:00
|
|
|
'disk',
|
2018-04-02 07:02:12 +08:00
|
|
|
'path',
|
|
|
|
'public',
|
|
|
|
'ref_model',
|
|
|
|
'ref_model_id',
|
|
|
|
];
|
|
|
|
|
|
|
|
public static $rules = [
|
|
|
|
'name' => 'required',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full URL to this attribute
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUrlAttribute(): string
|
|
|
|
{
|
2018-04-03 00:04:32 +08:00
|
|
|
$disk = $this->disk ?? config('filesystems.public_files');
|
|
|
|
|
|
|
|
// If the disk isn't stored in public (S3 or something),
|
|
|
|
// just pass through the URL call
|
2018-04-02 07:02:12 +08:00
|
|
|
if ($disk !== 'public') {
|
|
|
|
return Storage::disk(config('filesystems.public_files'))
|
|
|
|
->url($this->path);
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:04:32 +08:00
|
|
|
// Otherwise, figure out the public URL and save there
|
|
|
|
return public_asset(Storage::disk('public')->url($this->path));
|
2018-04-02 07:02:12 +08:00
|
|
|
}
|
|
|
|
}
|