Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Add support for \SplTempFileObject in BinaryFileResponse #49144

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

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Jan 28, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #48530
License MIT
Doc PR Todo

Add support for \SplTempFileObject in BinaryFileResponse

@carsonbot
Copy link

Hey!

I think @alli83 has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@fabpot
Copy link
Member

fabpot commented Oct 11, 2023

Would it be possible to add support in BinaryFileResponse instead?

@fabpot fabpot modified the milestones: 6.4, 7.1 Oct 11, 2023
@alexandre-daubois
Copy link
Member Author

It doesn't seem to. BinaryFileResponse needs a "physical" file. This would mean that the temporary file would have to be physically written to disk each time, and we'd lose the whole point of this new method I guess.

@nicolas-grekas
Copy link
Member

BinaryFileResponse accepts an SplFileInfo as argument and SplTempFileObject extends SplFileInfo, so it's already possible to pass a SplTempFileObject there. Can't we make BinaryFileResponse able to deal with such instances, skipping all the features that need a physical file when this happens?

@alexandre-daubois alexandre-daubois force-pushed the temp-streamed-response branch 4 times, most recently from 6294b4a to 0151376 Compare October 11, 2023 11:12
@alexandre-daubois alexandre-daubois changed the title [HttpFoundation] Add StreamedResponse::createFromTempFile() [HttpFoundation] Add support for \SplTempFileObject in BinaryFileResponse Oct 11, 2023
@alexandre-daubois
Copy link
Member Author

Thank you for the hint Nicolas, I tried a implementation in BinaryFileResponse to add support for \SplTempFileObject

@fabpot
Copy link
Member

fabpot commented Feb 3, 2024

@alexandre-daubois Can you rebase on current 7.1? Thank you.

@alexandre-daubois
Copy link
Member Author

Yep of course, here you go 🙂

@fabpot
Copy link
Member

fabpot commented Feb 5, 2024

Thank you @alexandre-daubois.

@fabpot fabpot merged commit d9d6024 into symfony:7.1 Feb 5, 2024
@fabpot fabpot mentioned this pull request May 2, 2024
nicolas-grekas added a commit that referenced this pull request Nov 5, 2024
…BinaryFileResponse` (elementaire)

This PR was merged into the 7.1 branch.

Discussion
----------

[HttpFoundation] Fix support for `\SplTempFileObject` in `BinaryFileResponse`

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | yes
| PR original Feature | #49144
| New feature?  | no
| Deprecations? | no
| Issues        |  -
| License       | MIT

We can not call some methods with an object of `\SplTempFileObject()`. We get this error: `[...] stat failed for php://temp`.

I have checked the code against methods listed in [this note](https://www.php.net/manual/en/class.spltempfileobject.php#128962).

I have found:
- `getMTime()` called in `BinaryFileResponse::setAutoLastModified()`
- `getSize()` called in `BinaryFileResponse::prepare()`
- `isReadable()` called in `BinaryFileResponse::setFile()` (already safe)

I have updated the unit test and patched the class `BinaryFileResponse`.

Note: calling `SplFileObject::fstat()` gives `mtime` equals to `0`. I think it is nonsense to use that as value for last modified. I have decided to use `time()` because i guess, we can not do better. Indeed, we have no idea how much time have passed between making the temp file and the call to `setAutoLastModified()` by the developper.

Commits
-------

a104d50 Fix support for \SplTempFileObject in BinaryFileResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BinaryFileResponse] Possible to use SplTempFileObject for download attachments.
4 participants