2017-05-02 15:04:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PlaylistArchiveStream class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
|
|
|
use Barracuda\ArchiveStream\TarArchive;
|
2018-02-05 15:48:58 +00:00
|
|
|
use GuzzleHttp\Client;
|
2019-04-20 22:34:12 +00:00
|
|
|
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.
|
|
|
|
*
|
2019-04-20 22:34:12 +00:00
|
|
|
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
class PlaylistArchiveStream extends TarArchive implements StreamInterface
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Files to add in the archive.
|
|
|
|
*
|
|
|
|
* @var array[]
|
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
private $files = [];
|
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.
|
|
|
|
*
|
2018-02-05 15:48:58 +00:00
|
|
|
* @var Client
|
2017-05-02 15:04:55 +00: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 00:12:47 +00:00
|
|
|
*
|
|
|
|
* @param Config $config Config instance.
|
2019-04-20 22:34:12 +00:00
|
|
|
* @param stdClass $video Video object returned by youtube-dl
|
|
|
|
* @param string $format Requested format
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function __construct(Config $config, stdClass $video, $format)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2018-02-05 15:48:58 +00:00
|
|
|
$this->client = new Client();
|
2017-11-11 12:58:55 +00:00
|
|
|
$this->download = new VideoDownload($config);
|
2019-04-20 22:34:12 +00:00
|
|
|
|
|
|
|
$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 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);
|
2019-04-19 23:19:55 +00:00
|
|
|
|
|
|
|
// Add data to the buffer.
|
2017-05-02 15:04:55 +00:00
|
|
|
fwrite($this->buffer, $data);
|
2017-12-05 14:49:13 +00:00
|
|
|
if ($pos !== false) {
|
2019-04-20 22:34:12 +00:00
|
|
|
// Rewind so that read() can later read this data.
|
2017-12-05 14:49:13 +00:00
|
|
|
fseek($this->buffer, $pos);
|
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Write data to the stream.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-20 22:34:12 +00:00
|
|
|
* @param string $string The string that is to be written.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-20 22:34:12 +00:00
|
|
|
* @return int
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function write($string)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2019-04-20 22:34:12 +00:00
|
|
|
throw new RuntimeException('This stream is not writeable.');
|
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
|
2019-04-20 22:34:12 +00: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 15:04:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Returns the remaining contents in a string.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-20 22:34:12 +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.
|
|
|
|
*
|
|
|
|
* @param string $key string $key Specific metadata to retrieve.
|
|
|
|
*
|
|
|
|
* @return null
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function getMetadata($key = null)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2019-04-20 22:34:12 +00:00
|
|
|
return null;
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Separates any underlying resources from the stream.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-20 22:34:12 +00:00
|
|
|
* @return resource
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function detach()
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2019-04-20 22:34:12 +00:00
|
|
|
$stream = $this->buffer;
|
|
|
|
$this->close();
|
|
|
|
return $stream;
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00: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 15:04:55 +00:00
|
|
|
*
|
2017-12-05 14:49:13 +00:00
|
|
|
* @return int|false
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function tell()
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
|
|
|
return ftell($this->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Seek to a position in the stream.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
|
|
|
* @param int $offset Offset
|
2019-04-20 22:34:12 +00:00
|
|
|
* @param int $whence Specifies how the cursor position will be calculated
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-20 22:34:12 +00:00
|
|
|
* @return void
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function seek($offset, $whence = SEEK_SET)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2019-04-20 22:34:12 +00:00
|
|
|
throw new RuntimeException('This stream is not seekable.');
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Returns true if the stream is at the end of the stream.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function eof()
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
|
|
|
foreach ($this->files as $file) {
|
|
|
|
if (!$file['complete']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Read data from the stream.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
|
|
|
* @param int $count Number of bytes to read
|
|
|
|
*
|
2017-12-05 14:49:13 +00:00
|
|
|
* @return string|false
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function read($count)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
|
|
|
if (!$this->files[$this->curFile]['headersSent']) {
|
2019-03-30 17:21:45 +00:00
|
|
|
$urls = $this->download->getURL($this->files[$this->curFile]['url'], $this->format);
|
2017-05-02 15:04:55 +00: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-19 22:37:49 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-20 22:34:12 +00:00
|
|
|
* Closes the stream and any underlying resources.
|
2019-04-19 22:37:49 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-04-20 22:34:12 +00:00
|
|
|
public function close()
|
2019-04-19 22:37:49 +00:00
|
|
|
{
|
|
|
|
if (is_resource($this->buffer)) {
|
|
|
|
fclose($this->buffer);
|
|
|
|
}
|
2019-04-20 22:34:12 +00:00
|
|
|
foreach ($this->files 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
|
|
|
}
|