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

89 lines
3.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Shimmie2;
class ReplaceFile extends Extension
{
/** @var ReplaceFileTheme */
protected Themelet $theme;
public function onPageRequest(PageRequestEvent $event): void
{
global $cache, $page, $user;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("replace/{image_id}", method: "GET", permission: Permissions::REPLACE_IMAGE)) {
$image_id = $event->get_iarg('image_id');
$image = Image::by_id_ex($image_id);
2024-02-11 11:34:09 +00:00
$this->theme->display_replace_page($page, $image_id);
}
2024-02-11 11:34:09 +00:00
if ($event->page_matches("replace/{image_id}", method: "POST", permission: Permissions::REPLACE_IMAGE)) {
$image_id = $event->get_iarg('image_id');
$image = Image::by_id_ex($image_id);
2024-02-11 11:34:09 +00:00
2024-08-31 16:05:18 +00:00
if (empty($event->get_POST("url")) && count($_FILES) == 0) {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("replace/$image_id"));
return;
}
2024-02-11 11:34:09 +00:00
if (!empty($event->get_POST("url"))) {
$tmp_filename = shm_tempnam("transload");
fetch_url($event->req_POST("url"), $tmp_filename);
send_event(new ImageReplaceEvent($image, $tmp_filename));
} elseif (count($_FILES) > 0) {
send_event(new ImageReplaceEvent($image, $_FILES["data"]['tmp_name']));
}
2024-08-31 16:05:18 +00:00
if ($event->get_POST("source")) {
2024-02-11 11:34:09 +00:00
send_event(new SourceSetEvent($image, $event->req_POST("source")));
}
2024-02-11 11:34:09 +00:00
$cache->delete("thumb-block:{$image_id}");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/$image_id"));
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
{
global $user;
/* In the future, could perhaps allow users to replace images that they own as well... */
if ($user->can(Permissions::REPLACE_IMAGE)) {
2024-02-10 18:35:55 +00:00
$event->add_button("Replace", "replace/{$event->image->id}");
}
}
public function onImageReplace(ImageReplaceEvent $event): void
{
$image = $event->image;
$duplicate = Image::by_hash($event->new_hash);
if (!is_null($duplicate) && $duplicate->id != $image->id) {
throw new ImageReplaceException("A different post >>{$duplicate->id} already has hash {$duplicate->hash}");
}
$image->remove_image_only(); // Actually delete the old image file from disk
$target = warehouse_path(Image::IMAGE_DIR, $event->new_hash);
try {
\Safe\copy($event->tmp_filename, $target);
} catch (\Exception $e) {
throw new ImageReplaceException("Failed to copy file from uploads ({$event->tmp_filename}) to archive ($target): {$e->getMessage()}");
}
unlink($event->tmp_filename);
// update metadata and save metadata to DB
$event->image->hash = $event->new_hash;
$event->image->filesize = \Safe\filesize($target);
$event->image->set_mime(MimeType::get_for_file($target));
send_event(new MediaCheckPropertiesEvent($image));
$image->save_to_db();
send_event(new ThumbnailGenerationEvent($image));
log_info("image", "Replaced >>{$image->id} {$event->old_hash} with {$event->new_hash}");
}
}