This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/bulk_add/main.php

74 lines
2.2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
use Symfony\Component\Console\Output\OutputInterface;
class BulkAddEvent extends Event
{
public string $dir;
/** @var UploadResult[] */
public array $results;
2015-08-23 15:09:52 +00:00
public function __construct(string $dir)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->dir = $dir;
$this->results = [];
}
2015-08-23 15:09:52 +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
public function onPageRequest(PageRequestEvent $event): void
{
2023-06-25 13:19:02 +00:00
global $page, $user;
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);
}
}
public function onCliGen(CliGenEvent $event): void
{
$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));
foreach ($bae->results as $r) {
2024-08-31 16:05:18 +00:00
if (is_a($r, UploadError::class)) {
$output->writeln($r->name." failed: ".$r->error);
} else {
$output->writeln($r->name." ok");
}
}
return Command::SUCCESS;
});
}
public function onAdminBuilding(AdminBuildingEvent $event): void
{
$this->theme->display_admin_block();
}
public function onBulkAdd(BulkAddEvent $event): void
{
if (is_dir($event->dir) && is_readable($event->dir)) {
$event->results = add_dir($event->dir);
} else {
$event->results = [new UploadError($event->dir, "is not a readable directory")];
}
}
2015-08-23 15:09:52 +00:00
}