40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\followupaudio;
|
|
|
|
/** Do not buffer a 500 MB recording into PHP memory or allow public caching. */
|
|
final class FollowupAudioStream extends \think\Response
|
|
{
|
|
private string $path;
|
|
|
|
public function __construct(string $path, string $extension)
|
|
{
|
|
$this->path = $path;
|
|
$this->init('', 200);
|
|
$mime = ['mp3' => 'audio/mpeg', 'm4a' => 'audio/mp4', 'wav' => 'audio/wav', 'amr' => 'audio/amr'][$extension];
|
|
$this->header([
|
|
'Content-Type' => $mime, 'Content-Length' => (string) filesize($path),
|
|
'Content-Disposition' => 'inline; filename="recording.' . $extension . '"',
|
|
'Cache-Control' => 'private, no-store, max-age=0', 'Pragma' => 'no-cache',
|
|
'X-Content-Type-Options' => 'nosniff', 'Accept-Ranges' => 'none',
|
|
]);
|
|
}
|
|
|
|
protected function sendData(string $data): void
|
|
{
|
|
$stream = fopen($this->path, 'rb');
|
|
if ($stream === false) {
|
|
return;
|
|
}
|
|
try {
|
|
while (!feof($stream) && !connection_aborted()) {
|
|
echo fread($stream, 1048576);
|
|
}
|
|
} finally {
|
|
fclose($stream);
|
|
}
|
|
}
|
|
}
|