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/regen_thumb/main.php

185 lines
6.5 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 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
{
global $cache;
$event = send_event(new ThumbnailGenerationEvent($image, $force));
$cache->delete("thumb-block:{$image->id}");
return $event->generated;
2019-06-05 23:03:22 +00:00
}
public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("regen_thumb/one/{image_id}", method: "POST", permission: Permissions::DELETE_IMAGE)) {
$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());
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
{
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}");
}
}
2017-06-01 19:44:17 +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)));
// }
// }
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event): void
{
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
}
}
public function onBulkAction(BulkActionEvent $event): void
2019-06-05 23:03:22 +00:00
{
global $page, $user;
2019-06-05 23:03:22 +00:00
2019-06-14 12:47:50 +00:00
switch ($event->action) {
case "bulk_regen":
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::DELETE_IMAGE)) {
$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;
}
$total = 0;
foreach ($event->items as $image) {
2019-06-14 12:47:50 +00:00
if ($this->regenerate_thumbnail($image, $force)) {
$total++;
}
}
$page->flash("Regenerated thumbnails for $total items");
}
break;
}
}
public function onAdminBuilding(AdminBuildingEvent $event): void
{
$this->theme->display_admin_block();
}
public function onAdminAction(AdminActionEvent $event): void
2019-06-14 12:47:50 +00:00
{
global $page;
2019-06-14 12:47:50 +00:00
switch ($event->action) {
case "regen_thumbs":
$event->redirect = true;
$force = false;
if (isset($event->params["regen_thumb_force"]) && $event->params["regen_thumb_force"] == "true") {
2023-11-11 21:49:12 +00:00
$force = true;
}
$limit = 1000;
if (isset($event->params["regen_thumb_limit"]) && is_numeric($event->params["regen_thumb_limit"])) {
$limit = intval($event->params["regen_thumb_limit"]);
}
2020-06-14 16:05:55 +00:00
$mime = "";
if (isset($event->params["regen_thumb_mime"])) {
$mime = $event->params["regen_thumb_mime"];
}
$images = Search::find_images(tags: ["mime=" . $mime]);
$i = 0;
foreach ($images as $image) {
2019-06-14 12:47:50 +00:00
if (!$force) {
$path = warehouse_path(Image::THUMBNAIL_DIR, $image->hash, false);
2019-06-14 12:47:50 +00:00
if (file_exists($path)) {
continue;
}
}
$event = send_event(new ThumbnailGenerationEvent($image, $force));
2019-06-14 12:47:50 +00:00
if ($event->generated) {
$i++;
}
2023-11-11 21:49:12 +00:00
if ($i >= $limit) {
break;
2019-06-05 23:03:22 +00:00
}
}
$page->flash("Re-generated $i thumbnails");
2019-06-05 23:03:22 +00:00
break;
case "delete_thumbs":
$event->redirect = true;
if (isset($event->params["delete_thumb_mime"]) && $event->params["delete_thumb_mime"] != "") {
$images = Search::find_images(tags: ["mime=" . $event->params["delete_thumb_mime"]]);
$i = 0;
foreach ($images as $image) {
$outname = $image->get_thumb_filename();
2019-06-14 12:47:50 +00:00
if (file_exists($outname)) {
unlink($outname);
$i++;
2019-06-14 12:47:50 +00:00
}
}
$page->flash("Deleted $i thumbnails for ".$event->params["delete_thumb_mime"]." images");
} else {
$dir = "data/thumbs/";
deltree($dir);
$page->flash("Deleted all thumbnails");
}
break;
}
}
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;
});
}
}