Added image database population command tool

This commit is contained in:
Helge-Mikael Nordgård 2025-05-06 13:38:12 +02:00
parent faabbc8c36
commit c9993e0808
2 changed files with 143 additions and 0 deletions

3
.gitignore vendored
View File

@ -23,3 +23,6 @@ Homestead.json
/.vagrant
.phpunit.result.cache
# Image Database
public/imagedb

View File

@ -0,0 +1,140 @@
<?php
namespace App\Console\Commands;
use Illuminate\Support\Facades\File;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\ImageNode;
class MakeImageDB extends Command
{
/**
* Summary of scanFolder
* @param string $fullPath
* @param mixed $parentId
* @param int $userId
* @param string $basePath
* @return void
*/
private function scanFolder(string $fullPath, ?int $parentId, int $userId, string $basePath): void
{
$entries = File::directories($fullPath);
foreach ($entries as $folderPath) {
$folderName = basename($folderPath);
$folderNode = ImageNode::create([
'user_id' => $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;
}
}