2017-05-02 15:04:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PlaylistArchiveStream class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube;
|
|
|
|
|
|
|
|
use Barracuda\ArchiveStream\TarArchive;
|
2019-04-21 16:30:02 +00:00
|
|
|
use GuzzleHttp\Psr7\Stream;
|
2019-04-20 22:34:12 +00:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
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
|
|
|
{
|
|
|
|
/**
|
2019-04-20 22:56:12 +00:00
|
|
|
* videos to add in the archive.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var Video[]
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-20 22:56:12 +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;
|
|
|
|
|
|
|
|
/**
|
2019-04-20 22:56:12 +00:00
|
|
|
* Current video being streamed to the archive.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var Stream
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private $curVideoStream;
|
2017-05-02 15:04:55 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-21 16:30:02 +00:00
|
|
|
* True if the archive is complete.
|
2019-04-21 16:35:24 +00:00
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @var bool
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
private $isComplete = false;
|
2017-05-02 15:04:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PlaylistArchiveStream constructor.
|
2017-12-24 00:12:47 +00:00
|
|
|
*
|
2019-04-21 16:30:02 +00:00
|
|
|
* @param Video $video Video/playlist to download
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-21 16:30:02 +00:00
|
|
|
public function __construct(Video $video)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2019-04-20 22:34:12 +00:00
|
|
|
$buffer = fopen('php://temp', 'r+');
|
|
|
|
if ($buffer !== false) {
|
|
|
|
$this->buffer = $buffer;
|
|
|
|
}
|
|
|
|
foreach ($video->entries as $entry) {
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->videos[] = new Video($entry->url);
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
$pos = $this->tell();
|
2019-04-19 23:19:55 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
// Add data to the end of the buffer.
|
|
|
|
$this->seek(0, SEEK_END);
|
|
|
|
$this->write($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.
|
2019-04-21 16:30:02 +00:00
|
|
|
$this->seek($pos);
|
2017-12-05 14:49:13 +00:00
|
|
|
}
|
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-21 16:30:02 +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-21 16:30:02 +00:00
|
|
|
fwrite($this->buffer, $string);
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is seekable.
|
|
|
|
*
|
2019-04-20 22:58:04 +00:00
|
|
|
* @return bool
|
2019-04-20 22:34:12 +00:00
|
|
|
*/
|
|
|
|
public function isSeekable()
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return true;
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek to the beginning of the stream.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function rewind()
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
rewind($this->buffer);
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is writable.
|
|
|
|
*
|
2019-04-20 22:58:04 +00:00
|
|
|
* @return bool
|
2019-04-20 22:34:12 +00:00
|
|
|
*/
|
|
|
|
public function isWritable()
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return true;
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the stream is readable.
|
|
|
|
*
|
2019-04-20 22:58:04 +00:00
|
|
|
* @return bool
|
2019-04-20 22:34:12 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2019-04-20 22:58:04 +00:00
|
|
|
* @param string $key string $key Specific metadata to retrieve.
|
2019-04-20 22:34:12 +00:00
|
|
|
*
|
2019-04-21 17:47:30 +00:00
|
|
|
* @return array|mixed|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-21 16:30:02 +00:00
|
|
|
$meta = stream_get_meta_data($this->buffer);
|
|
|
|
|
|
|
|
if (!isset($key)) {
|
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($meta[$key])) {
|
|
|
|
return $meta[$key];
|
|
|
|
}
|
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();
|
2019-04-20 22:58:04 +00:00
|
|
|
|
2019-04-20 22:34:12 +00:00
|
|
|
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()
|
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->getContents();
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
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-21 16:30:02 +00:00
|
|
|
fseek($this->buffer, $offset, $whence);
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-21 16:30:02 +00:00
|
|
|
* Returns true if the stream is at the end of the archive.
|
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
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
return $this->isComplete && feof($this->buffer);
|
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
|
2019-04-21 16:30:02 +00:00
|
|
|
/**
|
|
|
|
* Start streaming a new video.
|
|
|
|
*
|
|
|
|
* @param Video $video Video to stream
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function startVideoStream(Video $video)
|
|
|
|
{
|
|
|
|
$response = $video->getHttpResponse();
|
|
|
|
|
|
|
|
$this->curVideoStream = $response->getBody();
|
|
|
|
$contentLengthHeaders = $response->getHeader('Content-Length');
|
|
|
|
|
|
|
|
$this->init_file_stream_transfer(
|
|
|
|
$video->getFilename(),
|
|
|
|
$contentLengthHeaders[0]
|
|
|
|
);
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2019-04-21 16:30:02 +00:00
|
|
|
// If the archive is complete, we only read the remaining buffer.
|
|
|
|
if (!$this->isComplete) {
|
|
|
|
if (isset($this->curVideoStream)) {
|
|
|
|
if ($this->curVideoStream->eof()) {
|
|
|
|
// Stop streaming the current video.
|
2019-04-20 22:56:12 +00:00
|
|
|
$this->complete_file_stream();
|
2019-04-21 16:30:02 +00:00
|
|
|
|
|
|
|
$video = next($this->videos);
|
|
|
|
if ($video) {
|
|
|
|
// Start streaming the next video.
|
|
|
|
$this->startVideoStream($video);
|
|
|
|
} else {
|
|
|
|
// No video left.
|
|
|
|
$this->finish();
|
|
|
|
$this->isComplete = true;
|
|
|
|
}
|
2019-04-20 22:56:12 +00:00
|
|
|
} else {
|
2019-04-21 16:30:02 +00:00
|
|
|
// Continue streaming the current video.
|
|
|
|
$this->stream_file_part($this->curVideoStream->read($count));
|
2019-04-20 22:56:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-21 16:30:02 +00:00
|
|
|
// Start streaming the first video.
|
|
|
|
$this->startVideoStream(current($this->videos));
|
2019-04-20 22:56:12 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-21 16:30:02 +00:00
|
|
|
if (isset($this->curVideoStream)) {
|
|
|
|
$this->curVideoStream->close();
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
2019-04-19 22:37:49 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|