Files
speech2text/app/Http/Controllers/User/AudioController.php
2026-06-02 17:35:45 +08:00

41 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\TranscriptionProject;
use App\Services\StorageService;
use Symfony\Component\HttpFoundation\StreamedResponse;
class AudioController extends Controller
{
public function __construct(private readonly StorageService $storage) {}
public function stream(TranscriptionProject $project): StreamedResponse
{
$this->authorize('viewAudio', $project);
$path = $project->stored_audio_path;
abort_unless($this->storage->exists($path), 404);
$size = $this->storage->size($path);
$mimeType = $project->mime_type;
return response()->stream(function () use ($path) {
$stream = $this->storage->readStream($path);
if ($stream) {
fpassthru($stream);
fclose($stream);
}
}, 200, [
'Content-Type' => $mimeType,
'Content-Length' => $size,
'Content-Disposition' => 'inline',
'Cache-Control' => 'no-store, no-cache, private, must-revalidate',
'X-Content-Type-Options' => 'nosniff',
'Accept-Ranges' => 'bytes',
]);
}
}