-
-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LiveComponent] Add support for downloading files from LiveActions (Experimental) #2483
Open
smnandre
wants to merge
12
commits into
symfony:2.x
Choose a base branch
from
smnandre:live/download-files
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd6b8bd
Add getBlob method on BackendResponse
smnandre 61cee45
Handle downloads in Component.js
smnandre 2f34d71
Build dist files
smnandre f24dea6
Add LiveDownloadResponse + checks in Subscriber
smnandre d7d6bb5
Add entry in CHANGELOG
smnandre dd75be0
Add demo on website
smnandre fe92cc7
CS fixes
smnandre f81dcff
Prefer return instead of return await
smnandre 4f6f869
Reuse 'Content-Disposition' header value
smnandre 36143e6
Reuse 'Content-Disposition' header value
smnandre 32cd824
Simplify header checks
smnandre 456c0e2
rework
smnandre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,8 @@ export default class { | |
|
||
return this.body; | ||
} | ||
|
||
async getBlob(): Promise<Blob> { | ||
return this.response.blob(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Symfony\UX\LiveComponent; | ||
|
||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpFoundation\HeaderUtils; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class LiveResponse | ||
{ | ||
/** | ||
* @param string|\SplFileInfo $file The file to send as a response | ||
* @param string|null $filename The name of the file to send (defaults to the basename of the file) | ||
* @param string|null $contentType The content type of the file (defaults to `application/octet-stream`) | ||
*/ | ||
public static function file(string|\SplFileInfo $file, ?string $filename = null, ?string $contentType = null, ?int $size = null): BinaryFileResponse | ||
{ | ||
return new BinaryFileResponse($file, 200, [ | ||
'Content-Disposition' => HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $filename ?? basename($file)), | ||
'Content-Type' => $contentType ?? 'application/octet-stream', | ||
'Content-Length' => $size ?? ($file instanceof \SplFileInfo ? $file->getSize() : null), | ||
]); | ||
} | ||
|
||
/** | ||
* @param resource|Closure $file The file to stream as a response | ||
* @param string $filename The name of the file to send (defaults to the basename of the file) | ||
* @param string|null $contentType The content type of the file (defaults to `application/octet-stream`) | ||
* @param int|null $size The size of the file | ||
*/ | ||
public static function streamFile(mixed $file, string $filename, ?string $contentType = null, ?int $size = null): StreamedResponse | ||
{ | ||
if (!is_resource($file) && !$file instanceof \Closure) { | ||
throw new \InvalidArgumentException(sprintf('The file must be a resource or a closure, "%s" given.', get_debug_type($file))); | ||
} | ||
|
||
return new StreamedResponse($file instanceof \Closure ? $file(...) : function () use ($file) { | ||
while (!feof($file)) { | ||
echo fread($file, 1024); | ||
} | ||
}, 200, [ | ||
'Content-Disposition' => HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $filename), | ||
'Content-Type' => $contentType ?? 'application/octet-stream', | ||
'Content-Length' => $size, | ||
]); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/LiveComponent/tests/Fixtures/Component/DownloadFileComponent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
use Symfony\UX\LiveComponent\Attribute\LiveAction; | ||
use Symfony\UX\LiveComponent\Attribute\LiveArg; | ||
use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
use Symfony\UX\LiveComponent\LiveResponse; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
*/ | ||
#[AsLiveComponent('download_file', template: 'components/download_file.html.twig')] | ||
class DownloadFileComponent | ||
{ | ||
use DefaultActionTrait; | ||
|
||
private const FILE_DIRECTORY = __DIR__.'/../files/'; | ||
|
||
#[LiveAction] | ||
public function download(): BinaryFileResponse | ||
{ | ||
$file = new \SplFileInfo(self::FILE_DIRECTORY.'/foo.json'); | ||
|
||
return LiveResponse::file($file); | ||
} | ||
|
||
#[LiveAction] | ||
public function generate(): BinaryFileResponse | ||
{ | ||
$file = new \SplTempFileObject(); | ||
$file->fwrite(file_get_contents(self::FILE_DIRECTORY.'/foo.json')); | ||
|
||
return LiveResponse::file($file, 'foo.json', size: 1000); | ||
} | ||
|
||
#[LiveAction] | ||
public function heavyFile(#[LiveArg] int $size): BinaryFileResponse | ||
{ | ||
$file = new \SplFileInfo(self::FILE_DIRECTORY.'heavy.txt'); | ||
|
||
$response = LiveResponse::file($file); | ||
$response->headers->set('Content-Length', 10000000); // 10MB | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Foo</title> | ||
</head> | ||
<body> | ||
<h1>Bar</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"foo": "bar" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Foo | ||
|
||
## Bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text |
3 changes: 3 additions & 0 deletions
3
src/LiveComponent/tests/Fixtures/templates/components/download_file.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div {{ attributes }}> | ||
|
||
</div> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It depends... if you trigger the "click" and the file start downloading I think browser play the "pipe" role here.
But during my test I managed to crashed pretty violently Chrome and Safari multiple times.