- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 107
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
Calculate ZIP Size #241
Calculate ZIP Size #241
Conversation
@jszobody Can you have a look if this is going into a direction that would work for you? I introduced the parameter The PR is not done. I just wanted to try the points we talked about. The API for the user is quite bad at the moment and it would probably make sense to implement that completely differently. |
Pull Request Test Coverage Report for Build 4749066365
💛 - Coveralls |
I love the lax/strict simulation modes, those look very useful. Nice improvement over what I was doing before. One issue: it looks like your File class requires an open What do you think about optionally accepting a closure/callable for the new File(
...
stream: fn() => fopen($myFilePath, 'r')
); This way you can run the simulation with all the files up front, before streams are opened. This would only work in strict mode, obviously. Then when zipping up the files for real, you can retrieve the actual stream from the closure. My own package would then provide Adapters for different file sources, like S3. I would add files to your ZipStream with closures, and inside those closures handle the appropriate stream logic. Thoughts? |
@jszobody I want to keep the amount of branching as low as possible to make the code as simple as possible. I'll think about a way to add a simulated file with We could probably make the stream completely optional for Do you have an idea on how to structure the API on this feature? In the best case I would like to automatically set a content-length header if enabled. But I don't really want to store all the file parameter anywhere since that could cause some issues with larger zips / a lot of files. |
I have a better idea on the infterface of the changes:
This would look something like this: $zip = new ZipStream(
outputName: 'some_files.zip'
operationMode: OperationMode::SIMULATE_STRICT
);
// Add files as usual
$zip->addFileFromPath(fileName: 'example.jpg', path: '/data/example.jpg');
// Add callback files (S3 use-case)
foreach($s3Files as $fileName => $fileSize) {
$zip->addCallbackFile(
fileName: $fileName,
exactSize: $fileSize,
callback: funtion() use($fileName) {
// Returns `resource` / `StreamInterface` / raw data
return getDataFromS3($fileName);
}
}
$size = $zip->finish(); // Returns size & switches to `OperationMode` `Normal`
$zip->replaySimulation(); // Sets headers and replays recorded files The dowside would be that if thousands of files are simulated, they could potentially clog up memory. Alternatively, we can leave things as is in the PR and put the abstraction of it to the user. WDYT? In any case, I think this should be part of a new 3.1 release and we should go ahead with the release already as discussed in #237. |
c1c6047
to
803fdf0
Compare
@jszobody Do you have any feedback to my last two comments? I would like to wrap this PR up :) |
@maennchen Apologies! I missed your previous update. This approach looks very reasonable to me. Just a couple very minor thoughts on naming (which you are welcome to ignore and use whatever you think best!):
2) What about: $size = $zip->finishSimulation(); // Returns size & switches to `OperationMode` `Normal`
$zip->replay(); // Sets headers and replays recorded files, building the zip for real this time Again, super minor thoughts. I'm going to wrap this API anyway in my Laravel package so it doesn't ultimately matter all that much. Thanks again, this is looking awesome! |
0dcf9d4
to
3389870
Compare
@jszobody I think all the functionality should now be in place. There's still a few details that I'm unhappy about:
It would be cool if you could give it a spin and let me know what you think :) |
3389870
to
577ebd8
Compare
If there's no more feedback for the moment, I'll merge it and publish it as |
577ebd8
to
99b4179
Compare
Calculates the resulting file size with the minimal amount of work that is required. Fixes #89
99b4179
to
54bfe26
Compare
Calculates the resulting file size with the minimal amount of work that is required.
Implements basics for #89
Follow up of discussion in stechstudio/laravel-zipstream#83
TODO
\RuntimeError
with proper error