alltube/classes/Stream/YoutubeChunkStream.php

198 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2019-10-03 19:41:54 +00:00
/**
* YoutubeChunkStream class.
*/
namespace Alltube\Stream;
2020-05-13 19:39:38 +00:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* This is a wrapper around GuzzleHttp\Psr7\Stream.
* It is required because Youtube HTTP responses are buggy if we try to read further than the end of the response.
*/
class YoutubeChunkStream implements StreamInterface
{
/**
* HTTP response containing the video chunk.
*
2020-05-13 19:39:38 +00:00
* @var ResponseInterface
*/
private $response;
/**
* YoutubeChunkStream constructor.
*
2020-05-13 19:39:38 +00:00
* @param ResponseInterface $response HTTP response containing the video chunk
*/
2020-05-13 19:39:38 +00:00
public function __construct(ResponseInterface $response)
{
$this->response = $response;
}
/**
* Read data from the stream.
*
2020-09-27 13:53:53 +00:00
* @param mixed $length Read up to $length bytes from the object and return
*
* @return string
*/
2020-12-17 21:43:05 +00:00
public function read($length): string
{
2020-05-13 20:28:05 +00:00
$size = intval($this->response->getHeader('Content-Length')[0]);
if ($size - $this->tell() < $length) {
// Don't try to read further than the end of the stream.
$length = $size - $this->tell();
}
return $this->response->getBody()->read($length);
}
/**
* Reads all data from the stream into a string, from the beginning to end.
*/
2020-12-17 21:43:05 +00:00
public function __toString(): string
{
2020-05-13 20:28:05 +00:00
return (string)$this->response->getBody();
}
/**
* Closes the stream and any underlying resources.
*
* @return void
*/
public function close()
{
$this->response->getBody()->close();
}
/**
* Separates any underlying resources from the stream.
*
* @return resource|null
*/
public function detach()
{
return $this->response->getBody()->detach();
}
/**
* Get the size of the stream if known.
*
* @return int|null
*/
2020-12-17 21:43:05 +00:00
public function getSize(): ?int
{
return $this->response->getBody()->getSize();
}
/**
* Returns the current position of the file read/write pointer.
*
* @return int
*/
2020-12-17 21:43:05 +00:00
public function tell(): int
{
return $this->response->getBody()->tell();
}
/**
* Returns true if the stream is at the end of the stream.
*
* @return bool
*/
2020-12-17 21:43:05 +00:00
public function eof(): bool
{
return $this->response->getBody()->eof();
}
/**
* Returns whether or not the stream is seekable.
*
* @return bool
*/
2020-12-17 21:43:05 +00:00
public function isSeekable(): bool
{
return $this->response->getBody()->isSeekable();
}
/**
* Seek to a position in the stream.
*
2020-09-27 13:53:53 +00:00
* @param mixed $offset Stream offset
* @param int $whence Specifies how the cursor position will be calculated
*
2020-05-13 19:18:32 +00:00
* @return void
*/
public function seek($offset, $whence = SEEK_SET)
{
2020-05-13 19:18:32 +00:00
$this->response->getBody()->seek($offset, $whence);
}
/**
* Seek to the beginning of the stream.
*
2020-05-13 19:18:32 +00:00
* @return void
*/
public function rewind()
{
2020-05-13 19:18:32 +00:00
$this->response->getBody()->rewind();
}
/**
* Returns whether or not the stream is writable.
*
* @return bool
*/
2020-12-17 21:43:05 +00:00
public function isWritable(): bool
{
return $this->response->getBody()->isWritable();
}
/**
* Write data to the stream.
*
2020-09-27 13:53:53 +00:00
* @param mixed $string The string that is to be written
*
* @return mixed
*/
public function write($string)
{
return $this->response->getBody()->write($string);
}
/**
* Returns whether or not the stream is readable.
*
* @return bool
*/
2020-12-17 21:43:05 +00:00
public function isReadable(): bool
{
return $this->response->getBody()->isReadable();
}
/**
* Returns the remaining contents in a string.
*
* @return string
*/
2020-12-17 21:43:05 +00:00
public function getContents(): string
{
return $this->response->getBody()->getContents();
}
/**
* Get stream metadata as an associative array or retrieve a specific key.
*
2020-09-27 13:53:53 +00:00
* @param string|null $key Specific metadata to retrieve.
*
2020-12-17 21:43:05 +00:00
* @return mixed|null
*/
public function getMetadata($key = null)
{
return $this->response->getBody()->getMetadata($key);
}
}