File.php 1.03 KB
<?php

namespace Yjtec\Upload\Models;

use Illuminate\Database\Eloquent\Model;

class File extends Model
{
    //
    protected $table    = "file_system_v2";
    protected $fillable = [
        'filename', 'mimetype', 'filesize', 'extension', 'path', 'url', 'foreign_key', 'type',
    ];

    public function getUrlAttribute()
    {
        $path = $this->attributes['path'];
        if (config('filesystems.oss_host', '') == '') {
            return \Storage::url($path);
        } else {
            return config('filesystems.oss_host', '') . $path;
        }
    }
    public function setFileAttribute($file)
    {
        $this->attributes['filename']  = $file->getClientOriginalName();
        $this->attributes['mimetype']  = $file->getClientMimeType();
        $this->attributes['filesize']  = $file->getClientSize();
        $this->attributes['extension'] = $file->guessClientExtension();
    }

    public function setPathAttribute($path)
    {
        $this->attributes['key'] = md5($path);
        $this->attributes['path'] = $path;
    }
}