2017-05-02 15:04:55 +00:00
|
|
|
<?php
|
2019-10-03 19:41:54 +00:00
|
|
|
|
2017-05-02 15:04:55 +00:00
|
|
|
/**
|
|
|
|
* PlaylistArchiveStream class.
|
|
|
|
*/
|
|
|
|
|
2019-04-22 19:06:05 +00:00
|
|
|
namespace Alltube\Stream;
|
2017-05-02 15:04:55 +00:00
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
use Alltube\Library\Downloader;
|
|
|
|
use Alltube\Library\Exception\AlltubeLibraryException;
|
|
|
|
use Alltube\Library\Video;
|
2019-04-21 20:34:45 +00:00
|
|
|
use Barracuda\ArchiveStream\ZipArchive;
|
2019-04-20 22:34:12 +00:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
2017-05-02 15:04:55 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-21 20:34:45 +00:00
|
|
|
* Class used to create a Zip archive from playlists and stream it to the browser.
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
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-21 20:34:45 +00:00
|
|
|
class PlaylistArchiveStream extends ZipArchive 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 20:56:07 +00:00
|
|
|
* @var StreamInterface
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2019-04-21 20:56:07 +00:00
|
|
|
protected $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
|
|
|
|
2020-06-20 23:44:20 +00:00
|
|
|
/**
|
|
|
|
* Downloader object.
|
|
|
|
*
|
|
|
|
* @var Downloader
|
|
|
|
*/
|
|
|
|
protected $downloader;
|
|
|
|
|
2017-05-02 15:04:55 +00:00
|
|
|
/**
|
|
|
|
* PlaylistArchiveStream constructor.
|
2017-12-24 00:12:47 +00:00
|
|
|
*
|
2020-05-13 22:21:25 +00:00
|
|
|
* We don't call the parent constructor because it messes up the output buffering.
|
|
|
|
*
|
2020-06-20 23:44:20 +00:00
|
|
|
* @param Downloader $downloader Downloader object
|
2019-04-21 16:30:02 +00:00
|
|
|
* @param Video $video Video/playlist to download
|
2020-05-13 22:21:25 +00:00
|
|
|
* @noinspection PhpMissingParentConstructorInspection
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2020-06-20 23:44:20 +00:00
|
|
|
public function __construct(Downloader $downloader, Video $video)
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->downloader = $downloader;
|
|
|
|
|
2019-04-20 22:34:12 +00:00
|
|
|
$buffer = fopen('php://temp', 'r+');
|
|
|
|
if ($buffer !== false) {
|
|
|
|
$this->buffer = $buffer;
|
|
|
|
}
|
|
|
|
foreach ($video->entries as $entry) {
|
2020-06-20 23:44:20 +00:00
|
|
|
$this->videos[] = $downloader->getVideo($entry->url);
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data to the archive.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $data Data
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
protected function send($data): void
|
2017-05-02 15:04:55 +00:00
|
|
|
{
|
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
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $string The string that is to be written
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return int|false
|
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
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
return 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.
|
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return int|null
|
2019-04-20 22:34:12 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function getSize(): ?int
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
2020-05-13 20:28:05 +00:00
|
|
|
return null;
|
2019-04-20 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isSeekable(): bool
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
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
|
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function rewind(): void
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
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
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isWritable(): bool
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
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
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function isReadable(): bool
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
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
|
|
|
*
|
2020-05-13 20:28:05 +00:00
|
|
|
* @return string|false
|
2019-04-20 22:34:12 +00:00
|
|
|
*/
|
|
|
|
public function getContents()
|
|
|
|
{
|
|
|
|
return stream_get_contents($this->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get stream metadata as an associative array or retrieve a specific key.
|
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param string|null $key string $key Specific metadata to retrieve.
|
2019-04-20 22:34:12 +00:00
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @return 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];
|
|
|
|
}
|
2020-05-13 19:18:32 +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();
|
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
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function __toString(): string
|
2019-04-20 22:34:12 +00:00
|
|
|
{
|
2019-04-22 19:52:21 +00:00
|
|
|
$this->rewind();
|
|
|
|
|
2020-05-13 20:28:05 +00:00
|
|
|
return strval($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
|
|
|
*
|
2020-09-27 13:53:53 +00:00
|
|
|
* @param mixed $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
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function seek($offset, $whence = SEEK_SET): void
|
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
|
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function eof(): bool
|
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
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2019-04-21 16:30:02 +00:00
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
protected function startVideoStream(Video $video): void
|
2019-04-21 16:30:02 +00:00
|
|
|
{
|
2020-06-20 23:44:20 +00:00
|
|
|
$response = $this->downloader->getHttpResponse($video);
|
2019-04-21 16:30:02 +00:00
|
|
|
|
|
|
|
$this->curVideoStream = $response->getBody();
|
|
|
|
$contentLengthHeaders = $response->getHeader('Content-Length');
|
|
|
|
|
|
|
|
$this->init_file_stream_transfer(
|
|
|
|
$video->getFilename(),
|
2020-05-13 20:28:05 +00:00
|
|
|
intval($contentLengthHeaders[0])
|
2019-04-21 16:30:02 +00:00
|
|
|
);
|
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
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @param mixed $length Number of bytes to read
|
2017-05-02 15:04:55 +00:00
|
|
|
*
|
2017-12-05 14:49:13 +00:00
|
|
|
* @return string|false
|
2020-06-20 23:44:20 +00:00
|
|
|
* @throws AlltubeLibraryException
|
2017-05-02 15:04:55 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function read($length)
|
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.
|
2020-12-17 21:43:05 +00:00
|
|
|
$this->stream_file_part($this->curVideoStream->read($length));
|
2019-04-20 22:56:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-21 16:30:02 +00:00
|
|
|
// Start streaming the first video.
|
2021-02-07 11:42:03 +00:00
|
|
|
$video = current($this->videos);
|
|
|
|
if ($video) {
|
|
|
|
$this->startVideoStream($video);
|
|
|
|
} else {
|
|
|
|
$this->push_error('Playlist was empty');
|
|
|
|
$this->finish();
|
|
|
|
$this->isComplete = true;
|
|
|
|
}
|
2019-04-20 22:56:12 +00:00
|
|
|
}
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:43:05 +00:00
|
|
|
return fread($this->buffer, $length);
|
2017-05-02 15:04:55 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2022-05-28 21:43:07 +00:00
|
|
|
public function close(): void
|
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
|
|
|
}
|