first
This commit is contained in:
35
app/Services/AuditLogService.php
Normal file
35
app/Services/AuditLogService.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\AuditLog;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class AuditLogService
|
||||
{
|
||||
public function __construct(private readonly Request $request) {}
|
||||
|
||||
public function log(
|
||||
string $action,
|
||||
array $options = []
|
||||
): AuditLog {
|
||||
$actor = Auth::user();
|
||||
|
||||
return AuditLog::create([
|
||||
'actor_user_id' => $actor?->id ?? $options['actor_user_id'] ?? null,
|
||||
'actor_role' => $actor?->role ?? $options['actor_role'] ?? 'system',
|
||||
'action' => $action,
|
||||
'subject_type' => $options['subject_type'] ?? null,
|
||||
'subject_id' => $options['subject_id'] ?? null,
|
||||
'target_user_id'=> $options['target_user_id'] ?? null,
|
||||
'project_id' => $options['project_id'] ?? null,
|
||||
'old_values' => $options['old_values'] ?? null,
|
||||
'new_values' => $options['new_values'] ?? null,
|
||||
'justification' => $options['justification'] ?? null,
|
||||
'ip_address' => $this->request->ip(),
|
||||
'user_agent' => $this->request->userAgent(),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
71
app/Services/OllamaService.php
Normal file
71
app/Services/OllamaService.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class OllamaService
|
||||
{
|
||||
private string $baseUrl;
|
||||
private string $model;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->baseUrl = rtrim(config('speech2text.ollama.base_url'), '/');
|
||||
$this->model = config('speech2text.ollama.model');
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean and improve raw transcript text using a local Ollama model.
|
||||
* Returns corrected text, or null if Ollama is unavailable.
|
||||
*/
|
||||
public function cleanTranscript(string $rawText, string $language = 'ms'): ?string
|
||||
{
|
||||
$langLabel = $language === 'ms' ? 'Bahasa Melayu' : 'English';
|
||||
|
||||
$prompt = <<<PROMPT
|
||||
Anda adalah pembantu penyuntingan teks transkripsi automatik dalam {$langLabel}.
|
||||
|
||||
Tugas anda:
|
||||
1. Betulkan ejaan dan tanda baca
|
||||
2. Pisahkan ayat dengan betul
|
||||
3. Kekalkan maksud asal — JANGAN tukar kandungan atau tambah maklumat baru
|
||||
4. Kembalikan hanya teks yang telah diperbetulkan, tiada penjelasan tambahan
|
||||
|
||||
Teks transkripsi:
|
||||
{$rawText}
|
||||
PROMPT;
|
||||
|
||||
try {
|
||||
$response = Http::timeout(120)
|
||||
->post("{$this->baseUrl}/api/generate", [
|
||||
'model' => $this->model,
|
||||
'prompt' => $prompt,
|
||||
'stream' => false,
|
||||
]);
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::warning("OllamaService: HTTP {$response->status()} dari {$this->baseUrl}");
|
||||
return null;
|
||||
}
|
||||
|
||||
$text = trim($response->json('response') ?? '');
|
||||
|
||||
return $text !== '' ? $text : null;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning("OllamaService tidak tersedia: {$e->getMessage()}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
try {
|
||||
return Http::timeout(5)->get("{$this->baseUrl}/api/tags")->successful();
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
app/Services/StorageService.php
Normal file
45
app/Services/StorageService.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class StorageService
|
||||
{
|
||||
private const DISK = 'private';
|
||||
|
||||
public function storeAudio(UploadedFile $file, string $uuid): string
|
||||
{
|
||||
$ext = $file->getClientOriginalExtension();
|
||||
$filename = Str::random(40) . '.' . strtolower($ext);
|
||||
$dir = "transcriptions/{$uuid}/audio";
|
||||
|
||||
Storage::disk(self::DISK)->putFileAs($dir, $file, $filename);
|
||||
|
||||
return "{$dir}/{$filename}";
|
||||
}
|
||||
|
||||
public function delete(string $path): void
|
||||
{
|
||||
if (Storage::disk(self::DISK)->exists($path)) {
|
||||
Storage::disk(self::DISK)->delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function exists(string $path): bool
|
||||
{
|
||||
return Storage::disk(self::DISK)->exists($path);
|
||||
}
|
||||
|
||||
public function size(string $path): int
|
||||
{
|
||||
return Storage::disk(self::DISK)->size($path);
|
||||
}
|
||||
|
||||
public function readStream(string $path)
|
||||
{
|
||||
return Storage::disk(self::DISK)->readStream($path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user