Coder Social home page Coder Social logo

Cache file size about laravel-zipstream HOT 6 CLOSED

toto4ds avatar toto4ds commented on August 22, 2024
Cache file size

from laravel-zipstream.

Comments (6)

jszobody avatar jszobody commented on August 22, 2024

Sure thing. You just need to create the S3File model yourself, so you can set the filesize.

// Instead of this...
$zip->add('s3://bucket/key', 'zipfilename.txt');

// ... do this:
$zip->add(
    S3File::make('s3://bucket/key', 'zipfilename.txt')->setFilesize(12345)
);

from laravel-zipstream.

toto4ds avatar toto4ds commented on August 22, 2024

Hi jszobody,
Thanks for the quick response. Now it works great, download starts instantly.
It would be great to add this information to the README.md

But that doesn't solve the problem for HttpFile model - $this->getHeaders()

public function canPredictZipDataSize(): bool
{
return array_key_exists(self::HEADER_CONTENT_LENGTH, $this->getHeaders()) &&
parent::canPredictZipDataSize();
}

I think it's a good idea to add
if ($this->filesize) { return parent::canPredictZipDataSize(); }
here

from laravel-zipstream.

toto4ds avatar toto4ds commented on August 22, 2024

Hi jszobody,
I will try to convey my point of view again in other words.

The model File has a canPredictZipDataSize() method. And only in the HttpFile model it is also present.
The HttpFile model sets the header variable to make one HTTP request to the file rather than two via the calculateFilesize() and canPredictZipDataSize() methods to get the file size - HEADER_CONTENT_LENGTH
We know the size of each file and in order not to do extra work, we transfer it using ->setFilesize(12345).
Method calculateFilesize() is no longer called, but canPredictZipDataSize() also continues to run because the $headers variable is empty. What we wanted to get rid of continues to bother us.
It is necessary that the $this->getHeaders() are not called if the file size was set in advance here https://github.com/stechstudio/laravel-zipstream/blob/master/src/Models/HttpFile.php#L52
What do you think about this ?

from laravel-zipstream.

jszobody avatar jszobody commented on August 22, 2024

Yeah, makes sense. It should skip headers if filesize is present. Do you want to submit a PR for this?

Also, I'm curious what kind of HTTP file sources you're using, with known filesizes up front. That's interesting.

from laravel-zipstream.

toto4ds avatar toto4ds commented on August 22, 2024

Yeah, I'll do the PR later.

Initially, for a very long time, I use mod_zip (https://github.com/evanmiller/mod_zip) for nginx, and he wants to know the size and hash in advance.
I just decided to see what PHP has achieved while working with zip during this time.

from laravel-zipstream.

toto4ds avatar toto4ds commented on August 22, 2024

On a quick inspection, I ran into a problem.

This line returns the size https://github.com/stechstudio/laravel-zipstream/blob/master/src/Models/HttpFile.php#L26
But there may be no header Content-Length in the response and this will lead to an error.
We can process this and answer in the false - if (!array_key_exists(self::HEADER_CONTENT_LENGTH, $headers)) { return false; }
But after that we have to return union types - int|false which have been supported since php 8.0. And we have the minimum version php: ^7.1
Using zero to check is not correct too as it is the correct file size.
Then it turns out that the correct way is set to check the file size is if (is_numeric($this->filesize))

What do you think?

from laravel-zipstream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.