alltube/classes/PlaylistArchiveStream.php

302 lines
6.5 KiB
PHP
Raw Normal View History

2017-05-02 15:04:55 +00:00
<?php
/**
* PlaylistArchiveStream class.
*/
namespace Alltube;
use Barracuda\ArchiveStream\ZipArchive;
use Psr\Http\Message\StreamInterface;
2017-05-02 15:04:55 +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
*
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
2017-05-02 15:04:55 +00:00
*/
class PlaylistArchiveStream extends ZipArchive 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 Video[]
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;
/**
* Current video being streamed to the archive.
2017-05-02 15:04:55 +00:00
*
* @var StreamInterface
2017-05-02 15:04:55 +00:00
*/
protected $curVideoStream;
2017-05-02 15:04:55 +00:00
/**
* True if the archive is complete.
2019-04-21 16:35:24 +00:00
*
* @var bool
2017-05-02 15:04:55 +00:00
*/
private $isComplete = false;
2017-05-02 15:04:55 +00:00
/**
* PlaylistArchiveStream constructor.
2017-12-24 00:12:47 +00:00
*
* @param Video $video Video/playlist to download
2017-05-02 15:04:55 +00:00
*/
public function __construct(Video $video)
2017-05-02 15:04:55 +00:00
{
$buffer = fopen('php://temp', 'r+');
if ($buffer !== false) {
$this->buffer = $buffer;
}
foreach ($video->entries as $entry) {
$this->videos[] = new Video($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 = $this->tell();
// Add data to the end of the buffer.
$this->seek(0, SEEK_END);
$this->write($data);
if ($pos !== false) {
// Rewind so that read() can later read this data.
$this->seek($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
{
fwrite($this->buffer, $string);
}
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 true;
}
/**
* Seek to the beginning of the stream.
*
* @return void
*/
public function rewind()
{
rewind($this->buffer);
}
/**
* Returns whether or not the stream is writable.
*
2019-04-20 22:58:04 +00:00
* @return bool
*/
public function isWritable()
{
return true;
}
/**
* 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 array|mixed|null
2017-05-02 15:04:55 +00:00
*/
public function getMetadata($key = null)
2017-05-02 15:04:55 +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
}
/**
* 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()
{
return $this->getContents();
}
/**
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
{
fseek($this->buffer, $offset, $whence);
2017-05-02 15:04:55 +00:00
}
/**
* Returns true if the stream is at the end of the archive.
2017-05-02 15:04:55 +00:00
*
* @return bool
*/
public function eof()
2017-05-02 15:04:55 +00:00
{
return $this->isComplete && feof($this->buffer);
}
2017-05-02 15:04:55 +00:00
/**
* Start streaming a new video.
*
* @param Video $video Video to stream
*
* @return void
*/
protected 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
}
/**
* 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 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.
$this->complete_file_stream();
$video = next($this->videos);
if ($video) {
// Start streaming the next video.
$this->startVideoStream($video);
} else {
// No video left.
$this->finish();
$this->isComplete = true;
}
} else {
// Continue streaming the current video.
$this->stream_file_part($this->curVideoStream->read($count));
}
} else {
// Start streaming the first video.
$this->startVideoStream(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);
}
if (isset($this->curVideoStream)) {
$this->curVideoStream->close();
}
2019-04-19 22:37:49 +00:00
}
2017-05-02 15:04:55 +00:00
}