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;
|
2022-03-14 23:45:18 +08:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2018-04-02 07:02:12 +08:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2020-08-12 05:48:51 +08:00
|
|
|
* @property string $name
|
|
|
|
* @property string $description
|
|
|
|
* @property string $disk
|
|
|
|
* @property string $path
|
|
|
|
* @property bool $public
|
|
|
|
* @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';
|
2022-03-14 23:45:18 +08:00
|
|
|
|
2018-04-03 01:55:37 +08:00
|
|
|
public $incrementing = false;
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
protected $fillable = [
|
2022-03-14 23:45:18 +08:00
|
|
|
'id',
|
2018-04-02 07:02:12 +08:00
|
|
|
'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
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-04-03 01:55:37 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function extension(): Attribute
|
2018-04-03 01:55:37 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
get: function ($_, $attrs) {
|
|
|
|
if (!$this->pathinfo) {
|
|
|
|
$this->pathinfo = pathinfo($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->pathinfo['extension'];
|
|
|
|
}
|
|
|
|
);
|
2018-04-03 01:55:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get just the filename
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-04-03 01:55:37 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function filename(): Attribute
|
2018-04-03 01:55:37 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
get: function ($_, $attrs) {
|
|
|
|
if (!$this->pathinfo) {
|
|
|
|
$this->pathinfo = pathinfo($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->pathinfo['filename'].'.'.$this->pathinfo['extension'];
|
|
|
|
}
|
|
|
|
);
|
2018-04-03 01:55:37 +08:00
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:12 +08:00
|
|
|
/**
|
|
|
|
* Get the full URL to this attribute
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @return Attribute
|
2018-04-02 07:02:12 +08:00
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function url(): Attribute
|
2018-04-02 07:02:12 +08:00
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
return Attribute::make(
|
|
|
|
get: function ($_, $attrs) {
|
|
|
|
if (Str::startsWith($this->path, 'http')) {
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$disk = $this->disk ?? config('filesystems.public_files');
|
|
|
|
|
|
|
|
// If the disk isn't stored in public (S3 or something),
|
|
|
|
// just pass through the URL call
|
|
|
|
if ($disk !== 'public') {
|
|
|
|
return Storage::disk(config('filesystems.public_files'))
|
|
|
|
->url($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|