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

198 lines
6.4 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class RegenThumb extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var RegenThumbTheme */
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;
2020-06-14 16:05:55 +00:00
$event = new ThumbnailGenerationEvent($image->hash, $image->get_mime(), $force);
send_event($event);
$cache->delete("thumb-block:{$image->id}");
return $event->generated;
2019-06-05 23:03:22 +00:00
}
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user;
2019-07-09 14:10:21 +00:00
if ($event->page_matches("regen_thumb/one") && $user->can(Permissions::DELETE_IMAGE) && isset($_POST['image_id'])) {
$image = Image::by_id(int_escape($_POST['image_id']));
2019-06-05 23:03:22 +00:00
$this->regenerate_thumbnail($image);
$this->theme->display_results($page, $image);
}
2019-07-09 14:10:21 +00:00
if ($event->page_matches("regen_thumb/mass") && $user->can(Permissions::DELETE_IMAGE) && isset($_POST['tags'])) {
$tags = Tag::explode(strtolower($_POST['tags']), false);
$images = Image::find_images(0, 10000, $tags);
2017-06-01 19:44:17 +00:00
foreach ($images as $image) {
2019-06-05 23:03:22 +00:00
$this->regenerate_thumbnail($image);
}
2017-06-01 19:44:17 +00:00
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list"));
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::DELETE_IMAGE)) {
$event->add_part($this->theme->get_buttons_html($event->image->id));
}
}
2017-06-01 19:44:17 +00:00
2019-06-05 23:03:22 +00:00
// public function onPostListBuilding(PostListBuildingEvent $event)
// {
// 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)
{
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)
{
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;
2019-06-14 12:47:50 +00:00
if (isset($_POST["bulk_regen_thumb_missing_only"])
&&$_POST["bulk_regen_thumb_missing_only"]=="true") {
$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)
{
$this->theme->display_admin_block();
}
2019-06-14 12:47:50 +00:00
public function onAdminAction(AdminActionEvent $event)
{
global $page;
2019-06-14 12:47:50 +00:00
switch ($event->action) {
case "regen_thumbs":
$event->redirect = true;
$force = false;
2019-06-14 12:47:50 +00:00
if (isset($_POST["regen_thumb_force"])&&$_POST["regen_thumb_force"]=="true") {
$force=true;
}
$limit = 1000;
2019-06-14 12:47:50 +00:00
if (isset($_POST["regen_thumb_limit"])&&is_numeric($_POST["regen_thumb_limit"])) {
$limit=intval($_POST["regen_thumb_limit"]);
}
2020-06-14 16:05:55 +00:00
$mime = "";
if (isset($_POST["regen_thumb_mime"])) {
$mime = $_POST["regen_thumb_mime"];
}
2020-06-14 16:05:55 +00:00
$images = $this->get_images($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;
}
}
2020-06-14 16:05:55 +00:00
$event = new ThumbnailGenerationEvent($image["hash"], $image["mime"], $force);
send_event($event);
2019-06-14 12:47:50 +00:00
if ($event->generated) {
$i++;
}
2019-06-14 12:47:50 +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;
2020-06-14 16:05:55 +00:00
if (isset($_POST["delete_thumb_mime"])&&$_POST["delete_thumb_mime"]!="") {
$images = $this->get_images($_POST["delete_thumb_mime"]);
$i = 0;
foreach ($images as $image) {
$outname = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"]);
2019-06-14 12:47:50 +00:00
if (file_exists($outname)) {
unlink($outname);
$i++;
2019-06-14 12:47:50 +00:00
}
}
2020-06-14 16:05:55 +00:00
$page->flash("Deleted $i thumbnails for ".$_POST["delete_thumb_mime"]." images");
} else {
$dir = "data/thumbs/";
$this->remove_dir_recursively($dir);
$page->flash("Deleted all thumbnails");
}
break;
}
}
public function get_images(string $mime = null): array
{
global $database;
2020-06-14 16:05:55 +00:00
$query = "SELECT hash, mime FROM images";
$args = [];
2020-06-14 16:05:55 +00:00
if (!empty($mime)) {
$query .= " WHERE mime = :mime";
$args["mime"] = $mime;
}
return $database->get_all($query, $args);
}
2019-06-14 12:47:50 +00:00
public function remove_dir_recursively($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
2019-06-14 12:47:50 +00:00
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") {
2019-06-14 12:47:50 +00:00
$this->remove_dir_recursively($dir."/".$object);
} else {
2019-06-14 12:47:50 +00:00
unlink($dir."/".$object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}