105 lines
2.7 KiB
PHP
105 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class File extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'fileable_type',
|
|
'fileable_id',
|
|
'type',
|
|
'name',
|
|
'original_name',
|
|
'extension',
|
|
'path',
|
|
'created_by',
|
|
'updated_by',
|
|
'reason'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
'fileable_type',
|
|
'fileable_id'
|
|
];
|
|
|
|
public $appends = [
|
|
'url'
|
|
];
|
|
|
|
public static $file_directories = [
|
|
'import-temp' => 'import-temp/',
|
|
'avatar' => 'user-avatar/',
|
|
'dataDiri' => 'data-diri/',
|
|
'claim-result' => 'claim/',
|
|
'claim-diagnosis' => 'claim/',
|
|
'claim-kondisi' => 'claim/',
|
|
'claim-invoice' => 'claim/',
|
|
'final-log-result' => 'final-log/',
|
|
'final-log-diagnosis' => 'final-log/',
|
|
'final-log-kondisi' => 'final-log/',
|
|
'docs' => 'docs/',
|
|
'additional-files' => 'additional-files/',
|
|
'chat' => 'chat/',
|
|
];
|
|
|
|
public function fileable()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public static function getDirectory($type)
|
|
{
|
|
return self::$file_directories[$type] ?? 'any/';
|
|
}
|
|
|
|
public static function getFileName($type, $id)
|
|
{
|
|
return $type . '-' . $id . '-' . Str::random(10);
|
|
}
|
|
|
|
public function getNameAttribute($value)
|
|
{
|
|
return !empty($this->original_name) ? $this->original_name : ($value . '.' . $this->extension);
|
|
}
|
|
|
|
public function getUrlAttribute()
|
|
{
|
|
return url(Storage::url($this->path));
|
|
}
|
|
|
|
public static function storeFile($type, $id, $file)
|
|
{
|
|
// $fileName = self::getFileName($type, $id);
|
|
$fileName = $file->getClientOriginalName();
|
|
$directory = self::getDirectory($type);
|
|
$extension = $file->getClientOriginalExtension();
|
|
$path = $directory . $fileName . '.' . $extension;
|
|
$file->storeAs('public/' . $directory, $fileName . '.' . $extension);
|
|
return $path;
|
|
}
|
|
|
|
public static function storeFileChat($type, $id, $file)
|
|
{
|
|
// $fileName = self::getFileName($type, $id);
|
|
$fileName = $file->getClientOriginalName();
|
|
$directory = self::getDirectory($type);
|
|
$extension = $file->getClientOriginalExtension();
|
|
$path = $directory . $fileName . '.' . $extension;
|
|
$file->store('public/' .$path);
|
|
return $path;
|
|
}
|
|
}
|