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;
|
2019-08-27 02:43:50 +08:00
|
|
|
use Illuminate\Support\Str;
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
2019-09-13 20:05:02 +08:00
|
|
|
public $table = 'files';
|
|
|
|
|
|
|
|
protected $keyType = 'string';
|
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
|
|
|
|
{
|
2019-08-27 02:43:50 +08:00
|
|
|
if (Str::startsWith($this->path, 'http')) {
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|