[core] inline some single-use functions

This commit is contained in:
Shish 2024-01-09 04:22:59 +00:00
parent 03d4045117
commit 3c90597ca8
3 changed files with 31 additions and 30 deletions

View file

@ -287,19 +287,6 @@ abstract class DataHandlerExtension extends Extension
{
protected array $SUPPORTED_MIME = [];
protected function move_upload_to_archive(DataUploadEvent $event): string
{
$target = warehouse_path(Image::IMAGE_DIR, $event->hash);
if (!@copy($event->tmpname, $target)) {
$errors = error_get_last();
throw new UploadException(
"Failed to copy file from uploads ({$event->tmpname}) to archive ($target): ".
"{$errors['type']} / {$errors['message']}"
);
}
return $target;
}
public function onDataUpload(DataUploadEvent $event)
{
global $config;
@ -329,8 +316,15 @@ abstract class DataHandlerExtension extends Extension
$filename = $event->tmpname;
// FIXME: this should happen after ImageAdditionEvent, but the thumbnail
// code assumes the file is in the archive already instead of using
// the image->tmp_file field
$filename = $this->move_upload_to_archive($event);
// the image->get_image_filename() function
$filename = warehouse_path(Image::IMAGE_DIR, $event->hash);
if (!@copy($event->tmpname, $filename)) {
$errors = error_get_last();
throw new UploadException(
"Failed to copy file from uploads ({$event->tmpname}) to archive ($filename): ".
"{$errors['type']} / {$errors['message']}"
);
}
assert(is_readable($filename));
$image = new Image();

View file

@ -583,7 +583,7 @@ class Image
* This function removes an image (and thumbnail) from the DISK ONLY.
* It DOES NOT remove anything from the database.
*/
public function remove_image_only(bool $quiet=false): void
public function remove_image_only(bool $quiet = false): void
{
$img_del = @unlink($this->get_image_filename());
$thumb_del = @unlink($this->get_thumb_filename());
@ -591,8 +591,7 @@ class Image
if(!$quiet) {
log_info("core_image", "Deleted files for Post #{$this->id} ({$this->hash})");
}
}
else {
} else {
$img = $img_del ? '' : ' image';
$thumb = $thumb_del ? '' : ' thumbnail';
log_error('core_image', "Failed to delete files for Post #{$this->id}{$img}{$thumb}");

View file

@ -32,6 +32,26 @@ class SVGFileHandler extends DataHandlerExtension
}
}
public function onDataUpload(DataUploadEvent $event)
{
global $config;
if ($this->supported_mime($event->mime)) {
// If the SVG handler intends to handle this file,
// then sanitise it before touching it
$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = file_get_contents($event->tmpname);
$cleanSVG = $sanitizer->sanitize($dirtySVG);
$event->hash = md5($cleanSVG);
$new_tmpname = tempnam(sys_get_temp_dir(), "shimmie_svg");
file_put_contents($new_tmpname, $cleanSVG);
$event->set_tmpname($new_tmpname);
parent::onDataUpload($event);
}
}
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
$event->image->lossless = true;
@ -44,18 +64,6 @@ class SVGFileHandler extends DataHandlerExtension
$event->image->height = $msp->height;
}
protected function move_upload_to_archive(DataUploadEvent $event): string
{
$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true);
$dirtySVG = file_get_contents($event->tmpname);
$cleanSVG = $sanitizer->sanitize($dirtySVG);
$event->hash = md5($cleanSVG);
$filename = warehouse_path(Image::IMAGE_DIR, $event->hash);
file_put_contents($filename, $cleanSVG);
return $filename;
}
protected function create_thumb(string $hash, string $mime): bool
{
try {