Skip to content

Commit 05c5905

Browse files
committed
asset controller + fix bugs + extend resources + fix package service provider
1 parent 452c63b commit 05c5905

8 files changed

+178
-10
lines changed

config/filament-flexible-blocks-asset-manager.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@
22

33
// config for Statik/FilamentFlexibleBlocksAssetManager
44
return [
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Translatable assets
8+
|--------------------------------------------------------------------------
9+
|
10+
| Allows to translate the assets in different languages.
11+
*/
512
'translatable_assets' => true,
613

14+
/*
15+
|--------------------------------------------------------------------------
16+
| Navigation group
17+
|--------------------------------------------------------------------------
18+
|
19+
| Sets the navigation group label to which the assets table is added.
20+
*/
721
'navigation_group' => null,
822

923
/*
@@ -43,10 +57,46 @@
4357
//'application/pdf'
4458
],
4559

60+
/*
61+
|--------------------------------------------------------------------------
62+
| Storage disk
63+
|--------------------------------------------------------------------------
64+
|
65+
| The file system disk in which the assets will be stored.
66+
*/
4667
'storage_disk' => null,
4768

69+
/*
70+
|--------------------------------------------------------------------------
71+
| Storage directory
72+
|--------------------------------------------------------------------------
73+
|
74+
| The directory on the file system disk in which the assets will be stored.
75+
*/
4876
'storage_directory' => null,
4977

78+
/*
79+
|--------------------------------------------------------------------------
80+
| Storage visibility
81+
|--------------------------------------------------------------------------
82+
|
83+
| The visibility of the assets, see Spatie Medialibrary docs.
84+
*/
5085
'storage_visibility' => 'public',
5186

87+
/*
88+
|--------------------------------------------------------------------------
89+
| Asset authorisation
90+
|--------------------------------------------------------------------------
91+
|
92+
| The assets URLs can be protected. This can be done in different ways:
93+
| 1. a gate, see https://laravel.com/docs/11.x/authorization#gates
94+
| You can define a gate that takes the asset record as argument.
95+
| 2. a policy, see https://laravel.com/docs/11.x/authorization#writing-policies
96+
| You can write a policy for the Asset model. The `view` policy will be used to authorise.
97+
*/
98+
'asset_authorisation' => [
99+
//'gate' => 'asset-access',
100+
//'policy' => AssetPolicy::class,
101+
]
52102
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use Statikbe\FilamentFlexibleBlocksAssetManager\Http\Controllers\AssetController;
5+
6+
7+
Route::get('/asset/{asset}/{locale?}', [AssetController::class, 'index'])
8+
->middleware(['web'])
9+
->name('filament-flexible-blocks-asset-manager.asset_index');

src/Filament/Form/Fields/AssetMediaField.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
66
use Illuminate\Database\Eloquent\Model;
7+
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
78
use Statikbe\FilamentFlexibleContentBlocks\Filament\Form\Fields\ImageField;
89
use Filament\Support\Assets\Asset;
910

@@ -14,18 +15,18 @@ class AssetMediaField extends ImageField
1415
public static function create(bool $translatable = false): SpatieMediaLibraryFileUpload
1516
{
1617
$field = self::FIELD;
17-
$component = static::createImageField($field, $translatable, config('filament-flexible-blocks-asset-manager.image_editor', null))
18+
$component = static::createImageField($field, $translatable, FilamentFlexibleBlocksAssetManagerConfig::getImageEditor())
1819
->label(trans("filament-flexible-blocks-asset-manager::filament-flexible-blocks-asset-manager.form_component.{$field}_lbl"))
19-
->disk(config('filament-flexible-blocks-asset-manager.storage_disk'))
20-
->directory(config('filament-flexible-blocks-asset-manager.storage_directory'))
21-
->visibility(config('filament-flexible-blocks-asset-manager.storage_visibility') ?? 'public')
20+
->disk(FilamentFlexibleBlocksAssetManagerConfig::getStorageDisk())
21+
->directory(FilamentFlexibleBlocksAssetManagerConfig::getStorageDirectory())
22+
->visibility(FilamentFlexibleBlocksAssetManagerConfig::getStorageVisibility())
2223
->collection(function (Model $record) {
2324
/** @var Asset $record */
2425
return $record->getAssetCollection();
2526
})
2627
->conversion('thumbnail');
2728

28-
$acceptedFileTypes = config('filament-flexible-blocks-asset-manager.accepted_file_types') ?? [];
29+
$acceptedFileTypes = FilamentFlexibleBlocksAssetManagerConfig::getAcceptedFileTypes();
2930
if(!empty($acceptedFileTypes)){
3031
$component->acceptedFileTypes($acceptedFileTypes);
3132
}

src/Filament/Resources/AssetResource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Statikbe\FilamentFlexibleBlocksAssetManager\Filament\Resources\AssetResource\Pages\CreateAsset;
1515
use Statikbe\FilamentFlexibleBlocksAssetManager\Filament\Resources\AssetResource\Pages\EditAsset;
1616
use Statikbe\FilamentFlexibleBlocksAssetManager\Filament\Resources\AssetResource\Pages\ListAssets;
17+
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
1718
use Statikbe\FilamentFlexibleBlocksAssetManager\Models\Asset;
1819

1920
class AssetResource extends Resource
@@ -41,7 +42,7 @@ public static function getPluralLabel(): string
4142

4243
public static function getNavigationGroup(): ?string
4344
{
44-
return config('filament-flexible-blocks-asset-manager.navigation_group');
45+
return FilamentFlexibleBlocksAssetManagerConfig::getNavigationGroup();
4546
}
4647

4748
public static function form(Form $form): Form
@@ -52,7 +53,7 @@ public static function form(Form $form): Form
5253
->columns(2)
5354
->schema([
5455
AssetNameField::create(true),
55-
AssetMediaField::create(config('filament-flexible-blocks-asset-manager.translatable_assets', false)),
56+
AssetMediaField::create(FilamentFlexibleBlocksAssetManagerConfig::hasTranslatableAssets()),
5657
]),
5758
]);
5859
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Statikbe\FilamentFlexibleBlocksAssetManager;
4+
5+
class FilamentFlexibleBlocksAssetManagerConfig
6+
{
7+
public static function hasTranslatableAssets(): bool
8+
{
9+
return config('filament-flexible-blocks-asset-manager.translatable_assets', false);
10+
}
11+
12+
public static function getAssetAuthorisationGate(): ?string
13+
{
14+
return config('filament-flexible-blocks-asset-manager.asset_authorisation.gate');
15+
}
16+
17+
public static function getAssetAuthorisationPolicy(): ?string
18+
{
19+
return config('filament-flexible-blocks-asset-manager.asset_authorisation.policy');
20+
}
21+
22+
public static function getStorageDisk(): ?string
23+
{
24+
return config('filament-flexible-blocks-asset-manager.storage_disk');
25+
}
26+
27+
public static function getStorageDirectory(): ?string
28+
{
29+
return config('filament-flexible-blocks-asset-manager.storage_directory');
30+
}
31+
32+
public static function getStorageVisibility(): ?string
33+
{
34+
return config('filament-flexible-blocks-asset-manager.storage_visibility') ?? 'public';
35+
}
36+
37+
public static function getAcceptedFileTypes(): array
38+
{
39+
return config('filament-flexible-blocks-asset-manager.accepted_file_types') ?? [];
40+
}
41+
42+
public static function getImageEditor(): array|null
43+
{
44+
return config('filament-flexible-blocks-asset-manager.image_editor', null);
45+
}
46+
47+
public static function getNavigationGroup(): ?string
48+
{
49+
return config('filament-flexible-blocks-asset-manager.navigation_group');
50+
}
51+
}

src/FilamentFlexibleBlocksAssetManagerServiceProvider.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Filament\Support\Assets\Js;
99
use Filament\Support\Facades\FilamentAsset;
1010
use Filament\Support\Facades\FilamentIcon;
11+
use Illuminate\Database\Eloquent\Relations\Relation;
1112
use Illuminate\Filesystem\Filesystem;
1213
use Livewire\Features\SupportTesting\Testable;
1314
use Spatie\LaravelPackageTools\Commands\InstallCommand;
1415
use Spatie\LaravelPackageTools\Package;
1516
use Spatie\LaravelPackageTools\PackageServiceProvider;
1617
use Statikbe\FilamentFlexibleBlocksAssetManager\Testing\TestsLaravelFilamentFlexibleBlocksAssetManager;
18+
use Statikbe\FilamentFlexibleBlocksAssetManager\Models\Asset as AssetModel;
1719

1820
class FilamentFlexibleBlocksAssetManagerServiceProvider extends PackageServiceProvider
1921
{
@@ -43,6 +45,10 @@ public function configurePackage(Package $package): void
4345
$package->hasConfigFile();
4446
}
4547

48+
if (file_exists($package->basePath("/../routes/{$configFileName}.php"))) {
49+
$package->hasRoutes($this->getRoutes());
50+
}
51+
4652
if (file_exists($package->basePath('/../database/migrations'))) {
4753
$package->hasMigrations($this->getMigrations());
4854
}
@@ -85,6 +91,13 @@ public function packageBooted(): void
8591

8692
// Testing
8793
Testable::mixin(new TestsLaravelFilamentFlexibleBlocksAssetManager());
94+
95+
//add Asset to morph map when used:
96+
if(Relation::requiresMorphMap()){
97+
Relation::morphMap([
98+
'filament-flexible-blocks-asset-manager::asset' => AssetModel::class,
99+
], true);
100+
}
88101
}
89102

90103
protected function getAssetPackageName(): ?string
@@ -99,8 +112,8 @@ protected function getAssets(): array
99112
{
100113
return [
101114
// AlpineComponent::make('laravel-filament-flexible-blocks-asset-manager', __DIR__ . '/../resources/dist/components/laravel-filament-flexible-blocks-asset-manager.js'),
102-
Css::make('laravel-filament-flexible-blocks-asset-manager-styles', __DIR__ . '/../resources/dist/laravel-filament-flexible-blocks-asset-manager.css'),
103-
Js::make('laravel-filament-flexible-blocks-asset-manager-scripts', __DIR__ . '/../resources/dist/laravel-filament-flexible-blocks-asset-manager.js'),
115+
//Css::make('laravel-filament-flexible-blocks-asset-manager-styles', __DIR__ . '/../resources/dist/laravel-filament-flexible-blocks-asset-manager.css'),
116+
//Js::make('laravel-filament-flexible-blocks-asset-manager-scripts', __DIR__ . '/../resources/dist/laravel-filament-flexible-blocks-asset-manager.js'),
104117
];
105118
}
106119

@@ -129,7 +142,9 @@ protected function getIcons(): array
129142
*/
130143
protected function getRoutes(): array
131144
{
132-
return [];
145+
return [
146+
'filament-flexible-blocks-asset-manager',
147+
];
133148
}
134149

135150
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Statikbe\FilamentFlexibleBlocksAssetManager\Http\Controllers;
4+
5+
6+
use Illuminate\Http\Response;
7+
use Illuminate\Support\Facades\Gate;
8+
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
9+
use Statikbe\FilamentFlexibleBlocksAssetManager\Models\Asset;
10+
11+
class AssetController
12+
{
13+
public function index(Asset $asset, string $locale=null)
14+
{
15+
//check if a gate needs to be applied:
16+
$authGate = FilamentFlexibleBlocksAssetManagerConfig::getAssetAuthorisationGate();
17+
if($authGate) {
18+
if (!Gate::allows($authGate, $asset)) {
19+
abort(Response::HTTP_FORBIDDEN);
20+
}
21+
}
22+
23+
//check if a policy needs to be applied:
24+
if(FilamentFlexibleBlocksAssetManagerConfig::getAssetAuthorisationPolicy()) {
25+
Gate::authorize('view', $asset);
26+
}
27+
28+
//TODO conversions
29+
$filters = [];
30+
if($locale){
31+
$filters = ['locale' => $locale];
32+
}
33+
34+
return $asset->getFirstMedia($asset->getAssetCollection(), $filters);
35+
}
36+
}

src/Models/Asset.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public function getAssetCollection(): string
4343
{
4444
return self::MEDIA_COLLECTION_ASSETS;
4545
}
46+
47+
/*public function getMorphClass(): string
48+
{
49+
return 'filament-flexible-blocks-asset-manager::asset';
50+
}*/
4651
}

0 commit comments

Comments
 (0)