fix pictures 3
This commit is contained in:
+59
-6
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -42,10 +43,6 @@ class File extends Model
|
||||
return $this->src;
|
||||
}
|
||||
|
||||
/**
|
||||
* ساخت URL عمومی از مسیر نسبی storage/...
|
||||
* قبلاً: env('domain') + path — بدون اسلش بین دامنه و مسیر خراب میشد.
|
||||
*/
|
||||
public static function publicUrl(?string $path): ?string
|
||||
{
|
||||
if ($path === null || trim($path) === '') {
|
||||
@@ -58,8 +55,64 @@ class File extends Model
|
||||
return $trimmed;
|
||||
}
|
||||
|
||||
$domain = rtrim((string) (env('domain') ?: config('app.url')), '/');
|
||||
$base = self::resolvePublicBaseUrl();
|
||||
if ($base === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $domain.'/'.ltrim($trimmed, '/');
|
||||
$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 '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,9 @@ return [
|
||||
|
||||
'url' => env('APP_URL', 'http://localhost'),
|
||||
|
||||
/** پایهٔ URL فایلهای public — معمولاً https://example.ir/server */
|
||||
'domain' => env('domain'),
|
||||
|
||||
'asset_url' => env('ASSET_URL', null),
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\File;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FilePublicUrlTest extends TestCase
|
||||
{
|
||||
public function test_public_url_uses_config_domain_with_storage_path(): void
|
||||
{
|
||||
Config::set('app.domain', 'https://garoosigholaktala.ir/server');
|
||||
|
||||
$url = File::publicUrl('storage/1405/02/27/sample.jpg');
|
||||
|
||||
$this->assertSame(
|
||||
'https://garoosigholaktala.ir/server/storage/1405/02/27/sample.jpg',
|
||||
$url
|
||||
);
|
||||
}
|
||||
|
||||
public function test_public_url_derives_server_base_from_admin_url_setting(): void
|
||||
{
|
||||
Config::set('app.domain', null);
|
||||
Config::set('app.url', 'http://localhost');
|
||||
|
||||
\App\Models\Setting::query()->updateOrCreate(
|
||||
['key' => 'admin_url'],
|
||||
['value' => 'https://garoosigholaktala.ir/admin/', 'is_active' => 1, 'title_fa' => 'admin']
|
||||
);
|
||||
|
||||
$base = File::resolvePublicBaseUrl();
|
||||
|
||||
$this->assertSame('https://garoosigholaktala.ir/server', $base);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user