Example #1
0
 /**
  * {@inheritdoc}
  */
 public function send(Request $request, Response $response)
 {
     // Add headers that should always be included
     $response->type($this->options['content_type']);
     $response->header('accept-ranges', $request->isSafe() ? 'bytes' : 'none');
     $response->header('content-disposition', $this->options['disposition'] . '; filename="' . $this->options['file_name'] . '"');
     // Get the requested byte range
     $range = $request->header('range');
     if ($range !== null) {
         $range = $this->calculateRange($range);
     }
     if ($range === false) {
         // Not an acceptable range so we'll just send an empty response
         // along with a "requested range not satisfiable" status
         $response->status(416);
         $response->sendHeaders();
     } else {
         if ($range === null) {
             // No range was provided by the client so we'll just fake one for the sendFile method
             // and set the content-length header value to the full file size
             $range = ['start' => 0, 'end' => $this->fileSize - 1];
             $response->header('content-length', $this->fileSize);
         } else {
             // Valid range so we'll need to tell the client which range we're sending
             // and set the content-length header value to the length of the byte range
             $response->status(206);
             $response->header('content-range', sprintf('bytes %s-%s/%s', $range['start'], $range['end'], $this->fileSize));
             $response->header('content-length', $range['end'] - $range['start'] + 1);
         }
         // Send headers and the requested byte range
         $response->sendHeaders();
         $this->sendFile($range['start'], $range['end']);
         // Execute callback if there is one
         if (!empty($this->options['callback'])) {
             $this->options['callback']($this->filePath);
         }
     }
 }