alltube/classes/PlaylistArchiveStream.php

310 lines
6.8 KiB
PHP
Raw Normal View History

2017-05-02 17:04:55 +02: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 17:04:55 +02: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 17:04:55 +02:00
*/
class PlaylistArchiveStream extends TarArchive implements StreamInterface
2017-05-02 17:04:55 +02:00
{
/**
* Files to add in the archive.
*
* @var array[]
*/
private $files = [];
2017-05-02 17:04:55 +02:00
/**
* Stream used to store data before it is sent to the browser.
*
* @var resource
*/
private $buffer;
/**
* Guzzle client.
*
* @var Client
2017-05-02 17:04:55 +02:00
*/
private $client;
/**
* VideoDownload instance.
*
* @var VideoDownload
*/
private $download;
/**
* Current file position in $files array.
*
* @var int
*/
private $curFile = 0;
/**
* Video format to download.
*
* @var string
*/
private $format;
/**
* PlaylistArchiveStream constructor.
2017-12-24 01:12:47 +01:00
*
* @param Config $config Config instance.
* @param stdClass $video Video object returned by youtube-dl
* @param string $format Requested format
2017-05-02 17:04:55 +02:00
*/
public function __construct(Config $config, stdClass $video, $format)
2017-05-02 17:04:55 +02: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->files[] = [
'url' => $entry->url,
'headersSent' => false,
'complete' => false,
'stream' => null,
];
}
2017-05-02 17:04:55 +02:00
}
/**
* Add data to the archive.
*
2017-05-05 00:06:18 +02:00
* @param string $data Data
2017-05-02 17:04:55 +02:00
*
* @return void
*/
protected function send($data)
{
$pos = ftell($this->buffer);
// Add data to the buffer.
2017-05-02 17:04:55 +02:00
fwrite($this->buffer, $data);
if ($pos !== false) {
// Rewind so that read() can later read this data.
fseek($this->buffer, $pos);
}
2017-05-02 17:04:55 +02:00
}
/**
* Write data to the stream.
2017-05-02 17:04:55 +02:00
*
* @param string $string The string that is to be written.
2017-05-02 17:04:55 +02:00
*
* @return int
2017-05-02 17:04:55 +02:00
*/
public function write($string)
2017-05-02 17:04:55 +02:00
{
throw new RuntimeException('This stream is not writeable.');
}
2017-05-02 17:04:55 +02:00
/**
* Get the size of the stream if known.
*
* @return null
*/
public function getSize()
{
return null;
}
/**
* Returns whether or not the stream is seekable.
*
* @return boolean
*/
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.
*
* @return boolean
*/
public function isWritable()
{
return false;
}
/**
* Returns whether or not the stream is readable.
*
* @return boolean
*/
public function isReadable()
{
2017-05-02 17:04:55 +02:00
return true;
}
/**
* Returns the remaining contents in a string.
2017-05-02 17:04:55 +02:00
*
* @return string
*/
public function getContents()
{
return stream_get_contents($this->buffer);
}
/**
* Get stream metadata as an associative array or retrieve a specific key.
*
* @param string $key string $key Specific metadata to retrieve.
*
* @return null
2017-05-02 17:04:55 +02:00
*/
public function getMetadata($key = null)
2017-05-02 17:04:55 +02:00
{
return null;
2017-05-02 17:04:55 +02:00
}
/**
* Separates any underlying resources from the stream.
2017-05-02 17:04:55 +02:00
*
* @return resource
2017-05-02 17:04:55 +02:00
*/
public function detach()
2017-05-02 17:04:55 +02:00
{
$stream = $this->buffer;
$this->close();
return $stream;
2017-05-02 17:04:55 +02:00
}
/**
* Reads all data from the stream into a string, from the beginning to end.
*
* @return string
*/
public function __toString()
{
$string = '';
foreach ($this->files as $file) {
$string .= $file['url'];
}
return $string;
}
/**
* Returns the current position of the file read/write pointer
2017-05-02 17:04:55 +02:00
*
* @return int|false
2017-05-02 17:04:55 +02:00
*/
public function tell()
2017-05-02 17:04:55 +02:00
{
return ftell($this->buffer);
}
/**
* Seek to a position in the stream.
2017-05-02 17:04:55 +02:00
*
* @param int $offset Offset
* @param int $whence Specifies how the cursor position will be calculated
2017-05-02 17:04:55 +02:00
*
* @return void
2017-05-02 17:04:55 +02:00
*/
public function seek($offset, $whence = SEEK_SET)
2017-05-02 17:04:55 +02:00
{
throw new RuntimeException('This stream is not seekable.');
2017-05-02 17:04:55 +02:00
}
/**
* Returns true if the stream is at the end of the stream.
2017-05-02 17:04:55 +02:00
*
* @return bool
*/
public function eof()
2017-05-02 17:04:55 +02:00
{
foreach ($this->files as $file) {
if (!$file['complete']) {
return false;
}
}
return true;
}
/**
* Read data from the stream.
2017-05-02 17:04:55 +02:00
*
* @param int $count Number of bytes to read
*
* @return string|false
2017-05-02 17:04:55 +02:00
*/
public function read($count)
2017-05-02 17:04:55 +02:00
{
if (!$this->files[$this->curFile]['headersSent']) {
$urls = $this->download->getURL($this->files[$this->curFile]['url'], $this->format);
2017-05-02 17:04:55 +02:00
$response = $this->client->request('GET', $urls[0], ['stream' => true]);
$contentLengthHeaders = $response->getHeader('Content-Length');
$this->init_file_stream_transfer(
$this->download->getFilename($this->files[$this->curFile]['url'], $this->format),
$contentLengthHeaders[0]
);
$this->files[$this->curFile]['headersSent'] = true;
$this->files[$this->curFile]['stream'] = $response->getBody();
} elseif (!$this->files[$this->curFile]['stream']->eof()) {
$this->stream_file_part($this->files[$this->curFile]['stream']->read($count));
} elseif (!$this->files[$this->curFile]['complete']) {
$this->complete_file_stream();
$this->files[$this->curFile]['complete'] = true;
} elseif (isset($this->files[$this->curFile])) {
$this->curFile += 1;
}
return fread($this->buffer, $count);
}
2019-04-20 00:37:49 +02:00
/**
* Closes the stream and any underlying resources.
2019-04-20 00:37:49 +02:00
*
* @return void
*/
public function close()
2019-04-20 00:37:49 +02:00
{
if (is_resource($this->buffer)) {
fclose($this->buffer);
}
foreach ($this->files as $file) {
if (is_resource($file['stream'])) {
fclose($file['stream']);
}
}
2019-04-20 00:37:49 +02:00
}
2017-05-02 17:04:55 +02:00
}