Skip to content

Commit

Permalink
New 'getDownloadFilename' method for extensibility (#3504)
Browse files Browse the repository at this point in the history
* New 'getDownloadFilename' method for extensibility
Added Symfony HeaderUtils to create content disposition

* Refactored how download file name is determined

---------

Co-authored-by: Chris Page <[email protected]>
  • Loading branch information
chrispage1 and chrispage1 authored Jan 11, 2024
1 parent d6f077b commit fded7c9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/MediaCollections/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Mail\Attachment;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
Expand Down Expand Up @@ -138,6 +139,11 @@ public function getAvailableUrl(array $conversionNames): string
return $this->getUrl();
}

public function getDownloadFilename(): string
{
return $this->file_name;
}

public function getAvailableFullUrl(array $conversionNames): string
{
foreach ($conversionNames as $conversionName) {
Expand Down Expand Up @@ -324,11 +330,13 @@ public function toInlineResponse($request)

private function buildResponse($request, string $contentDispositionType)
{
$filename = str_replace('"', '\'', Str::ascii($this->getDownloadFilename()));

$downloadHeaders = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Type' => $this->mime_type,
'Content-Length' => $this->size,
'Content-Disposition' => $contentDispositionType.'; filename="'.$this->file_name.'"',
'Content-Disposition' => $contentDispositionType . '; filename="' . $filename . '"',
'Pragma' => 'public',
];

Expand Down

0 comments on commit fded7c9

Please sign in to comment.