2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-01-11 00:55:05 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class BulkAddEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $dir;
|
2024-01-05 13:53:21 +00:00
|
|
|
/** @var UploadResult[] */
|
2021-03-14 23:43:50 +00:00
|
|
|
public array $results;
|
2015-08-23 15:09:52 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(string $dir)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->dir = $dir;
|
|
|
|
$this->results = [];
|
|
|
|
}
|
2015-08-23 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class BulkAdd extends Extension
|
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var BulkAddTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-02-04 00:46:36 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-06-25 13:19:02 +00:00
|
|
|
global $page, $user;
|
2024-02-10 23:03:14 +00:00
|
|
|
if ($event->page_matches("bulk_add", method: "POST", permission: Permissions::BULK_ADD)) {
|
|
|
|
$dir = $event->req_POST('dir');
|
|
|
|
shm_set_timeout(null);
|
|
|
|
$bae = send_event(new BulkAddEvent($dir));
|
|
|
|
$this->theme->display_upload_results($page, $bae->results);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCliGen(CliGenEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-11 00:55:05 +00:00
|
|
|
$event->app->register('bulk-add')
|
|
|
|
->addArgument('directory', InputArgument::REQUIRED)
|
|
|
|
->setDescription('Import a directory of images')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
$dir = $input->getArgument('directory');
|
|
|
|
$bae = send_event(new BulkAddEvent($dir));
|
2024-01-05 13:53:21 +00:00
|
|
|
foreach ($bae->results as $r) {
|
2024-08-31 16:05:18 +00:00
|
|
|
if (is_a($r, UploadError::class)) {
|
2024-01-11 00:55:05 +00:00
|
|
|
$output->writeln($r->name." failed: ".$r->error);
|
2024-01-05 13:53:21 +00:00
|
|
|
} else {
|
2024-01-11 00:55:05 +00:00
|
|
|
$output->writeln($r->name." ok");
|
2024-01-05 13:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-11 00:55:05 +00:00
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-06-17 19:05:16 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkAdd(BulkAddEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if (is_dir($event->dir) && is_readable($event->dir)) {
|
|
|
|
$event->results = add_dir($event->dir);
|
|
|
|
} else {
|
2024-01-05 13:53:21 +00:00
|
|
|
$event->results = [new UploadError($event->dir, "is not a readable directory")];
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-23 15:09:52 +00:00
|
|
|
}
|