koel/app/Services/Streamers/PhpStreamer.php

46 lines
1.7 KiB
PHP
Raw Normal View History

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 DaveRandom\Resume\InvalidRangeHeaderException;
use DaveRandom\Resume\NonExistentFileException;
use DaveRandom\Resume\RangeSet;
use DaveRandom\Resume\ResourceServlet;
use DaveRandom\Resume\SendFileFailureException;
use DaveRandom\Resume\UnreadableFileException;
use DaveRandom\Resume\UnsatisfiableRangeException;
2019-06-30 15:26:27 +00:00
use Symfony\Component\HttpFoundation\Response;
2018-08-30 02:53:18 +00:00
2020-12-22 20:11:22 +00:00
use function DaveRandom\Resume\get_request_header;
2021-01-31 17:49:54 +00:00
class PhpStreamer extends Streamer implements DirectStreamerInterface
2015-12-13 04:42:28 +00:00
{
2020-12-22 20:11:22 +00:00
public function stream(): void
2015-12-13 04:42:28 +00:00
{
2018-08-30 02:53:18 +00:00
try {
$rangeHeader = get_request_header('Range');
// On Safari, "Range" header value can be "bytes=0-1" which breaks streaming.
$rangeHeader = $rangeHeader === 'bytes=0-1' ? 'bytes=0-' : $rangeHeader;
$rangeSet = RangeSet::createFromHeader($rangeHeader);
$resource = new FileResource($this->song->path, mime_content_type($this->song->path));
2018-08-30 02:53:18 +00:00
(new ResourceServlet($resource))->sendResource($rangeSet);
2022-08-08 16:00:59 +00:00
} catch (InvalidRangeHeaderException) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_BAD_REQUEST);
2022-08-08 16:00:59 +00:00
} catch (UnsatisfiableRangeException) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
2022-08-08 16:00:59 +00:00
} catch (NonExistentFileException) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_NOT_FOUND);
2022-08-08 16:00:59 +00:00
} catch (UnreadableFileException) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_INTERNAL_SERVER_ERROR);
2018-08-30 02:53:18 +00:00
} catch (SendFileFailureException $e) {
2019-06-30 15:26:27 +00:00
abort_unless(headers_sent(), Response::HTTP_INTERNAL_SERVER_ERROR);
2018-08-30 02:53:18 +00:00
echo "An error occurred while attempting to send the requested resource: {$e->getMessage()}";
2015-12-13 04:42:28 +00:00
}
exit;
}
}