Add Docker stack, Reverb live progress, and duplicate upload detection.

Ship FrankenPHP Compose services with Reverb WebSockets for real-time transcription status, skip re-uploading identical audio via content hash, and format disk usage without requiring intl.
This commit is contained in:
ben
2026-08-12 17:35:48 +02:00
parent 352b564f3a
commit 771ee8db5a
44 changed files with 2581 additions and 424 deletions
@@ -0,0 +1,50 @@
<?php
namespace App\Events;
use App\Models\Recording;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class RecordingTranscriptionUpdated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
public function __construct(public Recording $recording) {}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, Channel>
*/
public function broadcastOn(): array
{
return [
new Channel('recordings'),
];
}
public function broadcastAs(): string
{
return 'RecordingTranscriptionUpdated';
}
/**
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return array_merge(
$this->recording->transcriptionStatusPayload(),
[
'word_count' => $this->recording->word_count,
],
);
}
}
+73 -4
View File
@@ -49,7 +49,19 @@ class RecordingController extends Controller
*/
public function create(): View
{
return view('recordings.create');
$existingFingerprints = Recording::query()
->get(['original_filename', 'file_size_bytes'])
->map(fn (Recording $recording) => $this->uploadFingerprint(
$recording->original_filename,
(int) $recording->file_size_bytes,
))
->unique()
->values()
->all();
return view('recordings.create', [
'existingFingerprints' => $existingFingerprints,
]);
}
/**
@@ -65,24 +77,71 @@ class RecordingController extends Controller
$titleOverride = $request->string('title')->trim()->toString();
$recordings = [];
$skippedDuplicates = 0;
$seenHashes = [];
foreach ($files as $file) {
$hash = hash_file('sha256', $file->getRealPath());
if ($hash === false) {
continue;
}
if (
isset($seenHashes[$hash])
|| Recording::query()->where('content_hash', $hash)->exists()
|| Recording::query()
->where('original_filename', $file->getClientOriginalName())
->where('file_size_bytes', $file->getSize() ?: 0)
->exists()
) {
$skippedDuplicates++;
continue;
}
$seenHashes[$hash] = true;
$title = count($files) === 1 && $titleOverride !== ''
? $titleOverride
: null;
$recordings[] = $this->storeUploadedRecording($file, $metadata, $title);
$recordings[] = $this->storeUploadedRecording($file, $metadata, $hash, $title);
}
if ($recordings === [] && $skippedDuplicates > 0) {
return redirect()
->route('recordings.create')
->with('error', $skippedDuplicates === 1
? 'That file is already uploaded — nothing new was saved.'
: "All {$skippedDuplicates} files were duplicates — nothing new was saved.");
}
if ($recordings === []) {
return redirect()
->route('recordings.create')
->with('error', 'No valid audio files were uploaded.');
}
$message = count($recordings) === 1
? 'Recording uploaded — transcription queued.'
: count($recordings).' recordings uploaded — transcription queued.';
if ($skippedDuplicates > 0) {
$message .= $skippedDuplicates === 1
? ' Skipped 1 duplicate.'
: " Skipped {$skippedDuplicates} duplicates.";
}
if (count($recordings) === 1) {
return redirect()
->route('recordings.show', $recordings[0])
->with('success', 'Recording uploaded — transcription queued.');
->with('success', $message);
}
return redirect()
->route('recordings.index')
->with('success', count($recordings).' recordings uploaded — transcription queued.');
->with('success', $message);
}
/**
@@ -115,6 +174,7 @@ class RecordingController extends Controller
private function storeUploadedRecording(
UploadedFile $file,
Mp3MetadataService $metadata,
string $contentHash,
?string $titleOverride = null,
): Recording {
$path = $file->store('recordings', 'local');
@@ -133,6 +193,7 @@ class RecordingController extends Controller
'artist' => $tags['artist'],
'album' => $tags['album'],
'file_size_bytes' => $file->getSize() ?: 0,
'content_hash' => $contentHash,
'transcription_status' => 'pending',
'transcription_driver' => 'local',
]);
@@ -141,4 +202,12 @@ class RecordingController extends Controller
return $recording->fresh();
}
/**
* Client-side fingerprint for name + size duplicate checks before upload.
*/
private function uploadFingerprint(string $filename, int $sizeBytes): string
{
return strtolower($filename).':'.$sizeBytes;
}
}
+4
View File
@@ -143,6 +143,8 @@ class TranscribeRecording implements ShouldQueue
'transcribed_at' => now(),
])->save();
$this->recording->broadcastTranscriptionUpdated();
return true;
}
@@ -165,6 +167,8 @@ class TranscribeRecording implements ShouldQueue
'transcribed_at' => now(),
])->save();
$this->recording->broadcastTranscriptionUpdated();
return true;
}
+19 -1
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use App\Events\RecordingTranscriptionUpdated;
use App\Jobs\TranscribeRecording;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\Scope;
@@ -28,6 +29,7 @@ class Recording extends Model
'artist',
'album',
'file_size_bytes',
'content_hash',
'transcript',
'transcription_status',
'transcription_progress',
@@ -129,7 +131,9 @@ class Recording extends Model
'transcribed_at' => $this->transcribed_at,
]);
TranscribeRecording::dispatch($this->fresh());
$recording = $this->fresh();
RecordingTranscriptionUpdated::dispatch($recording);
TranscribeRecording::dispatch($recording);
}
/**
@@ -349,6 +353,8 @@ class Recording extends Model
'transcription_percent' => $this->transcription_percent ?: 0,
'transcription_error' => 'Stopped by user',
])->save();
RecordingTranscriptionUpdated::dispatch($this->fresh());
}
/**
@@ -376,6 +382,8 @@ class Recording extends Model
'transcription_percent' => $this->transcription_percent ?: 0,
'transcription_error' => $message,
])->save();
RecordingTranscriptionUpdated::dispatch($this->fresh());
}
/**
@@ -405,6 +413,16 @@ class Recording extends Model
'transcription_percent' => max(0, min(100, $percent)),
'transcription_error' => null,
])->save();
RecordingTranscriptionUpdated::dispatch($this->fresh());
}
/**
* Broadcast the current transcription status to connected browsers.
*/
public function broadcastTranscriptionUpdated(): void
{
RecordingTranscriptionUpdated::dispatch($this->fresh() ?? $this);
}
/**
+26 -4
View File
@@ -4,7 +4,6 @@ namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Number;
class DiskSpaceService
{
@@ -26,6 +25,10 @@ class DiskSpaceService
{
$path ??= Storage::disk('local')->path('');
if (! is_dir($path)) {
@mkdir($path, 0755, true);
}
/** @var array{total_bytes: int, free_bytes: int, used_bytes: int, used_percent: float, free_percent: float, total_human: string, free_human: string, used_human: string}|null */
return Cache::remember(
'disk-space:'.md5($path),
@@ -67,9 +70,28 @@ class DiskSpaceService
'used_bytes' => $usedBytes,
'used_percent' => $usedPercent,
'free_percent' => $freePercent,
'total_human' => Number::fileSize($totalBytes, precision: 1),
'free_human' => Number::fileSize($freeBytes, precision: 1),
'used_human' => Number::fileSize($usedBytes, precision: 1),
'total_human' => $this->formatBytes($totalBytes),
'free_human' => $this->formatBytes($freeBytes),
'used_human' => $this->formatBytes($usedBytes),
];
}
/**
* Human-readable byte size without requiring the intl extension.
*/
private function formatBytes(int $bytes): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$value = (float) max(0, $bytes);
$unit = 0;
while ($value >= 1024 && $unit < count($units) - 1) {
$value /= 1024;
$unit++;
}
$precision = $unit === 0 ? 0 : 1;
return number_format($value, $precision).' '.$units[$unit];
}
}