for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Uxmp\Core\Component\Io;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Uxmp\Core\Orm\Model\SongInterface;
final class SimpleFileStreamable implements StreamableInterface
{
public function __construct(
private readonly Psr17Factory $psr17Factory
) {
}
public function stream(
ResponseInterface $response,
SongInterface $song,
): ResponseInterface {
$size = $song->getFileSize();
return $response
->withHeader('Content-Type', (string) $song->getMimeType())
->withHeader('Content-Disposition', sprintf('attachment; filename=song%d.mp3', $song->getId()))
->withHeader('Content-Length', (string) $size)
->withHeader('Cache-Control', 'no-cache')
->withHeader('Content-Range', 'bytes 0-'.$size)
//->withHeader('Accept-Ranges', 'bytes') @todo add seek
->withBody(
$this->createStream($song)
);
public function createStream(SongInterface $song): StreamInterface
return $this->psr17Factory->createStreamFromFile($song->getFilename());