Skip to content
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

When streaming media, replace with feof/fread instead of fpassthru #3501

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

chrispage1
Copy link
Contributor

@chrispage1 chrispage1 commented Jan 4, 2024

Hi,

I ran into an issue where when returning a Media item directly in our controller, it was building our response as a stream but hitting memory allocation errors.

The reason for this is the functionality of fpassthru. It's designed to 'Output all remaining data on a file pointer' - https://www.php.net/manual/en/function.fpassthru.php

This update uses feof/fread to instead chunk the output and clear the buffers, preventing memory issues.

I've also added a setter to allow modification of the chunk size. Example usage:

public function show(Media $media) {
    // stream our media out in 5MB chunks
    return $media->setStreamChunkSize((1024*1024)*5);
}

While I was in the project I've also added a helper method to get a stream for a conversion, this was something that we've been using in a project of ours.

Replaced fpassthru with feof/fread with variable chunk size
@freekmurze freekmurze merged commit 7b4ccb7 into spatie:main Jan 5, 2024
5 checks passed
@freekmurze
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants