Changes 0128092025

This commit is contained in:
Simon Pocrnjič
2025-09-28 11:05:00 +02:00
parent a913cfc381
commit 765beb78b7
15 changed files with 480 additions and 150 deletions
+37
View File
@@ -15,6 +15,12 @@ class GenerateDocumentPreview implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Max seconds this job may run before the worker kills it.
* Does not speed up the job, but protects the queue and surfaces timeouts.
*/
public $timeout = 180; // 3 minutes
public function __construct(public int $documentId)
{
}
@@ -30,6 +36,22 @@ public function handle(): void
return;
$ext = strtolower(pathinfo($doc->original_name ?: $doc->file_name, PATHINFO_EXTENSION));
// If a preview was already generated after the document was last updated, skip re-generation
if ($doc->preview_generated_at && $doc->updated_at && $doc->preview_path) {
$previewDisk = config('files.preview_disk', 'public');
if (Storage::disk($previewDisk)->exists($doc->preview_path)) {
if ($doc->updated_at->lte($doc->preview_generated_at)) {
Log::info('Skipping preview generation (already up to date)', [
'document_id' => $doc->id,
'preview_path' => $doc->preview_path,
'updated_at' => (string) $doc->updated_at,
'preview_generated_at' => (string) $doc->preview_generated_at,
]);
return;
}
}
}
if (!in_array($ext, ['doc', 'docx']))
return; // only convert office docs here
@@ -53,6 +75,14 @@ public function handle(): void
// Run soffice headless to convert to PDF
$binCfg = config('files.libreoffice_bin');
$bin = $binCfg ? (string) $binCfg : 'soffice';
// If an absolute path is configured, ensure it exists to avoid long PATH resolution delays
if ($binCfg && preg_match('/^[a-zA-Z]:\\\\|^\//', $bin) && !file_exists($bin)) {
Log::warning('Configured LibreOffice binary not found; falling back to PATH', [
'document_id' => $doc->id,
'bin' => $bin,
]);
$bin = 'soffice';
}
// Windows quoting differs from POSIX. Build command parts safely.
$isWin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
if ($isWin) {
@@ -81,6 +111,7 @@ public function handle(): void
// Capture stderr as well for diagnostics
$cmdWithStderr = $cmd . ' 2>&1';
$t0 = microtime(true);
Log::info('Starting LibreOffice preview conversion', [
'document_id' => $doc->id,
'cmd' => $cmd,
@@ -99,6 +130,7 @@ public function handle(): void
@unlink($tmpIn);
return;
}
$elapsed = (int) round((microtime(true) - $t0) * 1000);
$pdfPathLocal = $tmpIn . '.pdf';
// LibreOffice writes output with source filename base; derive path
@@ -133,6 +165,11 @@ public function handle(): void
$doc->preview_mime = 'application/pdf';
$doc->preview_generated_at = now();
$doc->save();
Log::info('Preview generated and stored', [
'document_id' => $doc->id,
'preview_path' => $doc->preview_path,
'elapsed_ms' => $elapsed,
]);
}
@unlink($tmpIn);