koel/app/Services/Streamers/PhpStreamer.php

41 lines
1.5 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 {
$rangeSet = RangeSet::createFromHeader(get_request_header('Range'));
2019-06-30 15:26:27 +00:00
$resource = new FileResource($this->song->path);
2018-08-30 02:53:18 +00:00
(new ResourceServlet($resource))->sendResource($rangeSet);
} catch (InvalidRangeHeaderException $e) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_BAD_REQUEST);
2018-08-30 02:53:18 +00:00
} catch (UnsatisfiableRangeException $e) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
2018-08-30 02:53:18 +00:00
} catch (NonExistentFileException $e) {
2019-06-30 15:26:27 +00:00
abort(Response::HTTP_NOT_FOUND);
2018-08-30 02:53:18 +00:00
} catch (UnreadableFileException $e) {
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;
}
}