From 6f09526ee33a67b11840421aa5f606a9cda1176b Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 16 Sep 2021 19:17:22 +0200 Subject: [PATCH] wip #4666 @1 --- .gitignore | 1 - .idea/fluidbook_tools.iml | 1 - src/Compiler/Compiler.php | 4 ++- src/Compiler/Source.php | 2 ++ src/Jobs/ProcessFile.php | 2 +- src/Jobs/ProcessPage.php | 75 +++++++++++++++++++++++---------------- src/PDF/Document.php | 7 ++-- 7 files changed, 53 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 1e01c5e..299cf40 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,3 @@ Homestead.json .phpunit.result.cache resources/tools/fwstk/bin -resources/tools/fwstk/out diff --git a/.idea/fluidbook_tools.iml b/.idea/fluidbook_tools.iml index 0ad947c..792cd4f 100644 --- a/.idea/fluidbook_tools.iml +++ b/.idea/fluidbook_tools.iml @@ -70,7 +70,6 @@ - diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index 928890b..92790c4 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -8,6 +8,7 @@ use Cubist\Util\PHP; use Exception; use Fluidbook\Tools\Jobs\ProcessFile; use Fluidbook\Tools\Jobs\ProcessPage; +use Fluidbook\Tools\PDF\Document; use Fluidbook\Tools\Search\SearchIndex; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; @@ -47,7 +48,7 @@ class Compiler implements ShouldQueue, ShouldBeUnique public function addSource($document, $start = 1, $pages = null) { - $source = new Source($document,$this, $start ); + $source = new Source(new Document($document), $start, $pages); $this->sources[] = $source; foreach ($source->getRange() as $sourcePage) { $this->pages[] = new SourcePage($source, $sourcePage); @@ -79,6 +80,7 @@ class Compiler implements ShouldQueue, ShouldBeUnique { start_measure('Process Pages'); foreach ($this->sources as $source) { + dd( $source->getRange()); $source->getDocument()->processPages($this->getProcessFiles(), $source->getRange(), $sync); } stop_measure('Process Pages'); diff --git a/src/Compiler/Source.php b/src/Compiler/Source.php index 3bbe2e8..6705e64 100644 --- a/src/Compiler/Source.php +++ b/src/Compiler/Source.php @@ -54,6 +54,8 @@ class Source public function getEnd(): int { + dd($this->start,$this->pages); + return $this->start + $this->pages - 1; } diff --git a/src/Jobs/ProcessFile.php b/src/Jobs/ProcessFile.php index f1147a2..8dfed35 100644 --- a/src/Jobs/ProcessFile.php +++ b/src/Jobs/ProcessFile.php @@ -120,7 +120,7 @@ class ProcessFile */ public function getOut() { - return $this->job->getSource()->getSource()->getDocument()->getConvertPath(); + return $this->job->getOut(); } diff --git a/src/Jobs/ProcessPage.php b/src/Jobs/ProcessPage.php index 3effbfa..37d3627 100644 --- a/src/Jobs/ProcessPage.php +++ b/src/Jobs/ProcessPage.php @@ -3,7 +3,6 @@ namespace Fluidbook\Tools\Jobs; use Cubist\Util\Files\Files; -use Fluidbook\Tools\Compiler\SourcePage; use Fluidbook\Tools\PDF\Document; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -18,83 +17,107 @@ class ProcessPage implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** - * @var ProcessFile[] + * @var Document */ - protected $files = []; + protected $document; + + /** + * @var integer + */ + protected $page; /** - * @var SourcePage + * @var ProcessFile[] */ - protected $source; + protected $files = []; /** - * @param $source SourcePage + * @param $document Document + * @param $page integer * @param ProcessFile[] $files */ - public function __construct($source, $files = []) + public function __construct($document, $page, $files = []) { - $this->source = $source; + $this->document = $document; + $this->page = $page; $this->files = $files; } + /** + * @param int $page + */ + public function setPage(int $page): void + { + $this->page = $page; + } + /** * @return int */ public function getPage(): int { - return $this->source->getPage(); + return $this->page; + } + + /** + * @return string + */ + public function getOut(): string + { + return $this->document->getConvertPath(); } public function handle() { - start_measure('Process page ' . $this->source->getPage()); + start_measure('Process page ' . $this->page); foreach ($this->files as $file) { - $this->getFile($file, false); + $this->getFile($this->page, $file, false); } - stop_measure('Process page ' . $this->source->getPage()); + stop_measure('Process page ' . $this->page); } /** - * @p + * @param $page * @param $file ProcessFile * @param false $force * @return string */ - public function getFile($file, $force = false) + public function getFile($page, $file, $force = false) { + $this->setPage($page); $file->setJob($this); return $file->getPath($force); } public function getResolutionRatio() { - return $this->source->getSource()->getDocument()->getResolutionRatio(); + return $this->document->getResolutionRatio(); } public function getMobileFirstRatio() { - return $this->source->getSource()->getDocument()->getMobileFirstRatio(); + return $this->document->getMobileFirstRatio(); } public function splitDoc() { start_measure('Split PDF'); - Files::mkdir($this->out . '/pdf'); + Files::mkdir($this->getOut() . '/pdf'); $pdftk = new CommandLine('pdftk'); $pdftk->setArg(null, $this->getPDFInput()); $pdftk->setArg(null, 'burst'); $pdftk->setArg(null, 'uncompress'); $pdftk->setArg(null, 'output'); - $pdftk->setArg(null, $this->out . 'pdf/p%d.pdf'); + $pdftk->setArg(null, $this->getOut() . 'pdf/p%d.pdf'); $pdftk->execute(); for ($i = 1; $i <= $this->getPagesNumber(); $i++) { // Remove annotations : https://gist.github.com/stefanschmidt/5248592 - $file = sprintf($this->out . 'pdf/p%d.pdf', $i); - $to = sprintf($this->out . 'pdf/s%d.pdf', $i); + $file = sprintf($this->getOut() . 'pdf/p%d.pdf', $i); + $to = sprintf($this->getOut() . 'pdf/s%d.pdf', $i); `LANG=C LC_CTYPE=C sed -n '/^\/Annots/!p' $file > $to`; if (file_exists($to)) { if (filesize($to) > 0) { @@ -113,19 +136,11 @@ class ProcessPage implements ShouldQueue */ public function getPagesNumber() { - return $this->source->getSource()->getDocument()->getPages(); + return $this->document->getPages(); } public function getPDFInput() { - return $this->source->getSource()->getDocument()->getPDFInput(); - } - - /** - * @return SourcePage - */ - public function getSource(): SourcePage - { - return $this->source; + return $this->document->getPDFInput(); } } diff --git a/src/PDF/Document.php b/src/PDF/Document.php index 07ac15f..00433b0 100644 --- a/src/PDF/Document.php +++ b/src/PDF/Document.php @@ -2,10 +2,8 @@ namespace Fluidbook\Tools\PDF; -use Cubist\Util\CommandLine; use Cubist\Util\Text; use Fluidbook\Tools\CommandLine\FWSTK; -use Fluidbook\Tools\FluidbookTools; use Fluidbook\Tools\Jobs\ProcessFile; use Fluidbook\Tools\Jobs\ProcessPage; @@ -58,7 +56,7 @@ class Document return $this->hash; } - public function getConvertPath() + public function getConvertPath(): string { return storage_path('fluidbook/convert/' . $this->getHash() . '/'); } @@ -76,7 +74,6 @@ class Document $infos = $fwstk->getOutput(); - if (preg_match('/Pages:\s*(\d+)/', $infos, $matches)) { $this->pages = (int)$matches[1]; } @@ -161,7 +158,7 @@ class Document public function processPage($page, $files, $sync = false) { $dispatchFunction = $sync ? 'dispatchSync' : 'dispatch'; - ProcessPage::$dispatchFunction($this, $page, $this->getConvertPath(), $files); + ProcessPage::$dispatchFunction($this, $page, $files); } public function processLinks() -- 2.39.5