]> _ Git - psq.git/commitdiff
ingest pdf file artisan command
authorLouis Jeckel <louis.jeckel@outlook.cm>
Sun, 16 Aug 2020 16:21:01 +0000 (18:21 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Sun, 16 Aug 2020 16:21:01 +0000 (18:21 +0200)
app/Console/Commands/IngestPdfFiles.php [new file with mode: 0644]
app/Http/Controllers/Controller.php
app/Http/Controllers/HomeController.php
app/PdfFile.php

diff --git a/app/Console/Commands/IngestPdfFiles.php b/app/Console/Commands/IngestPdfFiles.php
new file mode 100644 (file)
index 0000000..1377f14
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\FileCollection;
+use App\PdfFile;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Http\File;
+use Illuminate\Support\Str;
+
+class IngestPdfFiles extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'pdf:ingest';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Ingests Pdf Files';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     * @throws \Spatie\PdfToImage\Exceptions\PdfDoesNotExist
+     */
+    public function handle()
+    {
+        $files = \Storage::files('ingest');
+
+
+        foreach ($files as $file) {
+            $path = \Storage::path($file);
+            $fileName = basename($path, '.pdf');
+            $ref = Str::after($fileName, 'PSQ ');
+
+            $lastModified = \File::lastModified($path);
+            $lastModified = Carbon::createFromFormat("U", $lastModified);
+
+            $collection = FileCollection::findOrFail(1);
+            $title = "{$collection->short_name} - $ref";
+
+            if(PdfFile::where('ref', $ref)->exists()) {
+                $this->warn("$title already exists");
+                continue;
+            }
+
+            /** @var PdfFile $pdfFile */
+            $pdfFile = PdfFile::create([
+                'ref' => $ref,
+                'collection_id' => $collection->id,
+                'slug' => Str::slug("{$collection->slug}_{$ref}"),
+                'title' => $title,
+                'headlines' => [],
+                'published' => 1,
+                'created_at' => $lastModified
+            ]);
+
+            $this->info("$pdfFile created. Processing file.");
+
+            \Storage::makeDirectory($pdfFile->directory);
+
+            copy($path, $pdfFile->absolutePdfPath);
+
+            $pdfFile->process();
+
+            $this->info("$pdfFile ingested");
+
+            return;
+
+
+
+        }
+
+
+    }
+}
index 854e0c7e9600b17de1b9f54f517aefab52066699..09fcf5b3e236e68b81db60884650850195c38737 100644 (file)
@@ -18,5 +18,4 @@ class Controller extends BaseController
         }
     }
 
-
 }
index 91a573bbed2f39cbe13b028353563b7f50a0e97f..e332c497c3819a594e53c86e1b3c10f4b792984f 100644 (file)
@@ -15,7 +15,7 @@ class HomeController extends Controller
      */
     public function index()
     {
-        \View::share('last_pdf', PdfFile::query()->orderByDesc('id')->published()->take(7)->get());
+        \View::share('last_pdf', PdfFile::query()->orderByDesc('created_at')->published()->take(7)->get());
         return view('home.index');
     }
 
index 0cce63566fae61e6d7e68537c9ae7ee4da934414..0b8aaef5aa8d86005aa8fd3f143f145f7d2d7f9b 100644 (file)
@@ -67,6 +67,7 @@ class PdfFile extends TwillModel implements Sortable
         'ref',
         'headlines',
         'published',
+        'created_at'
     ];
 
     protected $casts = [
@@ -127,9 +128,10 @@ class PdfFile extends TwillModel implements Sortable
 
         $this->makeJson();
         $this->makeCover();
-//        $this->makeSearchable();
-//        if(!env('APP_ENV') === 'local')
-//            $this->shortenLinks();
+        if(!config('app.env') === 'local'){
+            $this->shortenLinks();
+            $this->makeSearchable();
+        }
         $this->saveToCloud();
 
     }