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;
];
protected $fillable = [
- 'fluidbook',
'free',
'position',
'title',
];
protected $casts = [
- 'is_fluidbook' => 'bool',
'fluidbook' => 'bool',
'headlines' => 'array',
];
'slug' => Str::slug("{$collection->slug}_{$ref}"),
'published' => 0,
'collection_id' => $collection_id,
- 'fluidbook' => true,
],
[
'ref' => $ref,
*/
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
*/
public function getDirectoryAttribute(): string
{
- $dir = $this->fluidbook ? 'fluidbook' : 'flowpaper';
+ $dir = 'fluidbook';
return "$dir/$this->slug";
}
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";
}
*/
public function getCoverUrlAttribute(): string
{
- return Storage::cloud()->url($this->coverPath);
+ return url(str_replace('fluidbook', 'view', $this->coverPath));
}
/**
);
}
- /**
- * @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
}
- /**
- * @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
*/