From 3d82687e01aeafe89c07ec27b6d6bebd5e8a33ad Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 15 Sep 2021 17:23:59 +0200 Subject: [PATCH] wip #4666 @3 --- .../Controllers/Tools/FluidbookPreview.php | 3 + app/Jobs/ProcessFluidbook.php | 138 +++++++++++++++--- app/Models/Publication.php | 33 +++++ .../base/inc/sidebar_content.blade.php | 4 +- 4 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 app/Models/Publication.php diff --git a/app/Http/Controllers/Tools/FluidbookPreview.php b/app/Http/Controllers/Tools/FluidbookPreview.php index 319b59d..c699cec 100644 --- a/app/Http/Controllers/Tools/FluidbookPreview.php +++ b/app/Http/Controllers/Tools/FluidbookPreview.php @@ -11,6 +11,9 @@ trait FluidbookPreview { if (count($args) > 0) { $path = implode('/', $args); + }else{ + return redirect(backpack_url('tools/fluidbookpreview/index.html')); + } $path = storage_path('fluidbook/out') . '/' . $path; if(file_exists($path)){ diff --git a/app/Jobs/ProcessFluidbook.php b/app/Jobs/ProcessFluidbook.php index 742b514..2eb32da 100644 --- a/app/Jobs/ProcessFluidbook.php +++ b/app/Jobs/ProcessFluidbook.php @@ -2,12 +2,13 @@ namespace App\Jobs; -use Cubist\Util\CommandLine; +use App\Models\Publication; use Cubist\Util\Files\Files; use Cubist\Util\Files\VirtualDirectory; use Cubist\Util\PHP; -use Fluidbook\Tools\FluidbookTools; +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; @@ -18,6 +19,8 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Jobs\SyncJob; use Illuminate\Queue\SerializesModels; use JsonException; +use Spatie\MediaLibrary\MediaCollections\Models\Media; +use stdClass; class ProcessFluidbook implements ShouldQueue, ShouldBeUnique { @@ -27,12 +30,31 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique * @var Document */ protected $in; + + /** + * @var string + */ protected $out; + + /** + * @var string + */ protected $stub; + /** + * @var Publication + */ + protected $pub; + + /** @var VirtualDirectory */ protected $vdir; + /** + * @var stdClass + */ + protected $config; + /** * Create a new job instance. * @@ -40,7 +62,13 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique */ public function __construct() { - $this->in = new Document(storage_path('fluidbook/in/in.pdf')); + + $this->pub = Publication::find(1); + + /** @var Media $media */ + $media = $this->pub->getMediaInField($this->pub->getAttributeValue('document'))->first(); + + $this->in = new Document($media->getPath()); $this->out = storage_path('fluidbook/out/'); $this->stub = resource_path('fluidbook/'); @@ -71,13 +99,41 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique start_measure('Process Pages'); $out = $this->getConvertPath(); Files::mkdir($out); - $this->in->processPages($out, [new ProcessFile('jpg', 'thumb'), - new ProcessFile('jpg', 150, true, false), - new ProcessFile('svg', 300, false, true), - ], $sync); + $this->in->processPages($out, $this->getProcessFiles(), $sync); stop_measure('Process Pages'); } + /** + * @return ProcessFile[] + */ + public function getProcessFiles() + { + return [ + 'thumb' => new ProcessFile('jpg', 'thumb'), + 'back150' => new ProcessFile('jpg', 150, true, false), + 'text' => new ProcessFile('svg', 300, false, true), + ]; + } + + /** + * @param $name + * @param null|int $page + * @return ProcessFile + * @throws Exception + */ + public function getProcessFile($name, $page = null) + { + $files = $this->getProcessFiles(); + if (isset($files[$name])) { + $res = $files[$name]; + if (null !== $page) { + $res->setJob(new ProcessPage($this->in, $page, $this->getConvertPath())); + } + return $res; + } + throw new Exception(sprintf('File %s does not exist', $name)); + } + public function getConvertPath() { return storage_path('fluidbook/convert/' . $this->in->getHash() . '/'); @@ -97,6 +153,9 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique start_measure('Process links'); } + /** + * @throws JsonException + */ protected function compileFluidbook() { start_measure('Compile fluidbook'); @@ -115,11 +174,11 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique public function compileContents() { - $cp = $this->getConvertPath(); for ($i = 1; $i <= $this->in->getPages(); $i++) { - $this->vdir->copy($cp . 'html/t' . $i . '-150.jpg', 'data/background/150/p' . $i . '.jpg'); - $this->vdir->copy($cp . 'html/to' . $i . '.svg', 'data/contents/p' . $i . '.svg'); - $this->vdir->copy($cp . 'html/p' . $i . '.jpg', 'data/thumbnails/p' . $i . '.jpg'); + + $this->vdir->copy($this->getProcessFile('back150', $i)->getPath(), 'data/background/150/p' . $i . '.jpg'); + $this->vdir->copy($this->getProcessFile('text', $i)->getPath(), 'data/contents/p' . $i . '.svg'); + $this->vdir->copy($this->getProcessFile('thumb', $i)->getPath(), 'data/thumbnails/p' . $i . '.jpg'); } } @@ -145,20 +204,57 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique public function compileConfig() { $settings = mb_substr(file_get_contents($this->stub . '/data/datas.js'), 13, -2); - $config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR); + $this->config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR); + + $this->compileDimensions(); + $this->compilePageNumbers(); + $this->compileChapters(); + $this->config->links = $this->compileLinks(); + + $c = 'var SETTINGS=' . json_encode($this->config, JSON_THROW_ON_ERROR) . ';'; + $this->vdir->file_put_contents('data/datas.js', $c); + } + + protected function compileDimensions() + { + $width = round($this->in->getWidth(), 8); + $height = round($this->in->getHeight(), 8); - $w = $this->in->getWidth(); - $h = $this->in->getHeight(); + $this->config->pageZoomFactor = 3; + $this->config->optimalWidth = 567; + $this->config->optimalHeight = 709; + $imagesize = getimagesize($this->getProcessFile('back150', 1)->getPath()); + $this->config->pdfZoomFactor = round(($imagesize[0] * 0.48) / $width, 12); + + $this->config->cssScale = $this->config->pageZoomFactor * min($this->config->optimalWidth / $width, $this->config->optimalHeight / $height); + $cssWidth = $width * $this->config->cssScale; + $cssHeight = $height * $this->config->cssScale; + + $this->config->pages = $this->in->getPages(); + $this->config->width = $cssWidth; + $this->config->height = $cssHeight; + + $this->config->multiply = $this->config->pdfZoomFactor * $this->config->cssScale; + + $this->config->pagesDimensions = []; + + $thumbDimensions = getimagesize($this->getProcessFile('thumb', 1)->getPath()); + $this->config->thumbWidth = $thumbDimensions[0]; + $this->config->thumbHeight = $thumbDimensions[1]; - $config->pages = $this->in->getPages(); - $config->width = $w; - $config->height = $h; - $config->pagesDimensions = []; for ($i = 1; $i <= $this->in->getPages(); $i++) { - $config->pagesDimensions[$i] = [$w, $h]; + $this->config->pagesDimensions[$i] = [$this->config->width, $this->config->height]; } - $config->links = $this->compileLinks(); - $this->vdir->file_put_contents('data/datas.js', 'var SETTINGS=' . json_encode($config, JSON_THROW_ON_ERROR) . ';'); + } + + protected function compilePageNumbers() + { + $this->config->numerotation = $this->in->getPageNumbers(); + } + + protected function compileChapters() + { + $this->config->chapters = $this->in->getChapters(); } diff --git a/app/Models/Publication.php b/app/Models/Publication.php new file mode 100644 index 0000000..7722190 --- /dev/null +++ b/app/Models/Publication.php @@ -0,0 +1,33 @@ + 'publications', + 'singular' => 'publication', + 'plural' => 'publications', + 'oneinstance' => true]; + + public function setFields() + { + parent::setFields(); + + $this->addField('document', Files::class, 'Document'); + $this->addField('s_download', FormSection::class, 'Téléchargement et impression'); + $this->addField("download_name", Text::class, 'Nom du fichier', ['default' => 'document.pdf']); + $this->addField('pdf_download', Files::class, 'PDF pour le téléchargement et l\'impression'); + } + + public function postSave() + { + ProcessFluidbook::dispatch(); + } +} diff --git a/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php b/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php index 71d7efc..2a002b0 100644 --- a/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php +++ b/resources/views/vendor/backpack/base/inc/sidebar_content.blade.php @@ -4,8 +4,10 @@ @can('cms')