77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Juil;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
|
|
||
|
|
class PmisArchiveFile extends Model
|
||
|
|
{
|
||
|
|
use SoftDeletes;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'tenant_id',
|
||
|
|
'folder_id',
|
||
|
|
'title',
|
||
|
|
'original_name',
|
||
|
|
'file_path',
|
||
|
|
'file_type',
|
||
|
|
'file_size',
|
||
|
|
'site_name',
|
||
|
|
'registered_by',
|
||
|
|
'options',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'options' => 'array',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function scopeTenant($query, $tenantId)
|
||
|
|
{
|
||
|
|
return $query->where('tenant_id', $tenantId);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function folder(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(PmisArchiveFolder::class, 'folder_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function registeredByUser(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class, 'registered_by');
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function detectFileType(string $extension): string
|
||
|
|
{
|
||
|
|
$ext = strtolower($extension);
|
||
|
|
$imageExts = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
|
||
|
|
$videoExts = ['mp4', 'avi', 'mov', 'wmv', 'flv', 'mkv', 'webm'];
|
||
|
|
|
||
|
|
if (in_array($ext, $imageExts)) {
|
||
|
|
return '사진';
|
||
|
|
}
|
||
|
|
if (in_array($ext, $videoExts)) {
|
||
|
|
return '동영상';
|
||
|
|
}
|
||
|
|
|
||
|
|
return '문서';
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function formatSize(int $bytes): string
|
||
|
|
{
|
||
|
|
if ($bytes >= 1073741824) {
|
||
|
|
return round($bytes / 1073741824, 1).'GB';
|
||
|
|
}
|
||
|
|
if ($bytes >= 1048576) {
|
||
|
|
return round($bytes / 1048576, 1).'MB';
|
||
|
|
}
|
||
|
|
if ($bytes >= 1024) {
|
||
|
|
return round($bytes / 1024, 1).'KB';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $bytes.'B';
|
||
|
|
}
|
||
|
|
}
|