Files
boursecu_server/app/Models/File.php
T
2026-06-29 20:02:20 +03:30

66 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory, Selector;
protected $fillable = [
'original_name',
'title',
'ext',
'src',
'size',
'user_id',
'type',
'has_thumb',
'url',
];
protected $casts = [
'has_thumb' => 'boolean',
];
protected $appends = [
'full_src',
];
/**
* URL کامل تصویر — همان قرارداد قبلی API (src = آدرس قابل نمایش).
*/
public function getSrcAttribute(): ?string
{
return self::publicUrl($this->attributes['src'] ?? null);
}
public function getFullSrcAttribute(): ?string
{
return $this->src;
}
/**
* ساخت URL عمومی از مسیر نسبی storage/...
* قبلاً: env('domain') + path — بدون اسلش بین دامنه و مسیر خراب می‌شد.
*/
public static function publicUrl(?string $path): ?string
{
if ($path === null || trim($path) === '') {
return null;
}
$trimmed = trim($path);
if (preg_match('/^https?:\/\//i', $trimmed)) {
return $trimmed;
}
$domain = rtrim((string) (env('domain') ?: config('app.url')), '/');
return $domain.'/'.ltrim($trimmed, '/');
}
}