From e43cef1f71a417829430fd6ee2a817807595d779 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 3 Jan 2024 12:34:45 +0100 Subject: [PATCH] wait #6598 @0.5 --- app/Fluidbook/Farm.php | 24 +++++++++++++++++++++--- app/Models/FluidbookDocument.php | 4 ++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/Fluidbook/Farm.php b/app/Fluidbook/Farm.php index 9451ccf9b..ae9670212 100644 --- a/app/Fluidbook/Farm.php +++ b/app/Fluidbook/Farm.php @@ -194,9 +194,27 @@ class Farm public static function fixPDF($pdf, $out) { - return self::_getFile(['operation' => 'fixpdf', - 'pdf' => $pdf, - 'out' => $out], 0, false, true); + return static::lock($pdf, 'fix', function () use ($pdf, $out) { + if (file_exists($out) && filesize($out) > 0 && filemtime($out) >= filemtime($pdf)) { + return $out; + } + + return self::_getFile(['operation' => 'fixpdf', + 'pdf' => $pdf, + 'out' => $out], 0, false, true); + }); + } + + public static function lock($pdf, $operation, $callback) + { + $lockFile = $pdf . '.' . $operation . '.lock'; + while (file_exists($lockFile) && filemtime($lockFile) > time() - 3600) { + sleep(30); + } + touch($lockFile); + $res = $callback(); + unlink($lockFile); + return $res; } public static function cutPDF($pdf, $mode, $out) diff --git a/app/Models/FluidbookDocument.php b/app/Models/FluidbookDocument.php index fff829a03..fa9ce4ec7 100644 --- a/app/Models/FluidbookDocument.php +++ b/app/Models/FluidbookDocument.php @@ -193,9 +193,9 @@ class FluidbookDocument extends ToolboxModel { $fixed = $this->getPDFSource('fixed'); $original = $this->getPDFSource('original'); - if (!file_exists($fixed) || filesize($fixed) === 0 || filemtime($fixed) < filemtime($original)) { + Farm::fixPDF($original, $fixed); - } + return $fixed; } -- 2.39.5