diff --git a/packages/spatie-laravel-media-library-plugin/README.md b/packages/spatie-laravel-media-library-plugin/README.md index 030bf926d7..7acd5a8a03 100644 --- a/packages/spatie-laravel-media-library-plugin/README.md +++ b/packages/spatie-laravel-media-library-plugin/README.md @@ -92,6 +92,18 @@ SpatieMediaLibraryFileUpload::make('attachments') ->customProperties(['zip_filename_prefix' => 'folder/subfolder/']) ``` +### Adding custom headers + +You may pass in custom headers when uploading files using the `customHeaders()` method: + +```php +use Filament\Forms\Components\SpatieMediaLibraryFileUpload; + +SpatieMediaLibraryFileUpload::make('attachments') + ->multiple() + ->customHeaders(['CacheControl' => 'max-age=86400']) +``` + ### Generating responsive images You may [generate responsive images](https://spatie.be/docs/laravel-medialibrary/responsive-images/getting-started-with-responsive-images) when the files are uploaded using the `responsiveImages()` method: diff --git a/packages/spatie-laravel-media-library-plugin/src/Forms/Components/SpatieMediaLibraryFileUpload.php b/packages/spatie-laravel-media-library-plugin/src/Forms/Components/SpatieMediaLibraryFileUpload.php index ec3670eb02..f6ebfef47b 100644 --- a/packages/spatie-laravel-media-library-plugin/src/Forms/Components/SpatieMediaLibraryFileUpload.php +++ b/packages/spatie-laravel-media-library-plugin/src/Forms/Components/SpatieMediaLibraryFileUpload.php @@ -25,6 +25,11 @@ class SpatieMediaLibraryFileUpload extends FileUpload protected string | Closure | null $mediaName = null; + /** + * @var array | Closure | null + */ + protected array | Closure | null $customHeaders = null; + /** * @var array | Closure | null */ @@ -134,6 +139,7 @@ class SpatieMediaLibraryFileUpload extends FileUpload $filename = $component->getUploadedFileNameForStorage($file); $media = $mediaAdder + ->addCustomHeaders($component->getCustomHeaders()) ->usingFileName($filename) ->usingName($component->getMediaName($file) ?? pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) ->storingConversionsOnDisk($component->getConversionsDisk() ?? '') @@ -183,6 +189,16 @@ class SpatieMediaLibraryFileUpload extends FileUpload return $this; } + /** + * @param array | Closure | null $headers + */ + public function customHeaders(array | Closure | null $headers): static + { + $this->customHeaders = $headers; + + return $this; + } + /** * @param array | Closure | null $properties */ @@ -265,6 +281,14 @@ class SpatieMediaLibraryFileUpload extends FileUpload return $this->evaluate($this->conversionsDisk); } + /** + * @return array + */ + public function getCustomHeaders(): array + { + return $this->evaluate($this->customHeaders) ?? []; + } + /** * @return array */