2018-04-02 07:02:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Model;
|
2018-04-03 01:55:37 +08:00
|
|
|
use App\Models\Traits\HashIdTrait;
|
2018-04-02 07:02:12 +08:00
|
|
|
use App\Models\Traits\ReferenceTrait;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2018-08-27 00:40:04 +08:00
|
|
|
* @property bool $public
|
2018-04-03 01:55:37 +08:00
|
|
|
* @property int $download_count
|
|
|
|
* @property string $url
|
|
|
|
* @property string $filename
|
2018-04-02 07:02:12 +08:00
|
|
|
*/
|
|
|
|
class File extends Model
|
|
|
|
{
|
2018-04-03 01:55:37 +08:00
|
|
|
use HashIdTrait;
|
2018-04-02 07:02:12 +08:00
|
|
|
use ReferenceTrait;
|
|
|
|
|
|
|
|
protected $table = 'files';
|
2018-04-03 01:55:37 +08:00
|
|
|
public $incrementing = false;
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2018-04-03 01:55:37 +08:00
|
|
|
protected $casts = [
|
|
|
|
'public' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2018-04-02 07:02:12 +08:00
|
|
|
public static $rules = [
|
2018-08-27 00:40:04 +08:00
|
|
|
'name' => 'required',
|
2018-04-02 07:02:12 +08:00
|
|
|
];
|
|
|
|
|
2018-04-03 01:55:37 +08:00
|
|
|
private $pathinfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the file extension
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 01:55:37 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getExtensionAttribute(): string
|
|
|
|
{
|
|
|
|
if (!$this->pathinfo) {
|
|
|
|
$this->pathinfo = pathinfo($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->pathinfo['extension'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get just the filename
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 01:55:37 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFilenameAttribute() :string
|
|
|
|
{
|
|
|
|
if (!$this->pathinfo) {
|
|
|
|
$this->pathinfo = pathinfo($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->pathinfo['filename'].'.'.$this->pathinfo['extension'];
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:12 +08:00
|
|
|
/**
|
|
|
|
* Get the full URL to this attribute
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-02 07:02:12 +08:00
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|