Simplify PSR-4 autoload
This commit is contained in:
parent
65ccf95437
commit
d127964eff
12 changed files with 81 additions and 84 deletions
36
classes/Stream/ConvertedPlaylistArchiveStream.php
Normal file
36
classes/Stream/ConvertedPlaylistArchiveStream.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* ConvertedPlaylistArchiveStream class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Stream;
|
||||
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Alltube\Library\Video;
|
||||
use Slim\Http\Stream;
|
||||
|
||||
/**
|
||||
* Class used to create a Zip archive from converted playlists entries.
|
||||
*/
|
||||
class ConvertedPlaylistArchiveStream extends PlaylistArchiveStream
|
||||
{
|
||||
/**
|
||||
* Start streaming a new video.
|
||||
*
|
||||
* @param Video $video Video to stream
|
||||
*
|
||||
* @return void
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
protected function startVideoStream(Video $video)
|
||||
{
|
||||
$this->curVideoStream = new Stream($this->downloader->getAudioStream($video));
|
||||
|
||||
$this->init_file_stream_transfer(
|
||||
$video->getFileNameWithExtension('mp3'),
|
||||
// The ZIP format does not care about the file size.
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
325
classes/Stream/PlaylistArchiveStream.php
Normal file
325
classes/Stream/PlaylistArchiveStream.php
Normal file
|
@ -0,0 +1,325 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PlaylistArchiveStream class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Stream;
|
||||
|
||||
use Alltube\Library\Downloader;
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Alltube\Library\Video;
|
||||
use Barracuda\ArchiveStream\ZipArchive;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
/**
|
||||
* Class used to create a Zip archive from playlists and stream it to the browser.
|
||||
*
|
||||
* @link https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php
|
||||
*/
|
||||
class PlaylistArchiveStream extends ZipArchive implements StreamInterface
|
||||
{
|
||||
/**
|
||||
* videos to add in the archive.
|
||||
*
|
||||
* @var Video[]
|
||||
*/
|
||||
private $videos = [];
|
||||
|
||||
/**
|
||||
* Stream used to store data before it is sent to the browser.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
private $buffer;
|
||||
|
||||
/**
|
||||
* Current video being streamed to the archive.
|
||||
*
|
||||
* @var StreamInterface
|
||||
*/
|
||||
protected $curVideoStream;
|
||||
|
||||
/**
|
||||
* True if the archive is complete.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isComplete = false;
|
||||
|
||||
/**
|
||||
* Downloader object.
|
||||
*
|
||||
* @var Downloader
|
||||
*/
|
||||
protected $downloader;
|
||||
|
||||
/**
|
||||
* PlaylistArchiveStream constructor.
|
||||
*
|
||||
* We don't call the parent constructor because it messes up the output buffering.
|
||||
*
|
||||
* @param Downloader $downloader Downloader object
|
||||
* @param Video $video Video/playlist to download
|
||||
* @noinspection PhpMissingParentConstructorInspection
|
||||
*/
|
||||
public function __construct(Downloader $downloader, Video $video)
|
||||
{
|
||||
$this->downloader = $downloader;
|
||||
|
||||
$buffer = fopen('php://temp', 'r+');
|
||||
if ($buffer !== false) {
|
||||
$this->buffer = $buffer;
|
||||
}
|
||||
foreach ($video->entries as $entry) {
|
||||
$this->videos[] = $downloader->getVideo($entry->url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add data to the archive.
|
||||
*
|
||||
* @param string $data Data
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the stream.
|
||||
*
|
||||
* @param string $string The string that is to be written
|
||||
*
|
||||
* @return int|false
|
||||
*/
|
||||
public function write($string)
|
||||
{
|
||||
return fwrite($this->buffer, $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the stream if known.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is seekable.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isWritable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is readable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remaining contents in a string.
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
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 array|mixed|null
|
||||
*/
|
||||
public function getMetadata($key = null)
|
||||
{
|
||||
$meta = stream_get_meta_data($this->buffer);
|
||||
|
||||
if (!isset($key)) {
|
||||
return $meta;
|
||||
}
|
||||
|
||||
if (isset($meta[$key])) {
|
||||
return $meta[$key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separates any underlying resources from the stream.
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function detach()
|
||||
{
|
||||
$stream = $this->buffer;
|
||||
$this->close();
|
||||
|
||||
return $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all data from the stream into a string, from the beginning to end.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$this->rewind();
|
||||
|
||||
return strval($this->getContents());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current position of the file read/write pointer.
|
||||
*
|
||||
* @return int|false
|
||||
*/
|
||||
public function tell()
|
||||
{
|
||||
return ftell($this->buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to a position in the stream.
|
||||
*
|
||||
* @param int $offset Offset
|
||||
* @param int $whence Specifies how the cursor position will be calculated
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seek($offset, $whence = SEEK_SET)
|
||||
{
|
||||
fseek($this->buffer, $offset, $whence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the stream is at the end of the archive.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function eof()
|
||||
{
|
||||
return $this->isComplete && feof($this->buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start streaming a new video.
|
||||
*
|
||||
* @param Video $video Video to stream
|
||||
*
|
||||
* @return void
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
protected function startVideoStream(Video $video)
|
||||
{
|
||||
$response = $this->downloader->getHttpResponse($video);
|
||||
|
||||
$this->curVideoStream = $response->getBody();
|
||||
$contentLengthHeaders = $response->getHeader('Content-Length');
|
||||
|
||||
$this->init_file_stream_transfer(
|
||||
$video->getFilename(),
|
||||
intval($contentLengthHeaders[0])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the stream.
|
||||
*
|
||||
* @param int $count Number of bytes to read
|
||||
*
|
||||
* @return string|false
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function read($count)
|
||||
{
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
return fread($this->buffer, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the stream and any underlying resources.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (is_resource($this->buffer)) {
|
||||
fclose($this->buffer);
|
||||
}
|
||||
if (isset($this->curVideoStream)) {
|
||||
$this->curVideoStream->close();
|
||||
}
|
||||
}
|
||||
}
|
197
classes/Stream/YoutubeChunkStream.php
Normal file
197
classes/Stream/YoutubeChunkStream.php
Normal file
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* YoutubeChunkStream class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Stream;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
/**
|
||||
* This is a wrapper around GuzzleHttp\Psr7\Stream.
|
||||
* It is required because Youtube HTTP responses are buggy if we try to read further than the end of the response.
|
||||
*/
|
||||
class YoutubeChunkStream implements StreamInterface
|
||||
{
|
||||
/**
|
||||
* HTTP response containing the video chunk.
|
||||
*
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* YoutubeChunkStream constructor.
|
||||
*
|
||||
* @param ResponseInterface $response HTTP response containing the video chunk
|
||||
*/
|
||||
public function __construct(ResponseInterface $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the stream.
|
||||
*
|
||||
* @param int $length Read up to $length bytes from the object and return
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read($length)
|
||||
{
|
||||
$size = intval($this->response->getHeader('Content-Length')[0]);
|
||||
if ($size - $this->tell() < $length) {
|
||||
// Don't try to read further than the end of the stream.
|
||||
$length = $size - $this->tell();
|
||||
}
|
||||
|
||||
return $this->response->getBody()->read($length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads all data from the stream into a string, from the beginning to end.
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (string)$this->response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the stream and any underlying resources.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->response->getBody()->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Separates any underlying resources from the stream.
|
||||
*
|
||||
* @return resource|null
|
||||
*/
|
||||
public function detach()
|
||||
{
|
||||
return $this->response->getBody()->detach();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of the stream if known.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return $this->response->getBody()->getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current position of the file read/write pointer.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function tell()
|
||||
{
|
||||
return $this->response->getBody()->tell();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the stream is at the end of the stream.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function eof()
|
||||
{
|
||||
return $this->response->getBody()->eof();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is seekable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSeekable()
|
||||
{
|
||||
return $this->response->getBody()->isSeekable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to a position in the stream.
|
||||
*
|
||||
* @param int $offset Stream offset
|
||||
* @param int $whence Specifies how the cursor position will be calculated
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function seek($offset, $whence = SEEK_SET)
|
||||
{
|
||||
$this->response->getBody()->seek($offset, $whence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to the beginning of the stream.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->response->getBody()->rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is writable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isWritable()
|
||||
{
|
||||
return $this->response->getBody()->isWritable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the stream.
|
||||
*
|
||||
* @param string $string The string that is to be written
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function write($string)
|
||||
{
|
||||
return $this->response->getBody()->write($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not the stream is readable.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadable()
|
||||
{
|
||||
return $this->response->getBody()->isReadable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remaining contents in a string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContents()
|
||||
{
|
||||
return $this->response->getBody()->getContents();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stream metadata as an associative array or retrieve a specific key.
|
||||
*
|
||||
* @param string $key Specific metadata to retrieve.
|
||||
*
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public function getMetadata($key = null)
|
||||
{
|
||||
return $this->response->getBody()->getMetadata($key);
|
||||
}
|
||||
}
|
45
classes/Stream/YoutubeStream.php
Normal file
45
classes/Stream/YoutubeStream.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* YoutubeStream class.
|
||||
*/
|
||||
|
||||
namespace Alltube\Stream;
|
||||
|
||||
use Alltube\Library\Downloader;
|
||||
use Alltube\Library\Exception\AlltubeLibraryException;
|
||||
use Alltube\Library\Video;
|
||||
use GuzzleHttp\Psr7\AppendStream;
|
||||
|
||||
/**
|
||||
* Stream that downloads a video in chunks.
|
||||
* This is required because Youtube throttles the download speed on chunks larger than 10M.
|
||||
*/
|
||||
class YoutubeStream extends AppendStream
|
||||
{
|
||||
/**
|
||||
* YoutubeStream constructor.
|
||||
*
|
||||
* @param Downloader $downloader Downloader object
|
||||
* @param Video $video Video to stream
|
||||
* @throws AlltubeLibraryException
|
||||
*/
|
||||
public function __construct(Downloader $downloader, Video $video)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$stream = $downloader->getHttpResponse($video);
|
||||
$contentLenghtHeader = $stream->getHeader('Content-Length');
|
||||
$rangeStart = 0;
|
||||
|
||||
while ($rangeStart < $contentLenghtHeader[0]) {
|
||||
$rangeEnd = $rangeStart + $video->downloader_options->http_chunk_size;
|
||||
if ($rangeEnd >= $contentLenghtHeader[0]) {
|
||||
$rangeEnd = intval($contentLenghtHeader[0]) - 1;
|
||||
}
|
||||
$response = $downloader->getHttpResponse($video, ['Range' => 'bytes=' . $rangeStart . '-' . $rangeEnd]);
|
||||
$this->addStream(new YoutubeChunkStream($response));
|
||||
$rangeStart = $rangeEnd + 1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue