From 1eee50530aa815926d8b5c6d118565deab46b11f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 17 Sep 2025 09:19:53 +0200 Subject: [PATCH] wait #7743 @1 --- app/Http/Controllers/FluidbookController.php | 2 +- app/PdfFile.php | 218 ++++--------------- 2 files changed, 44 insertions(+), 176 deletions(-) diff --git a/app/Http/Controllers/FluidbookController.php b/app/Http/Controllers/FluidbookController.php index c6bb342..5bfced1 100644 --- a/app/Http/Controllers/FluidbookController.php +++ b/app/Http/Controllers/FluidbookController.php @@ -21,7 +21,7 @@ class FluidbookController extends Controller */ public function view(PdfFile $file, string $path = '') { - if (app(SettingRepository::class)->byKey('fluidbook_demo_ref') != $file->slug) { + if ($path != 'nscover.jpg' && app(SettingRepository::class)->byKey('fluidbook_demo_ref') != $file->slug) { $this->authorize('view', $file); } // Add trailing slash if needed diff --git a/app/PdfFile.php b/app/PdfFile.php index c206395..f243402 100644 --- a/app/PdfFile.php +++ b/app/PdfFile.php @@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations; use Illuminate\Http\File; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; use Laravel\Nova\Fields\HasOne; use Storage; use Illuminate\Support\Str; @@ -69,7 +70,6 @@ class PdfFile extends TwillModel implements Sortable ]; protected $fillable = [ - 'fluidbook', 'free', 'position', 'title', @@ -83,7 +83,6 @@ class PdfFile extends TwillModel implements Sortable ]; protected $casts = [ - 'is_fluidbook' => 'bool', 'fluidbook' => 'bool', 'headlines' => 'array', ]; @@ -111,7 +110,6 @@ class PdfFile extends TwillModel implements Sortable 'slug' => Str::slug("{$collection->slug}_{$ref}"), 'published' => 0, 'collection_id' => $collection_id, - 'fluidbook' => true, ], [ 'ref' => $ref, @@ -145,22 +143,49 @@ class PdfFile extends TwillModel implements Sortable */ public function process(): void { - $client = new Client(env("FLUIDBOOK_TOOLBOX_API_TOKEN")); - $client->createFluidbook(new \SplFileInfo($this->absolutePdfPath), env('FLUIDBOOK_TOOLBOX_BASE_FLUIDBOOK'), $this->title, env('FLUIDBOOK_TOOLBOX_EXTERNAL_SERVER'), 'PSQ-' . $this->ref); + $client = new Client(config('fluidbook.api_token')); + $path = \Storage::disk('local')->path($this->pdfPath); + $e = explode('/', $path); + array_pop($e); + $filebin = implode('/', $e) . '/file.bin'; + if (file_exists($filebin) && !file_exists($path)) { + copy($filebin, $path); + } + $res = $client->createFluidbook( + new \SplFileInfo($path), + config('fluidbook.base_fluidbook'), + $this->title, + config('fluidbook.external_server'), + $this->slug, + ['reference' => $this->slug] + ); + + if ($res) { + Cache::forever(self::getCacheKey($this->slug), $res); + } -// \Log::debug('json'); -// $this->makeJson(); -// \Log::debug('cover'); -// $this->makeCover(); -// \Log::debug('links'); -// $this->shortenLinks(); -// \Log::debug('searchable'); -// $this->makeSearchable(); -// \Log::debug('cloud'); -// $this->saveToCloud(); + } + public static function getCacheKey($reference) + { + return "fluidbook_byref_$reference"; } + public static function getFluidbookByReference(string $reference, Client $client, string $cacheKey = null) + { + if (null === $cacheKey) { + $cacheKey = self::getCacheKey($reference); + } + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); + } + $fluidbook = $client->getFluidbookByReference($reference); + if ($fluidbook) { + Cache::forever($cacheKey, $fluidbook); + return $fluidbook; + } + return false; + } /** * Sets published to true @@ -254,7 +279,7 @@ class PdfFile extends TwillModel implements Sortable */ public function getDirectoryAttribute(): string { - $dir = $this->fluidbook ? 'fluidbook' : 'flowpaper'; + $dir = 'fluidbook'; return "$dir/$this->slug"; } @@ -279,22 +304,13 @@ class PdfFile extends TwillModel implements Sortable return "$this->directory/file.bin"; } - /** - * @return string - * returns relative json path (s3) - */ - public function getJsonPathAttribute(): string - { - return "$this->directory/searchable.json"; - } - /** * @return string * returns cover path (s3) */ public function getCoverPathAttribute(): string { - return "$this->directory/cover.jpg"; + return "$this->directory/nscover.jpg"; } @@ -353,7 +369,7 @@ class PdfFile extends TwillModel implements Sortable */ public function getCoverUrlAttribute(): string { - return Storage::cloud()->url($this->coverPath); + return url(str_replace('fluidbook', 'view', $this->coverPath)); } /** @@ -417,19 +433,6 @@ class PdfFile extends TwillModel implements Sortable ); } - /** - * @return string - * Returns download link. - */ - public function getDownloadUrl(): string - { - return \URL::temporarySignedRoute( - 'pdf.download', - now()->addDays(3), - ['file' => $this->slug] - ); - } - /** * @param User $user * @return string @@ -446,141 +449,6 @@ class PdfFile extends TwillModel implements Sortable } - /** - * @param false $download - * @return \Symfony\Component\HttpFoundation\StreamedResponse - * @throws \League\Flysystem\FileNotFoundException - */ - public function downloadFile($download = false) - { - $disposition = $download ? - 'attachment' : - 'inline'; - $fs = Storage::cloud()->getDriver(); - $stream = $fs->readStream($this->binPath); - return \Response::stream(function () use ($stream) { - fpassthru($stream); - }, 200, [ - 'Content-Type' => 'application/pdf', - 'Content-Length' => $fs->getSize($this->binPath), - 'Content-Disposition' => sprintf('%s; filename="%s.pdf"', $disposition, $this->title), - ]); - } - - - /** - * Processes pdf to json conversion for in-file search - */ - public function makeJson(): void - { - $tmp = sys_get_temp_dir() . '/' . uniqid('json_', false); - - Pdf2Json::convert($this->absolutePdfPath, $tmp); - - Storage::cloud()->putFileAs('/', $tmp, $this->jsonPath); - - unlink($tmp); - } - - /** - * @throws PdfToImage\Exceptions\PdfDoesNotExist - * Generates a jpg cover - */ - public function makeCover(): void - { - $tmp = sys_get_temp_dir() . '/' . uniqid('cover_', false); - - Storage::disk('public')->makeDirectory('covers'); - $pdf = new PdfToImage\Pdf($this->absolutePdfPath); - - $pdf->setResolution(100) - ->setColorspace(\Imagick::COLORSPACE_SRGB) - ->saveImage($tmp); - - $image = \Image::make($tmp); - $image->interlace(); - $image->save(null, 50); - - - Storage::cloud()->putFileAS('/', $tmp, $this->coverPath); - - unlink($tmp); - - } - - /** - * @return void - * @throws \Exception - */ - public function makeSearchable() - { - $client = resolve(TikaClient::class); - $html = $client - ->setEncoding('UTF-8') - ->getHTML($this->absolutePdfPath); - - $html = preg_replace('/\-\n/', '', $html); - - $result = []; - - $this->searchableTexts()->delete(); - - $page_i = 0; - - foreach (HtmlDomParser::str_get_html($html)->find('div.page') as $page) { - $page_i++; - - $buffer = ""; - - foreach ($page->find('p') as $paragraph) { - - $text = html_entity_decode($paragraph->plaintext); - if (empty($text)) - continue; - - $text = preg_replace('/^([A-Z]) /', '$1', $text); - $text = Str::lower(trim($text)); - - $newBuffer = $buffer . ' ' . $text; - - if (Str::length($newBuffer) > config('scout.text_max_length')) { - $result[] = [ - 'content' => $buffer, - 'page' => $page_i, - ]; - $newBuffer = $text; - } - - $buffer = $newBuffer; - - } - - - $result[] = [ - 'content' => $buffer, - 'page' => $page_i, - ]; - - - } - - $this->searchableTexts()->createMany($result); - } - - - /** - * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException - * Copies file to S3 - */ - public function saveToCloud() - { -// $binPath = sys_get_temp_dir().'/'.uniqid('bin_', false); -// file_put_contents($binPath, base64_encode($this->getPdf())); - Storage::cloud()->putFileAs('/', $this->absolutePdfPath, $this->binPath); -// unlink($binPath); - } - - /** * Shortens and tracks PDF Links */ -- 2.39.5