first
This commit is contained in:
112
app/Http/Controllers/User/TranscriptController.php
Normal file
112
app/Http/Controllers/User/TranscriptController.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\User;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\User\UpdateTranscriptRequest;
|
||||
use App\Http\Requests\User\UploadExternalTranscriptRequest;
|
||||
use App\Jobs\OllamaPostProcessJob;
|
||||
use App\Models\TranscriptVersion;
|
||||
use App\Models\TranscriptionProject;
|
||||
use App\Services\AuditLogService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class TranscriptController extends Controller
|
||||
{
|
||||
/**
|
||||
* Simpan perubahan teks transkripsi dan cipta versi baru.
|
||||
*/
|
||||
public function update(
|
||||
UpdateTranscriptRequest $request,
|
||||
TranscriptionProject $project,
|
||||
AuditLogService $audit
|
||||
): RedirectResponse {
|
||||
$this->authorize('editTranscript', $project);
|
||||
|
||||
$oldText = $project->transcript_text;
|
||||
|
||||
$nextVersion = ($project->transcriptVersions()->max('version_number') ?? 0) + 1;
|
||||
|
||||
TranscriptVersion::create([
|
||||
'project_id' => $project->id,
|
||||
'edited_by' => Auth::id(),
|
||||
'version_number' => $nextVersion,
|
||||
'old_text' => $oldText,
|
||||
'new_text' => $request->transcript_text,
|
||||
'change_summary' => $request->change_summary ?? 'Dikemaskini oleh pengguna',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$project->update([
|
||||
'transcript_text' => $request->transcript_text,
|
||||
'transcription_status' => 'completed',
|
||||
]);
|
||||
|
||||
$audit->log('transcript_edited', [
|
||||
'project_id' => $project->id,
|
||||
'new_values' => ['version_number' => $nextVersion],
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Transkripsi berjaya disimpan.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Muat naik fail transkripsi .txt dari luar sistem.
|
||||
*/
|
||||
public function uploadExternal(
|
||||
UploadExternalTranscriptRequest $request,
|
||||
TranscriptionProject $project,
|
||||
AuditLogService $audit
|
||||
): RedirectResponse {
|
||||
$this->authorize('editTranscript', $project);
|
||||
|
||||
$text = file_get_contents($request->file('transcript_file')->getRealPath());
|
||||
|
||||
// Bersihkan BOM dan normalkan line endings
|
||||
$text = preg_replace('/^\xef\xbb\xbf/', '', $text);
|
||||
$text = str_replace(["\r\n", "\r"], "\n", $text);
|
||||
$text = trim($text);
|
||||
|
||||
if ($text === '') {
|
||||
return back()->withErrors(['transcript_file' => 'Fail transkripsi kosong.']);
|
||||
}
|
||||
|
||||
$oldText = $project->transcript_text;
|
||||
$nextVersion = ($project->transcriptVersions()->max('version_number') ?? 0) + 1;
|
||||
|
||||
TranscriptVersion::create([
|
||||
'project_id' => $project->id,
|
||||
'edited_by' => Auth::id(),
|
||||
'version_number' => $nextVersion,
|
||||
'old_text' => $oldText,
|
||||
'new_text' => $text,
|
||||
'change_summary' => 'Transkripsi dimuat naik secara manual',
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
$project->update([
|
||||
'transcript_text' => $text,
|
||||
'transcription_status' => 'completed',
|
||||
'transcription_engine' => 'manual',
|
||||
'processed_at' => $project->processed_at ?? now(),
|
||||
]);
|
||||
|
||||
$audit->log('transcript_uploaded_external', [
|
||||
'project_id' => $project->id,
|
||||
'new_values' => ['version_number' => $nextVersion],
|
||||
]);
|
||||
|
||||
// Hantar ke Ollama jika pengguna minta dan Ollama dikonfigurasi
|
||||
if ($request->boolean('clean_with_ollama') && config('speech2text.ollama.enabled')) {
|
||||
OllamaPostProcessJob::dispatch($project);
|
||||
}
|
||||
|
||||
$msg = 'Transkripsi berjaya dimuat naik.';
|
||||
if ($request->boolean('clean_with_ollama') && config('speech2text.ollama.enabled')) {
|
||||
$msg .= ' Pembersihan Ollama sedang diproses.';
|
||||
}
|
||||
|
||||
return back()->with('success', $msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user