How to stream partial content of file

Hello,

I’m migrating my existing webapp to REST API using Slim3.0

I offer possibility to stream partial content of requested file if a range is provided in request header, like here : http://pastebin.com/w7Hsd7tQ

So i use \Slim\Http\Stream to handle it, and it’s work fine but i’m unable to send partial content of files.

\Slim\Http\Stream::isSeekable() return TRUE but when i seek my file, it fully downloaded anyway…

$stream->seek($start_seek, SEEK_CUR);
$stream->tell() == $start_seek // TRUE;

I send same headers options as before and HTTP code 200 -> fully downloading file

I send same headers options as before and HTTP code 206 (partial content) -> chrome give me an error (file not found)

How can I implement partial content ?

Regards,

Hello eric22,

In App::respond() the stream is [rewinded] (https://github.com/slimphp/Slim/blob/3.x/Slim/App.php#L373):

if ($body->isSeekable()) {
    $body->rewind();
}

I think this is why calling $stream->seek() still results in the full file content.

You could try creating a decorating the \Slim\Http\Stream instance with a class that implements Psr\Http\Message\StreamInterface and implement at minimum isSeekable(), rewind(), eof() and read(). The call to rewind() would then translate to a seek() call on the stream that is decorated.

I haven’t checked, but perhaps there is already a middleware solution for this. If so you don’t have to roll your own.

Regards,

Lennaert

Hello llvdl,

You’re right, I just added a condition around the $body->rewind();
In order to differentiate the stream from a [partial] file or a JSON response in body.

Thank you for support !