2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2024-03-17 21:55:55 +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 RegenThumb extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var RegenThumbTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
public function regenerate_thumbnail(Image $image, bool $force = true): bool
|
2019-06-05 23:03:22 +00:00
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache;
|
2024-01-09 01:03:46 +00:00
|
|
|
$event = send_event(new ThumbnailGenerationEvent($image, $force));
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("thumb-block:{$image->id}");
|
2019-06-09 18:22:48 +00:00
|
|
|
return $event->generated;
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $page, $user;
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("regen_thumb/one/{image_id}", method: "POST", permission: Permissions::DELETE_IMAGE)) {
|
2024-02-20 00:22:25 +00:00
|
|
|
$image = Image::by_id_ex($event->get_iarg('image_id'));
|
2019-06-05 23:03:22 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
$this->regenerate_thumbnail($image);
|
2024-02-10 01:55:05 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
$this->theme->display_results($page, $image);
|
|
|
|
}
|
|
|
|
if ($event->page_matches("regen_thumb/mass", method: "POST", permission: Permissions::DELETE_IMAGE)) {
|
|
|
|
$tags = Tag::explode(strtolower($event->req_POST('tags')), false);
|
|
|
|
$images = Search::find_images(limit: 10000, tags: $tags);
|
2017-06-01 19:44:17 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
foreach ($images as $image) {
|
|
|
|
$this->regenerate_thumbnail($image);
|
2024-02-10 01:55:05 +00:00
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link());
|
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 onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2024-02-10 18:35:55 +00:00
|
|
|
$event->add_button("Regenerate Thumbnail", "regen_thumb/one/{$event->image->id}");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-01 19:44:17 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
// public function onPostListBuilding(PostListBuildingEvent $event): void
|
2019-06-05 23:03:22 +00:00
|
|
|
// {
|
|
|
|
// global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
// if ($user->can(UserAbilities::DELETE_IMAGE) && !empty($event->search_terms)) {
|
2019-06-05 23:03:22 +00:00
|
|
|
// $event->add_control($this->theme->mtr_html(Tag::implode($event->search_terms)));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-06-05 23:03:22 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2019-09-29 13:30:55 +00:00
|
|
|
$event->add_action("bulk_regen", "Regen Thumbnails", "", "", $this->theme->bulk_html());
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkAction(BulkActionEvent $event): void
|
2019-06-05 23:03:22 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-06-05 23:03:22 +00:00
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($event->action) {
|
2019-06-12 22:44:25 +00:00
|
|
|
case "bulk_regen":
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$force = true;
|
2024-02-10 01:55:05 +00:00
|
|
|
if (isset($event->params["bulk_regen_thumb_missing_only"])
|
|
|
|
&& $event->params["bulk_regen_thumb_missing_only"] == "true") {
|
2023-11-11 21:49:12 +00:00
|
|
|
$force = false;
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-12 22:44:25 +00:00
|
|
|
$total = 0;
|
2019-07-05 15:24:46 +00:00
|
|
|
foreach ($event->items as $image) {
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($this->regenerate_thumbnail($image, $force)) {
|
2019-06-12 22:44:25 +00:00
|
|
|
$total++;
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Regenerated thumbnails for $total items");
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event): void
|
2019-06-09 18:22:48 +00:00
|
|
|
{
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAdminAction(AdminActionEvent $event): void
|
2019-06-14 12:47:50 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page;
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($event->action) {
|
2019-06-09 18:22:48 +00:00
|
|
|
case "regen_thumbs":
|
2022-10-27 16:21:46 +00:00
|
|
|
$event->redirect = true;
|
2019-06-09 18:22:48 +00:00
|
|
|
$force = false;
|
2024-02-10 00:00:49 +00:00
|
|
|
if (isset($event->params["regen_thumb_force"]) && $event->params["regen_thumb_force"] == "true") {
|
2023-11-11 21:49:12 +00:00
|
|
|
$force = true;
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
$limit = 1000;
|
2024-02-10 00:00:49 +00:00
|
|
|
if (isset($event->params["regen_thumb_limit"]) && is_numeric($event->params["regen_thumb_limit"])) {
|
|
|
|
$limit = intval($event->params["regen_thumb_limit"]);
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
$mime = "";
|
2024-02-10 00:00:49 +00:00
|
|
|
if (isset($event->params["regen_thumb_mime"])) {
|
|
|
|
$mime = $event->params["regen_thumb_mime"];
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
2024-01-09 00:40:37 +00:00
|
|
|
$images = Search::find_images(tags: ["mime=" . $mime]);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ($images as $image) {
|
2019-06-14 12:47:50 +00:00
|
|
|
if (!$force) {
|
2024-01-09 00:40:37 +00:00
|
|
|
$path = warehouse_path(Image::THUMBNAIL_DIR, $image->hash, false);
|
2019-06-14 12:47:50 +00:00
|
|
|
if (file_exists($path)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 01:03:46 +00:00
|
|
|
$event = send_event(new ThumbnailGenerationEvent($image, $force));
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($event->generated) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$i++;
|
|
|
|
}
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($i >= $limit) {
|
2019-06-09 18:22:48 +00:00
|
|
|
break;
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Re-generated $i thumbnails");
|
2019-06-05 23:03:22 +00:00
|
|
|
break;
|
2019-06-09 18:22:48 +00:00
|
|
|
case "delete_thumbs":
|
|
|
|
$event->redirect = true;
|
|
|
|
|
2024-02-10 00:00:49 +00:00
|
|
|
if (isset($event->params["delete_thumb_mime"]) && $event->params["delete_thumb_mime"] != "") {
|
|
|
|
$images = Search::find_images(tags: ["mime=" . $event->params["delete_thumb_mime"]]);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ($images as $image) {
|
2024-01-09 00:40:37 +00:00
|
|
|
$outname = $image->get_thumb_filename();
|
2019-06-14 12:47:50 +00:00
|
|
|
if (file_exists($outname)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
unlink($outname);
|
|
|
|
$i++;
|
2019-06-14 12:47:50 +00:00
|
|
|
}
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
2024-02-10 00:00:49 +00:00
|
|
|
$page->flash("Deleted $i thumbnails for ".$event->params["delete_thumb_mime"]." images");
|
2019-06-09 18:22:48 +00:00
|
|
|
} else {
|
|
|
|
$dir = "data/thumbs/";
|
2024-01-09 00:37:02 +00:00
|
|
|
deltree($dir);
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Deleted all thumbnails");
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 21:55:55 +00:00
|
|
|
|
|
|
|
public function onCliGen(CliGenEvent $event): void
|
|
|
|
{
|
|
|
|
$event->app->register('regen-thumb')
|
|
|
|
->addArgument('id_or_hash', InputArgument::REQUIRED)
|
|
|
|
->setDescription("Regenerate a post's thumbnail")
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
$uid = $input->getArgument('id_or_hash');
|
|
|
|
$image = Image::by_id_or_hash($uid);
|
|
|
|
if ($image) {
|
|
|
|
send_event(new ThumbnailGenerationEvent($image, true));
|
|
|
|
} else {
|
|
|
|
$output->writeln("No post with ID '$uid'\n");
|
|
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|