How to implement resilient download in Silex

In silex, I can do this to force the download of a file:

use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; $app = new Silex\Application(); // Url can be http://pathtomysilexapp.com/download $app->get('/download', function (Request $request) use ($app) { $file = '/path/to/download.zip'; if( !file_exists($file) ){ return new Response('File not found.', 404); } return $app->sendFile($file)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'download.zip'); }); $app->run(); 

This works well for small files. However, my use case requires downloading a large file, which can be paused / resumed by the download manager.

There is an example streaming file, but it doesn't seem to be what I'm looking for. Has anyone done this before? I could just use the answer from here and do it. But it would be nice if it were silexy.

+5
source share
1 answer

The implementation is quite trivial if you have a file on disk. This is similar to dragging records from a table.

Read the headers, open the file, find the desired position and read the required size.

You only need to know a little about the request and response headers.

Does the server accept ranges:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests

HTTP status 206 for content ranges:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206

Content range header information:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range

0
source

Source: https://habr.com/ru/post/1203121/


All Articles