Fix blog post image upload storage
This commit is contained in:
@@ -121,9 +121,10 @@ class PostResource extends Resource
|
|||||||
->unique(BlogPost::class, 'slug', ignoreRecord: true)
|
->unique(BlogPost::class, 'slug', ignoreRecord: true)
|
||||||
->maxLength(255)
|
->maxLength(255)
|
||||||
->columnSpanFull(),
|
->columnSpanFull(),
|
||||||
FileUpload::make('featured_image')
|
FileUpload::make('banner')
|
||||||
->label('Featured Image')
|
->label('Featured Image')
|
||||||
->image()
|
->image()
|
||||||
|
->disk('public')
|
||||||
->directory('blog')
|
->directory('blog')
|
||||||
->visibility('public'),
|
->visibility('public'),
|
||||||
Select::make('blog_category_id')
|
Select::make('blog_category_id')
|
||||||
|
|||||||
@@ -63,12 +63,11 @@ class BlogPost extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
$path = ltrim($this->banner, '/');
|
$path = ltrim($this->banner, '/');
|
||||||
|
if (str_starts_with($path, 'storage/')) {
|
||||||
|
$path = substr($path, strlen('storage/'));
|
||||||
|
}
|
||||||
|
|
||||||
return \URL::temporarySignedRoute(
|
return \Storage::disk('public')->url($path);
|
||||||
'api.v1.branding.asset',
|
|
||||||
now()->addMinutes(30),
|
|
||||||
['path' => $path]
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
tests/Unit/BlogPostBannerUrlTest.php
Normal file
36
tests/Unit/BlogPostBannerUrlTest.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Models\BlogPost;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class BlogPostBannerUrlTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_banner_url_uses_public_storage_url_for_blog_paths(): void
|
||||||
|
{
|
||||||
|
config(['app.url' => 'https://example.test']);
|
||||||
|
|
||||||
|
$post = new BlogPost;
|
||||||
|
$post->banner = 'blog/test.jpg';
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
Storage::disk('public')->url('blog/test.jpg'),
|
||||||
|
$post->banner_url
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_banner_url_strips_storage_prefix(): void
|
||||||
|
{
|
||||||
|
config(['app.url' => 'https://example.test']);
|
||||||
|
|
||||||
|
$post = new BlogPost;
|
||||||
|
$post->banner = 'storage/blog/test.jpg';
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
Storage::disk('public')->url('blog/test.jpg'),
|
||||||
|
$post->banner_url
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user