From 30a65308d88e5cfc4d702f6f839229f0913b4d0e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 16 Jan 2026 18:21:00 +0100 Subject: [PATCH] wip #7894 @2 --- app/Fluidbook/Compiler/Compiler.php | 2 +- app/Fluidbook/Link/LinksData.php | 68 ++++++++++++++++++++++++-- app/Jobs/FluidbookImagesPreprocess.php | 60 +++++++++-------------- app/Models/FluidbookPublication.php | 6 +-- app/SubForms/Link/Base.php | 2 +- 5 files changed, 93 insertions(+), 45 deletions(-) diff --git a/app/Fluidbook/Compiler/Compiler.php b/app/Fluidbook/Compiler/Compiler.php index 3800bca6c..5c3929651 100644 --- a/app/Fluidbook/Compiler/Compiler.php +++ b/app/Fluidbook/Compiler/Compiler.php @@ -313,7 +313,7 @@ class Compiler extends Base implements CompilerInterface, IVirtualDirectoryError * @throws \Exception */ - function __construct(FluidbookPublication $book, $variant = 'online', $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, FluidbookTheme $theme = null, $hybrid = false, Command $command = null) + function __construct(FluidbookPublication $book, $variant = 'online', $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, ?FluidbookTheme $theme, $hybrid = false, ?Command $command) { ExcelToArray::setCache($book->protected_path('fluidbookpublication/cache/exceltoarray')); YoutubeDL::setCookiesFile(resource_path('yt-dlp/cookies.txt'), 'firefox'); diff --git a/app/Fluidbook/Link/LinksData.php b/app/Fluidbook/Link/LinksData.php index a87bea79c..6142c98d1 100644 --- a/app/Fluidbook/Link/LinksData.php +++ b/app/Fluidbook/Link/LinksData.php @@ -2,6 +2,8 @@ namespace App\Fluidbook\Link; +use App\Fluidbook\Compiler\Compiler; +use App\Jobs\OCR; use App\Models\FluidbookPublication; use App\Models\User; use App\SubForms\Link\Base; @@ -624,9 +626,10 @@ class LinksData } } if (!isset(static::$_names[$u])) { - try { - static::$_names[$u] = User::find($u)->name; - } catch (\Exception $e) { + $u=User::find($u); + if($u){ + static::$_names[$u] = $u->name; + } else { static::$_names[$u] = '-'; } } @@ -789,4 +792,63 @@ class LinksData self::getLinksAndRulers($source, $links, $rulers, $sourceVersion); self::saveLinksInFile($dest, backpack_user()->id, __('Copier les liens à partir du fluidbook :fluidbook', ['fluidbook' => $source]), $links, $rulers); } + + /** + * @param $book FluidbookPublication + * @param $sync bool + * @param bool $updateLinks + * @return OCR[] + * @throws \Exception + */ + public static function ocrLinksImages($book, $sync = false, $updateLinks = false) + { + $res = []; + if (!($book instanceof FluidbookPublication)) { + $book = FluidbookPublication::withoutGlobalScopes()->find($book); + } + $book->getLinksAndRulers($links, $rulers); + $compiler = new Compiler($book); + $i = 0; + $linksToUpdate = []; + foreach ($links as $link) { + $l = Link::getInstance($i, $link, $compiler); + if (null === $l) { + continue; + } + $jobs = $compiler->getLinkAlternativeText($l, true); + if (!$jobs) { + continue; + } + $linksToUpdate[] = ['data' => $link, 'object' => $l]; + $res = array_merge($res, $jobs); + + foreach ($jobs as $job) { + if ($sync) { + $job->handle(); + } else { + dispatch($job); + } + } + $i++; + } + + if ($sync && $updateLinks) { + $change = false; + foreach ($linksToUpdate as $link) { + $d = $link['data']; + $o = $link['object']; + if ($d['alt'] !== '') { + continue; + } + $alt = $compiler->getLinkAlternativeText($o); + if (!$alt) { + continue; + } + $change = true; + $links[$d['id']]['alt'] = $alt; + } + self::saveLinksInFile($book->id, backpack_user() ?? backpack_user()->id, 'Alternative contents automatically ocerized', $links, $rulers); + } + return $res; + } } diff --git a/app/Jobs/FluidbookImagesPreprocess.php b/app/Jobs/FluidbookImagesPreprocess.php index 9b8cb5fc3..05378c8f7 100644 --- a/app/Jobs/FluidbookImagesPreprocess.php +++ b/app/Jobs/FluidbookImagesPreprocess.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Fluidbook\Compiler\Compiler; +use App\Fluidbook\Link\LinksData; use App\Models\FluidbookPublication; use Cubist\Util\ArrayUtil; use Fluidbook\Tools\Links\Link; @@ -36,6 +37,9 @@ class FluidbookImagesPreprocess extends Base parent::__construct(); } + /** + * @throws \Exception + */ public function handle() { $this->book = FluidbookPublication::withoutGlobalScopes()->find($this->book_id); @@ -66,51 +70,34 @@ class FluidbookImagesPreprocess extends Base $imdir = 'html'; } -// for ($page = 1; $page <= $this->book->getPagesNumber(); $page++) { -// $thisrasterize = in_array($page, $rasterizePages); -// $thisimagesvg = !$thisrasterize && $svg; -// $pageBackgroundsToGenerate = $thisrasterize ? [true] : $globalBackgroundToGenerate; -// foreach ($resolutions as $r) { -// foreach ($pageBackgroundsToGenerate as $withText) { -// $this->getFile($page, $settings->imageFormat, $r, $withText, true, $transparent, $imdir); -// } -// } -// if ($thisimagesvg) { -// $this->getFile($page, 'svg', 150, true, in_array($page, $vectorPages), false, 'html'); -// } -// $this->getFile($page, $settings->imageFormat, 'thumb'); -// } - - $this->book->getLinksAndRulers($links, $rulers); - $compiler = new Compiler($this->book); - $i = 0; - foreach ($links as $link) { - $l = Link::getInstance($i, $link, $compiler); - if (null === $l) { - continue; - } - $jobs = $compiler->getLinkAlternativeText($l, true); - if (!$jobs) { - continue; - } - $this->jobs = array_merge($this->jobs, $jobs); - - foreach ($jobs as $job) { - if ($this->_sync) { - $job->handle(); - } else { - dispatch($job); + for ($page = 1; $page <= $this->book->getPagesNumber(); $page++) { + $thisrasterize = in_array($page, $rasterizePages); + $thisimagesvg = !$thisrasterize && $svg; + $pageBackgroundsToGenerate = $thisrasterize ? [true] : $globalBackgroundToGenerate; + foreach ($resolutions as $r) { + foreach ($pageBackgroundsToGenerate as $withText) { + $this->getFile($page, $settings->imageFormat, $r, $withText, true, $transparent, $imdir); } } - $i++; + if ($thisimagesvg) { + $this->getFile($page, 'svg', 150, true, in_array($page, $vectorPages), false, 'html'); + } + $this->getFile($page, $settings->imageFormat, 'thumb'); } + $this->jobs = array_merge($this->jobs, LinksData::ocrLinksImages($this->book, $this->_sync, false)); + while (true) { if ($this->_checkJobs()) { + dispatch(function () { + LinksData::ocrLinksImages($this->book, true, true); + }); return; } usleep(0.25 * 1000000); } + + } protected function _checkJobs() @@ -119,7 +106,6 @@ class FluidbookImagesPreprocess extends Base $done = 0; $error = 0; - foreach ($this->jobs as $job) { if ($job->isDone()) { $done++; @@ -127,6 +113,7 @@ class FluidbookImagesPreprocess extends Base $error++; } } + $finished = $done + $error; $state = $finished . '(--' . $error . ') /' . $nbjobs; @@ -146,7 +133,6 @@ class FluidbookImagesPreprocess extends Base if ($job->isError()) { Log::error('This file failed ' . $job->getPath()); } - //Log::error('This file is stuck ' . $job->getPath()); } return true; } diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index a5e380eb8..16e95210b 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -951,7 +951,7 @@ class FluidbookPublication extends ToolboxStatusModel return true; } - protected function _baseReplicate(array $except = null) + protected function _baseReplicate(?array $except) { $current = $this; if ($this->syncFromWorkshop()) { @@ -971,7 +971,7 @@ class FluidbookPublication extends ToolboxStatusModel return $new; } - public function replicateFull(array $except = null) + public function replicateFull(?array $except) { $new = $this->_baseReplicate($except); if (!$this->composition_fluidbook && $this->version >= 3) { @@ -988,7 +988,7 @@ class FluidbookPublication extends ToolboxStatusModel return $new; } - public function replicateSettings(array $except = null) + public function replicateSettings(?array $except) { $new = $this->_baseReplicate(array_merge($except ?? [], ['composition'])); $new->composition = []; diff --git a/app/SubForms/Link/Base.php b/app/SubForms/Link/Base.php index fb5869286..d3c274702 100644 --- a/app/SubForms/Link/Base.php +++ b/app/SubForms/Link/Base.php @@ -355,7 +355,7 @@ class Base extends Form * @return string */ - public function render(callable $callback = null) + public function render(?callable $callback) { return view('fluidbook_publication.link_editor_form', ['form' => $this, 'crud' => $this->crud])->render($callback); } -- 2.39.5