You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 lines
2.5 KiB

<?php
namespace App\Console\Commands;
use App\ImageRecord;
use Illuminate\Console\Command;
use App\Services\DirService;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
class RecordDir extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'record:dir {path}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$path = trim($this->argument("path"));
$service = new DirService();
$list = $service->recursiveScan($path);
if (isset($list["files"]) && count($list["files"]) > 0) {
foreach ($list["files"] as $file) {
if (strstr($file, ".DS_Store")) {
continue;
}
$fileInfo = pathinfo($file);
try {
$innerPath = str_replace($path, "", $fileInfo["dirname"]);
$innerName = $fileInfo["basename"];
ImageRecord::firstOrCreate(["path" => $innerPath, "name" => $innerName],
["path" => $innerPath,
"name" => $innerName,
"type" => 2
]
);
} catch (QueryException $e) {
if (!str_contains($e->getMessage(), "Duplicate entry")) {
Log::error($e->getMessage());
}
}
unset($imageRecord);
}
}
foreach ($list["dirs"] as $dir) {
if (strstr($dir, ".DS_Store") || $dir == ".." || $dir == "." || str_starts_with($dir, ".")) {
continue;
}
try {
ImageRecord::firstOrCreate(["path" => $path, "name" => $dir],
["path" => $path,
"name" => $dir,
"type" => 1
]
);
} catch (QueryException $e) {
if (!str_contains($e->getMessage(), "Duplicate entry")) {
Log::error($e->getMessage());
}
}
}
}
}