alltube/classes/PlaylistArchiveStream.php

309 lines
6.7 KiB
PHP
Raw Normal View History

2017-05-02 15:04:55 +00:00
<?php
/**
* PlaylistArchiveStream class.
*/
namespace Alltube;
use Barracuda\ArchiveStream\TarArchive;
use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use stdClass;
2017-05-02 15:04:55 +00:00
/**
* Class used to create a Tar archive from playlists and stream it to the browser.
*
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
2017-05-02 15:04:55 +00:00
*/
class PlaylistArchiveStream extends TarArchive implements StreamInterface
2017-05-02 15:04:55 +00:00
{
/**
* videos to add in the archive.
2017-05-02 15:04:55 +00:00
*
* @var PlaylistArchiveVideo[]
2017-05-02 15:04:55 +00:00
*/
private $videos = [];
2017-05-02 15:04:55 +00:00
/**
* Stream used to store data before it is sent to the browser.
*
* @var resource
*/
private $buffer;
/**
* Guzzle client.
*
* @var Client
2017-05-02 15:04:55 +00:00
*/
private $client;
/**
* VideoDownload instance.
*
* @var VideoDownload
*/
private $download;
/**
* Current video being streamed to the archive.
2017-05-02 15:04:55 +00:00
*
* @var int
*/
private $curVideo;
2017-05-02 15:04:55 +00:00
/**
* Video format to download.
*
* @var string
*/
private $format;
/**
* PlaylistArchiveStream constructor.
2017-12-24 00:12:47 +00:00
*
2019-04-20 22:58:04 +00:00
* @param Config $config Config instance.
* @param stdClass $video Video object returned by youtube-dl
* @param string $format Requested format
2017-05-02 15:04:55 +00:00
*/
public function __construct(Config $config, stdClass $video, $format)
2017-05-02 15:04:55 +00:00
{
$this->client = new Client();
$this->download = new VideoDownload($config);
$this->format = $format;
$buffer = fopen('php://temp', 'r+');
if ($buffer !== false) {
$this->buffer = $buffer;
}
foreach ($video->entries as $entry) {
$this->videos[] = new PlaylistArchiveVideo($entry->url);
}
2017-05-02 15:04:55 +00:00
}
/**
* Add data to the archive.
*
2017-05-04 22:06:18 +00:00
* @param string $data Data
2017-05-02 15:04:55 +00:00
*
* @return void
*/
protected function send($data)
{
$pos = ftell($this->buffer);
// Add data to the buffer.
2017-05-02 15:04:55 +00:00
fwrite($this->buffer, $data);
if ($pos !== false) {
// Rewind so that read() can later read this data.
fseek($this->buffer, $pos);
}
2017-05-02 15:04:55 +00:00
}
/**
* Write data to the stream.
2017-05-02 15:04:55 +00:00
*
* @param string $string The string that is to be written.
2017-05-02 15:04:55 +00:00
*
* @return int
2017-05-02 15:04:55 +00:00
*/
public function write($string)
2017-05-02 15:04:55 +00:00
{
throw new RuntimeException('This stream is not writeable.');
}
2017-05-02 15:04:55 +00:00
/**
* Get the size of the stream if known.
*
* @return null
*/
public function getSize()
{
}
/**
* Returns whether or not the stream is seekable.
*
2019-04-20 22:58:04 +00:00
* @return bool
*/
public function isSeekable()
{
return false;
}
/**
* Seek to the beginning of the stream.
*
* @return void
*/
public function rewind()
{
throw new RuntimeException('This stream is not seekable.');
}
/**
* Returns whether or not the stream is writable.
*
2019-04-20 22:58:04 +00:00
* @return bool
*/
public function isWritable()
{
return false;
}
/**
* Returns whether or not the stream is readable.
*
2019-04-20 22:58:04 +00:00
* @return bool
*/
public function isReadable()
{
2017-05-02 15:04:55 +00:00
return true;
}
/**
* Returns the remaining contents in a string.
2017-05-02 15:04:55 +00:00
*
* @return string
*/
public function getContents()
{
return stream_get_contents($this->buffer);
}
/**
* Get stream metadata as an associative array or retrieve a specific key.
*
2019-04-20 22:58:04 +00:00
* @param string $key string $key Specific metadata to retrieve.
*
* @return null
2017-05-02 15:04:55 +00:00
*/
public function getMetadata($key = null)
2017-05-02 15:04:55 +00:00
{
}
/**
* Separates any underlying resources from the stream.
2017-05-02 15:04:55 +00:00
*
* @return resource
2017-05-02 15:04:55 +00:00
*/
public function detach()
2017-05-02 15:04:55 +00:00
{
$stream = $this->buffer;
$this->close();
2019-04-20 22:58:04 +00:00
return $stream;
2017-05-02 15:04:55 +00:00
}
/**
* Reads all data from the stream into a string, from the beginning to end.
*
* @return string
*/
public function __toString()
{
$string = '';
foreach ($this->videos as $file) {
$string .= $file->url;
}
return $string;
}
/**
2019-04-20 22:58:04 +00:00
* Returns the current position of the file read/write pointer.
2017-05-02 15:04:55 +00:00
*
* @return int|false
2017-05-02 15:04:55 +00:00
*/
public function tell()
2017-05-02 15:04:55 +00:00
{
return ftell($this->buffer);
}
/**
* Seek to a position in the stream.
2017-05-02 15:04:55 +00:00
*
* @param int $offset Offset
* @param int $whence Specifies how the cursor position will be calculated
2017-05-02 15:04:55 +00:00
*
* @return void
2017-05-02 15:04:55 +00:00
*/
public function seek($offset, $whence = SEEK_SET)
2017-05-02 15:04:55 +00:00
{
throw new RuntimeException('This stream is not seekable.');
2017-05-02 15:04:55 +00:00
}
/**
* Returns true if the stream is at the end of the stream.
2017-05-02 15:04:55 +00:00
*
* @return bool
*/
public function eof()
2017-05-02 15:04:55 +00:00
{
foreach ($this->videos as $file) {
if (!$file->complete) {
2017-05-02 15:04:55 +00:00
return false;
}
}
return true;
}
/**
* Read data from the stream.
2017-05-02 15:04:55 +00:00
*
* @param int $count Number of bytes to read
*
* @return string|false
2017-05-02 15:04:55 +00:00
*/
public function read($count)
2017-05-02 15:04:55 +00:00
{
if (isset($this->curVideo)) {
if (isset($this->curVideo->stream)) {
if (!$this->curVideo->stream->eof()) {
$this->stream_file_part($this->curVideo->stream->read($count));
} elseif (!$this->curVideo->complete) {
$this->complete_file_stream();
$this->curVideo->complete = true;
} else {
$this->curVideo = next($this->videos);
}
} else {
$urls = $this->download->getURL($this->curVideo->url, $this->format);
$response = $this->client->request('GET', $urls[0], ['stream' => true]);
$contentLengthHeaders = $response->getHeader('Content-Length');
$this->init_file_stream_transfer(
$this->download->getFilename($this->curVideo->url, $this->format),
$contentLengthHeaders[0]
);
$this->curVideo->stream = $response->getBody();
}
} else {
$this->curVideo = current($this->videos);
2017-05-02 15:04:55 +00:00
}
return fread($this->buffer, $count);
}
2019-04-19 22:37:49 +00:00
/**
* Closes the stream and any underlying resources.
2019-04-19 22:37:49 +00:00
*
* @return void
*/
public function close()
2019-04-19 22:37:49 +00:00
{
if (is_resource($this->buffer)) {
fclose($this->buffer);
}
foreach ($this->videos as $file) {
if (is_resource($file->stream)) {
fclose($file->stream);
}
}
2019-04-19 22:37:49 +00:00
}
2017-05-02 15:04:55 +00:00
}