From c9993e08085e292b0d4c6d47ff418d45e2481a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge-Mikael=20Nordg=C3=A5rd?= Date: Tue, 6 May 2025 13:38:12 +0200 Subject: [PATCH] Added image database population command tool --- .gitignore | 3 + app/Console/Commands/MakeImageDB.php | 140 +++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 app/Console/Commands/MakeImageDB.php diff --git a/.gitignore b/.gitignore index 9d5dff3..713b8ae 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ Homestead.json /.vagrant .phpunit.result.cache +# Image Database + +public/imagedb \ No newline at end of file diff --git a/app/Console/Commands/MakeImageDB.php b/app/Console/Commands/MakeImageDB.php new file mode 100644 index 0000000..bb5a79b --- /dev/null +++ b/app/Console/Commands/MakeImageDB.php @@ -0,0 +1,140 @@ + $userId, + 'name' => $folderName, + 'fs_type' => 'folder', + 'parent_id' => $parentId, + ]); + + $this->scanFolder($folderPath, $folderNode->id, $userId, $basePath); + } + + $files = File::files($fullPath); + foreach ($files as $file) { + $ext = strtolower($file->getExtension()); + if (!in_array($ext, ['jpg', 'jpeg', 'png'])) { + continue; // skip unsupported file types + } + + $imageFileName = $file->getFilename(); + $relativePath = str_replace(public_path(), '', $file->getPathname()); + $relativePath = str_replace(DIRECTORY_SEPARATOR, '/', $relativePath); + $relativePath = '/' . ltrim($relativePath, '/'); + + ImageNode::create([ + 'user_id' => $userId, + 'parent_id' => $parentId, + 'name' => $imageFileName, + 'fs_type' => 'file', + 'fs_meta' => json_encode([ + 'real_name' => $file->getPathname(), + 'symbolic_name' => $relativePath, + 'title' => pathinfo($imageFileName, PATHINFO_FILENAME), + 'description' => '', + 'copyright' => '', + 'mime_type' => File::mimeType($file->getPathname()), + 'size' => $file->getSize(), + ]), + ]); + } + } + + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'pagebuilder:make-imagedb'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'This command is a substitute for PolarPress image handling/uploading and administration, in order to provide the page builder with images to work with in the various page components for the pagebuilder. It will scan the public/imagedb directory, and replicate its file/folder structure in the database.'; + + /** + * Execute the console command. + */ + public function handle() { + // The public path to the imagedb directory. If you change this, please also make sure you update + // the .gitignore file so you don't accidentally push garbage to the repository. + $imageDbPath = public_path('imagedb'); + + if (!File::exists($imageDbPath) || !File::isDirectory($imageDbPath)) { + $this->error("The 'public/imagedb' directory does not exist."); + return Command::FAILURE; + } + + // At this point, the directory exist, but we also need to make sure the directory is not empty + $contents = File::allFiles($imageDbPath); + $subdirs = File::directories($imageDbPath); + + if (empty($contents) && empty($subdirs)) { + $this->error("The 'public/imagedb' directory is empty. Nothing to index."); + return Command::FAILURE; + } + + // To avoid dublications (and to make this simple), we're now going to reset the table and + // truncate the data so we start on a clean slate + + $this->info("Clearing existing image nodes..."); + ImageNode::truncate(); + + // The image node model requires a user relationship, so we need to make the command operator + // select which user to act as in order to populate the database correctly + + $users = User::orderBy('name')->get(); + if ($users->isEmpty()) { // Make sure there is at least one user in the database before continuing + $this->error("No users found in the database. Cannot continue!"); + $this->line('(Use artisan serve and npm run dev to spool up a local instance, and register a user from the web interface)'); + return Command::FAILURE; + } + + $options = []; + foreach ($users as $user) { + $label = "{$user->name} ({$user->email})"; + $options[$label] = $user->id; + } + + $selectedLabel = $this->choice( + 'Select a user to act as:', + array_keys($options), + array_key_first($options) + ); + + $userId = $options[$selectedLabel]; + + $this->info("Selected user ID: $userId: $selectedLabel"); + $this->info("Scanning and indexing image directory..."); + $this->scanFolder($imageDbPath, null, $userId, $imageDbPath); + $this->info("Image DB creation complete."); + + return Command::SUCCESS; + } +}