ResponseDownload trait

Framework\HTTP\ResponseDownload

Trait ResponseDownload.

Defined in libraries/http/src/ResponseDownload.php

Properties #

$filepath
-
$filesize
-
$acceptRanges
-
$byteRanges
-
$sendType
-
$boundary
-
$handle
-
$delay
-
$readLength
-

$filepath

private string $filepath

$filesize

private int $filesize

$acceptRanges

private bool $acceptRanges = true

$byteRanges

private array|false $byteRanges = array ( )

$sendType

private string $sendType = 'normal'

$boundary

private string $boundary

$handle

private $handle = NULL

$delay

private int $delay = 0

$readLength

private int $readLength = 1024

Methods #

hasDownload()
Tell if Response has a downloadable file.
setDownload()
Sets a file to download/stream.
sendDownload()
-
flush()
-
getBoundaryLine()
-
getContentRangeLine()
-
getMultiPartTopLine()
-
parseByteRange()
Parse the HTTP Range Header line.
prepareRange()
-
readBuffer()
-
readFile()
-
sendMultiPart()
-
sendSinglePart()
-
setAcceptRanges()
-
setMultiPart()
-
setSinglePart()
-
validBytePos()
-

hasDownload()

public function hasDownload(): bool

Tell if Response has a downloadable file.

Returns bool

setDownload()

public function setDownload(string $filepath, bool $inline = false, bool $acceptRanges = true, int $delay = 0, int $readLength = 1024, ?string $filename = NULL): static

Sets a file to download/stream.

Parameters
  • string $filepath
  • bool $inline - Set Content-Disposition header as "inline". Browsers load the file in the window. Set true to allow video or audio streams
  • bool $acceptRanges - Set Accept-Ranges header to "bytes". Allow partial downloads, media players to move the time position forward and back and download managers to continue/download multi-parts
  • int $delay - Delay between flushs in microseconds
  • int $readLength - Bytes read by flush
  • ?string $filename - A custom filename
Returns static
throws
InvalidArgumentException If invalid file path
throws
RuntimeException If can not get the file size or modification time

sendDownload()

protected function sendDownload(): void
Returns void

flush()

private function flush(int $length): void
Parameters
  • int $length
Returns void

getBoundaryLine()

private function getBoundaryLine(): string
Returns string

getContentRangeLine()

private function getContentRangeLine(int $fistByte, int $lastByte): string
Parameters
  • int $fistByte
  • int $lastByte
Returns string

getMultiPartTopLine()

private function getMultiPartTopLine(): string
Returns string

parseByteRange()

private function parseByteRange(string $line): array|false

Parse the HTTP Range Header line.

Returns arrays of two indexes, representing first-byte-pos and last-byte-pos.
If return false, the Byte Ranges are invalid, so the Response must return
a 416 (Range Not Satisfiable) status.

Parameters
  • string $line
Returns array|false

prepareRange()

private function prepareRange(string $rangeLine): void
Parameters
  • string $rangeLine
Returns void

readBuffer()

private function readBuffer(int $firstByte, int $lastByte): void
Parameters
  • int $firstByte
  • int $lastByte
Returns void

readFile()

private function readFile(): void
Returns void

sendMultiPart()

private function sendMultiPart(): void
Returns void

sendSinglePart()

private function sendSinglePart(): void
Returns void

setAcceptRanges()

private function setAcceptRanges(bool $acceptRanges): void
Parameters
  • bool $acceptRanges
Returns void

setMultiPart()

private function setMultiPart(array ...$byteRanges): void
Parameters
  • array $byteRanges
Returns void

setSinglePart()

private function setSinglePart(int $firstByte, int $lastByte): void
Parameters
  • int $firstByte
  • int $lastByte
Returns void

validBytePos()

private function validBytePos(string $pos): int|false
Parameters
  • string $pos
Returns int|false