* @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');
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;
}
}
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] = '-';
}
}
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;
+ }
}
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;
parent::__construct();
}
+ /**
+ * @throws \Exception
+ */
public function handle()
{
$this->book = FluidbookPublication::withoutGlobalScopes()->find($this->book_id);
$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()
$done = 0;
$error = 0;
-
foreach ($this->jobs as $job) {
if ($job->isDone()) {
$done++;
$error++;
}
}
+
$finished = $done + $error;
$state = $finished . '(--' . $error . ') /' . $nbjobs;
if ($job->isError()) {
Log::error('This file failed ' . $job->getPath());
}
- //Log::error('This file is stuck ' . $job->getPath());
}
return true;
}
return true;
}
- protected function _baseReplicate(array $except = null)
+ protected function _baseReplicate(?array $except)
{
$current = $this;
if ($this->syncFromWorkshop()) {
return $new;
}
- public function replicateFull(array $except = null)
+ public function replicateFull(?array $except)
{
$new = $this->_baseReplicate($except);
if (!$this->composition_fluidbook && $this->version >= 3) {
return $new;
}
- public function replicateSettings(array $except = null)
+ public function replicateSettings(?array $except)
{
$new = $this->_baseReplicate(array_merge($except ?? [], ['composition']));
$new->composition = [];
* @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);
}