|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibraryPro; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Cache\RateLimiting\Limit; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\Blade; |
|
8
|
|
|
use Illuminate\Support\Facades\RateLimiter; |
|
9
|
|
|
use Illuminate\Support\Facades\Route; |
|
10
|
|
|
use Illuminate\Support\ServiceProvider; |
|
11
|
|
|
use Livewire\Livewire; |
|
12
|
|
|
use Spatie\MediaLibraryPro\Commands\DeleteTemporaryUploadsCommand; |
|
13
|
|
|
use Spatie\MediaLibraryPro\Http\Components\MediaLibraryAttachmentComponent; |
|
14
|
|
|
use Spatie\MediaLibraryPro\Http\Components\MediaLibraryCollectionComponent; |
|
15
|
|
|
use Spatie\MediaLibraryPro\Http\Controllers\MediaLibraryPostS3Controller; |
|
16
|
|
|
use Spatie\MediaLibraryPro\Http\Controllers\MediaLibraryUploadController; |
|
17
|
|
|
use Spatie\MediaLibraryPro\Http\Livewire\LivewireMediaLibraryComponent; |
|
18
|
|
|
use Spatie\MediaLibraryPro\Http\Livewire\LivewireUploaderComponent; |
|
19
|
|
|
use Spatie\MediaLibraryPro\Models\TemporaryUpload; |
|
20
|
|
|
use Spatie\MediaLibraryPro\Support\TemporaryUploadPathGenerator; |
|
21
|
|
|
|
|
22
|
|
|
class MediaLibraryProServiceProvider extends ServiceProvider |
|
23
|
|
|
{ |
|
24
|
|
|
public function boot() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'media-library'); |
|
27
|
|
|
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang/', 'media-library'); |
|
28
|
|
|
$this->loadJsonTranslationsFrom(__DIR__ . '/../resources/lang/'); |
|
29
|
|
|
|
|
30
|
|
|
$this |
|
31
|
|
|
->registerPublishables() |
|
32
|
|
|
->registerBladeComponents() |
|
33
|
|
|
->registerLivewireComponents() |
|
34
|
|
|
->registerRouteMacros() |
|
35
|
|
|
->registerTemporaryUploaderPathGenerator(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function register() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::register(); |
|
41
|
|
|
|
|
42
|
|
|
$this->commands([ |
|
43
|
|
|
DeleteTemporaryUploadsCommand::class, |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function registerPublishables(): self |
|
48
|
|
|
{ |
|
49
|
|
|
if (! class_exists('CreateTemporaryUploadsTable')) { |
|
50
|
|
|
$this->publishes([ |
|
51
|
|
|
__DIR__ . '/../database/migrations/create_temporary_uploads_table.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_temporary_uploads_table.php'), |
|
52
|
|
|
], 'media-library-pro-migrations'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->publishes([ |
|
56
|
|
|
__DIR__ . '/../resources/views' => base_path('resources/views/vendor/media-library'), |
|
57
|
|
|
], 'media-library-pro-views'); |
|
58
|
|
|
|
|
59
|
|
|
$this->publishes([ |
|
60
|
|
|
__DIR__ . '/../resources/lang' => "{$this->app['path.lang']}/vendor/media-library", |
|
61
|
|
|
], 'media-library-pro-lang'); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function registerBladeComponents(): self |
|
67
|
|
|
{ |
|
68
|
|
|
Blade::component('media-library-attachment', MediaLibraryAttachmentComponent::class); |
|
69
|
|
|
Blade::component('media-library-collection', MediaLibraryCollectionComponent::class); |
|
70
|
|
|
Blade::component('media-library::components.media-library-icon', 'media-library-icon'); |
|
71
|
|
|
Blade::component('media-library::components.media-library-button', 'media-library-button'); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function registerLivewireComponents(): self |
|
77
|
|
|
{ |
|
78
|
|
|
if (! class_exists(Livewire::class)) { |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
Livewire::component('media-library', LivewireMediaLibraryComponent::class); |
|
83
|
|
|
Livewire::component('media-library-uploader', LivewireUploaderComponent::class); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function registerRouteMacros(): self |
|
89
|
|
|
{ |
|
90
|
|
|
RateLimiter::for('medialibrary-pro-uploads', function (Request $request) { |
|
91
|
|
|
return [ |
|
92
|
|
|
Limit::perMinute(10)->by($request->ip()), |
|
93
|
|
|
]; |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
Route::macro('mediaLibrary', function (string $baseUrl = 'media-library-pro') { |
|
97
|
|
|
Route::prefix($baseUrl)->group(function () { |
|
98
|
|
|
if (config('media-library.enable_vapor_uploads')) { |
|
99
|
|
|
Route::post("post-s3", '\\' . MediaLibraryPostS3Controller::class) |
|
100
|
|
|
->name('media-library-post-s3') |
|
101
|
|
|
->middleware(['throttle:medialibrary-pro-uploads']); |
|
102
|
|
|
|
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
Route::post("uploads", '\\' . MediaLibraryUploadController::class) |
|
107
|
|
|
->name('media-library-uploads') |
|
108
|
|
|
->middleware(['throttle:medialibrary-pro-uploads']); |
|
109
|
|
|
}); |
|
110
|
|
|
}); |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function registerTemporaryUploaderPathGenerator(): self |
|
116
|
|
|
{ |
|
117
|
|
|
$configuredValues = config('media-library.custom_path_generators', []); |
|
118
|
|
|
|
|
119
|
|
|
if (! array_key_exists(TemporaryUpload::class, $configuredValues)) { |
|
120
|
|
|
$configuredValues[TemporaryUpload::class] = TemporaryUploadPathGenerator::class; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
config()->set('media-library.custom_path_generators', $configuredValues); |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|