Files
boursecu_server/app/Models/File.php
T

119 lines
3.3 KiB
PHP
Raw Normal View History

2026-02-16 09:29:37 +00:00
<?php
namespace App\Models;
2026-06-29 20:07:03 +03:30
use App\Models\Setting;
2026-02-16 09:29:37 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
2026-06-29 20:02:20 +03:30
use HasFactory, Selector;
2026-06-29 19:58:06 +03:30
2026-02-16 09:29:37 +00:00
protected $fillable = [
'original_name',
'title',
'ext',
'src',
'size',
'user_id',
'type',
'has_thumb',
'url',
];
2026-06-29 19:58:06 +03:30
2026-02-16 09:29:37 +00:00
protected $casts = [
2026-06-29 19:58:06 +03:30
'has_thumb' => 'boolean',
2026-02-16 09:29:37 +00:00
];
2026-06-29 19:58:06 +03:30
protected $appends = [
'full_src',
];
/**
2026-06-29 20:02:20 +03:30
* URL کامل تصویر — همان قرارداد قبلی API (src = آدرس قابل نمایش).
2026-06-29 19:58:06 +03:30
*/
public function getSrcAttribute(): ?string
{
return self::publicUrl($this->attributes['src'] ?? null);
}
2026-06-29 20:02:20 +03:30
public function getFullSrcAttribute(): ?string
{
return $this->src;
}
2026-06-29 19:58:06 +03:30
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;
}
2026-06-29 20:07:03 +03:30
$base = self::resolvePublicBaseUrl();
if ($base === '') {
return null;
}
2026-06-29 20:02:20 +03:30
2026-06-29 20:07:03 +03:30
$relative = ltrim($trimmed, '/');
if (! str_starts_with($relative, 'storage/')) {
$relative = 'storage/'.$relative;
}
return $base.'/'.$relative;
}
/**
* پایهٔ URL فایل‌های storage — cache-safe و مستقل از env() خام.
*/
public static function resolvePublicBaseUrl(): string
{
$domain = config('app.domain');
if (is_string($domain) && $domain !== '' && ! str_contains($domain, 'localhost')) {
return rtrim($domain, '/');
}
$appUrl = config('app.url');
if (is_string($appUrl) && $appUrl !== '' && ! str_contains($appUrl, 'localhost') && str_contains($appUrl, '/server')) {
return rtrim($appUrl, '/');
}
try {
$rows = Setting::query()
->whereIn('key', ['bace_url', 'server_url', 'admin_url', 'app_url'])
->where('is_active', 1)
->pluck('value', 'key');
foreach (['bace_url', 'server_url'] as $key) {
$value = trim((string) ($rows[$key] ?? ''));
if ($value !== '' && preg_match('/^https?:\/\//i', $value)) {
return rtrim($value, '/');
}
}
$adminUrl = trim((string) ($rows['admin_url'] ?? ''));
if ($adminUrl !== '' && preg_match('#/admin#i', $adminUrl)) {
return rtrim(preg_replace('#/admin/?$#i', '/server', rtrim($adminUrl, '/')), '/');
}
$siteUrl = trim((string) ($rows['app_url'] ?? ''));
if ($siteUrl !== '' && preg_match('/^https?:\/\//i', $siteUrl)) {
return rtrim($siteUrl, '/').'/server';
}
} catch (\Throwable) {
// DB unavailable during deploy/migrate
}
if (is_string($appUrl) && $appUrl !== '' && ! str_contains($appUrl, 'localhost')) {
return rtrim($appUrl, '/');
}
return '';
2026-02-16 09:29:37 +00:00
}
}