2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
2017-04-20 11:20:32 +00:00
|
|
|
namespace App\Services\Streamers;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2018-08-30 02:53:18 +00:00
|
|
|
use DaveRandom\Resume\FileResource;
|
|
|
|
use function DaveRandom\Resume\get_request_header;
|
|
|
|
use DaveRandom\Resume\InvalidRangeHeaderException;
|
|
|
|
use DaveRandom\Resume\NonExistentFileException;
|
|
|
|
use DaveRandom\Resume\RangeSet;
|
|
|
|
use DaveRandom\Resume\Resource;
|
|
|
|
use DaveRandom\Resume\ResourceServlet;
|
|
|
|
use DaveRandom\Resume\SendFileFailureException;
|
|
|
|
use DaveRandom\Resume\UnreadableFileException;
|
|
|
|
use DaveRandom\Resume\UnsatisfiableRangeException;
|
|
|
|
|
2018-08-22 17:59:14 +00:00
|
|
|
class PHPStreamer extends Streamer implements DirectStreamerInterface
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-08-30 02:53:18 +00:00
|
|
|
public function stream()
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-08-30 02:53:18 +00:00
|
|
|
try {
|
|
|
|
$rangeSet = RangeSet::createFromHeader(get_request_header('Range'));
|
|
|
|
/** @var Resource $resource */
|
|
|
|
$resource = new FileResource($this->song->path, 'application/octet-stream');
|
|
|
|
(new ResourceServlet($resource))->sendResource($rangeSet);
|
|
|
|
} catch (InvalidRangeHeaderException $e) {
|
|
|
|
abort(400);
|
|
|
|
} catch (UnsatisfiableRangeException $e) {
|
|
|
|
abort(416);
|
|
|
|
} catch (NonExistentFileException $e) {
|
|
|
|
abort(404);
|
|
|
|
} catch (UnreadableFileException $e) {
|
|
|
|
abort(500);
|
|
|
|
} catch (SendFileFailureException $e) {
|
|
|
|
abort_unless(headers_sent(), 500);
|
|
|
|
echo "An error occurred while attempting to send the requested resource: {$e->getMessage()}";
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|