From 37fe743f6565d8b3e663fad0e08c8173d293c311 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Sat, 15 Jun 2019 11:18:52 -0500 Subject: [PATCH] Changed "images" and "thumbs" usages to constants --- core/extension.php | 6 +++--- core/imageboard/image.php | 8 ++++++-- core/imageboard/misc.php | 10 +++++----- core/util.php | 7 +++++-- ext/admin/main.php | 2 +- ext/bulk_add_csv/main.php | 2 +- ext/handle_flash/main.php | 2 +- ext/handle_mp3/main.php | 2 +- ext/handle_pixel/main.php | 4 ++-- ext/handle_svg/main.php | 8 ++++---- ext/regen_thumb/main.php | 4 ++-- ext/resize/main.php | 6 +++--- ext/rotate/main.php | 4 ++-- ext/rule34/main.php | 4 ++-- ext/transcode/main.php | 4 ++-- 15 files changed, 40 insertions(+), 33 deletions(-) diff --git a/core/extension.php b/core/extension.php index af7bb6ad..d691bd68 100644 --- a/core/extension.php +++ b/core/extension.php @@ -182,7 +182,7 @@ abstract class DataHandlerExtension extends Extension // even more hax.. $event->metadata['tags'] = $existing->get_tag_list(); - $image = $this->create_image_from_data(warehouse_path("images", $event->metadata['hash']), $event->metadata); + $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->metadata['hash']), $event->metadata); if (is_null($image)) { throw new UploadException("Data handler failed to create image object from data"); @@ -192,7 +192,7 @@ abstract class DataHandlerExtension extends Extension send_event($ire); $event->image_id = $image_id; } else { - $image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata); + $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata); if (is_null($image)) { throw new UploadException("Data handler failed to create image object from data"); } @@ -224,7 +224,7 @@ abstract class DataHandlerExtension extends Extension if ($event->force) { $result = $this->create_thumb($event->hash, $event->type); } else { - $outname = warehouse_path("thumbs", $event->hash); + $outname = warehouse_path(Image::THUMBNAIL_DIR, $event->hash); if (file_exists($outname)) { return; } diff --git a/core/imageboard/image.php b/core/imageboard/image.php index af6a15d8..717abf7f 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -10,6 +10,10 @@ */ class Image { + public const DATA_DIR = "data"; + public const IMAGE_DIR = "images"; + public const THUMBNAIL_DIR = "thumbs"; + private static $tag_n = 0; // temp hack public static $order_sql = null; // this feels ugly @@ -502,7 +506,7 @@ class Image */ public function get_image_filename(): string { - return warehouse_path("images", $this->hash); + return warehouse_path(self::IMAGE_DIR, $this->hash); } /** @@ -510,7 +514,7 @@ class Image */ public function get_thumb_filename(): string { - return warehouse_path("thumbs", $this->hash); + return warehouse_path(self::THUMBNAIL_DIR, $this->hash); } /** diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index e9bd93c6..d08daf2b 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -12,7 +12,7 @@ */ function move_upload_to_archive(DataUploadEvent $event): void { - $target = warehouse_path("images", $event->hash); + $target = warehouse_path(Image::IMAGE_DIR, $event->hash); if (!@copy($event->tmpname, $target)) { $errors = error_get_last(); throw new UploadException( @@ -171,8 +171,8 @@ function create_thumbnail_convert($hash, $input_type = ""): bool { global $config; - $inname = warehouse_path("images", $hash); - $outname = warehouse_path("thumbs", $hash); + $inname = warehouse_path(Image::IMAGE_DIR, $hash); + $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash); $q = $config->get_int("thumb_quality"); $convert = $config->get_string("thumb_convert_path"); @@ -236,8 +236,8 @@ function create_thumbnail_ffmpeg($hash): bool return false; } - $inname = warehouse_path("images", $hash); - $outname = warehouse_path("thumbs", $hash); + $inname = warehouse_path(Image::IMAGE_DIR, $hash); + $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash); $orig_size = video_size($inname); $scaled_size = get_thumbnail_size($orig_size[0], $orig_size[1], true); diff --git a/core/util.php b/core/util.php index 299463d8..6dd74b26 100644 --- a/core/util.php +++ b/core/util.php @@ -163,10 +163,13 @@ function warehouse_path(string $base, string $hash, bool $create=true): string { $ab = substr($hash, 0, 2); $cd = substr($hash, 2, 2); + + $pa = Image::DATA_DIR.'/'.$base.'/'; + if (WH_SPLITS == 2) { - $pa = 'data/'.$base.'/'.$ab.'/'.$cd.'/'.$hash; + $pa .= $ab.'/'.$cd.'/'.$hash; } else { - $pa = 'data/'.$base.'/'.$ab.'/'.$hash; + $pa .= $ab.'/'.$hash; } if ($create && !file_exists(dirname($pa))) { mkdir(dirname($pa), 0755, true); diff --git a/ext/admin/main.php b/ext/admin/main.php index 2b484bc1..22845575 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -237,7 +237,7 @@ class AdminPage extends Extension $zip = new ZipArchive; if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === true) { foreach ($images as $img) { - $img_loc = warehouse_path("images", $img["hash"], false); + $img_loc = warehouse_path(Image::IMAGE_DIR, $img["hash"], false); $zip->addFile($img_loc, $img["hash"].".".$img["ext"]); } $zip->close(); diff --git a/ext/bulk_add_csv/main.php b/ext/bulk_add_csv/main.php index 14db3591..d1648a7d 100644 --- a/ext/bulk_add_csv/main.php +++ b/ext/bulk_add_csv/main.php @@ -81,7 +81,7 @@ class BulkAddCSV extends Extension send_event($ratingevent); } if (file_exists($thumbfile)) { - copy($thumbfile, warehouse_path("thumbs", $event->hash)); + copy($thumbfile, warehouse_path(Image::THUMBNAIL_DIR, $event->hash)); } } } diff --git a/ext/handle_flash/main.php b/ext/handle_flash/main.php index 9476499e..e47b193b 100644 --- a/ext/handle_flash/main.php +++ b/ext/handle_flash/main.php @@ -13,7 +13,7 @@ class FlashFileHandler extends DataHandlerExtension global $config; if (!create_thumbnail_ffmpeg($hash)) { - copy("ext/handle_flash/thumb.jpg", warehouse_path("thumbs", $hash)); + copy("ext/handle_flash/thumb.jpg", warehouse_path(Image::THUMBNAIL_DIR, $hash)); } return true; } diff --git a/ext/handle_mp3/main.php b/ext/handle_mp3/main.php index 13e2bab4..e0fcd5a7 100644 --- a/ext/handle_mp3/main.php +++ b/ext/handle_mp3/main.php @@ -9,7 +9,7 @@ class MP3FileHandler extends DataHandlerExtension { protected function create_thumb(string $hash, string $type): bool { - copy("ext/handle_mp3/thumb.jpg", warehouse_path("thumbs", $hash)); + copy("ext/handle_mp3/thumb.jpg", warehouse_path(Image::THUMBNAIL_DIR, $hash)); return true; } diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index a3bc3bd2..ac72bcc1 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -58,8 +58,8 @@ class PixelFileHandler extends DataHandlerExtension { global $config; - $inname = warehouse_path("images", $hash); - $outname = warehouse_path("thumbs", $hash); + $inname = warehouse_path(Image::IMAGE_DIR, $hash); + $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash); $ok = false; diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index f2151c06..a15942b1 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -19,10 +19,10 @@ class SVGFileHandler extends DataHandlerExtension $sanitizer->removeRemoteReferences(true); $dirtySVG = file_get_contents($event->tmpname); $cleanSVG = $sanitizer->sanitize($dirtySVG); - file_put_contents(warehouse_path("images", $hash), $cleanSVG); + file_put_contents(warehouse_path(Image::IMAGE_DIR, $hash), $cleanSVG); send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data(warehouse_path("images", $hash), $event->metadata); + $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $hash), $event->metadata); if (is_null($image)) { throw new UploadException("SVG handler failed to create image object from data"); } @@ -35,7 +35,7 @@ class SVGFileHandler extends DataHandlerExtension protected function create_thumb(string $hash, string $type): bool { if (!create_thumbnail_convert($hash)) { - copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash)); + copy("ext/handle_svg/thumb.jpg", warehouse_path(Image::THUMBNAIL_DIR, $hash)); } return true; } @@ -61,7 +61,7 @@ class SVGFileHandler extends DataHandlerExtension $sanitizer = new Sanitizer(); $sanitizer->removeRemoteReferences(true); - $dirtySVG = file_get_contents(warehouse_path("images", $hash)); + $dirtySVG = file_get_contents(warehouse_path(Image::IMAGE_DIR, $hash)); $cleanSVG = $sanitizer->sanitize($dirtySVG); $page->set_data($cleanSVG); } diff --git a/ext/regen_thumb/main.php b/ext/regen_thumb/main.php index c7115b34..a653e902 100644 --- a/ext/regen_thumb/main.php +++ b/ext/regen_thumb/main.php @@ -133,7 +133,7 @@ class RegenThumb extends Extension $i = 0; foreach ($images as $image) { if (!$force) { - $path = warehouse_path("thumbs", $image["hash"], false); + $path = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"], false); if (file_exists($path)) { continue; } @@ -157,7 +157,7 @@ class RegenThumb extends Extension $i = 0; foreach ($images as $image) { - $outname = warehouse_path("thumbs", $image["hash"]); + $outname = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"]); if (file_exists($outname)) { unlink($outname); $i++; diff --git a/ext/resize/main.php b/ext/resize/main.php index 35fd537f..bad21f6b 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -79,7 +79,7 @@ class ResizeImage extends Extension } $isanigif = 0; if ($image_obj->ext == "gif") { - $image_filename = warehouse_path("images", $image_obj->hash); + $image_filename = warehouse_path(Image::IMAGE_DIR, $image_obj->hash); if (($fh = @fopen($image_filename, 'rb'))) { //check if gif is animated (via http://www.php.net/manual/en/function.imagecreatefromgif.php#104473) while (!feof($fh) && $isanigif < 2) { @@ -167,7 +167,7 @@ class ResizeImage extends Extension } $hash = $image_obj->hash; - $image_filename = warehouse_path("images", $hash); + $image_filename = warehouse_path(Image::IMAGE_DIR, $hash); $info = getimagesize($image_filename); if (($image_obj->width != $info[0]) || ($image_obj->height != $info[1])) { @@ -193,7 +193,7 @@ class ResizeImage extends Extension $new_image->ext = $image_obj->ext; /* Move the new image into the main storage location */ - $target = warehouse_path("images", $new_image->hash); + $target = warehouse_path(Image::IMAGE_DIR, $new_image->hash); if (!@copy($tmp_filename, $target)) { throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); } diff --git a/ext/rotate/main.php b/ext/rotate/main.php index 397639a3..b2b1df3d 100644 --- a/ext/rotate/main.php +++ b/ext/rotate/main.php @@ -120,7 +120,7 @@ class RotateImage extends Extension throw new ImageRotateException("Image does not have a hash associated with it."); } - $image_filename = warehouse_path("images", $hash); + $image_filename = warehouse_path(Image::IMAGE_DIR, $hash); if (file_exists($image_filename)==false) { throw new ImageRotateException("$image_filename does not exist."); } @@ -212,7 +212,7 @@ class RotateImage extends Extension $new_image->ext = $image_obj->ext; /* Move the new image into the main storage location */ - $target = warehouse_path("images", $new_image->hash); + $target = warehouse_path(Image::IMAGE_DIR, $new_image->hash); if (!@copy($tmp_filename, $target)) { throw new ImageRotateException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); } diff --git a/ext/rule34/main.php b/ext/rule34/main.php index 577ca5af..775fe0ec 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -116,8 +116,8 @@ class Rule34 extends Extension continue; } log_info("admin", "Cleaning {$hash}"); - @unlink(warehouse_path('images', $hash)); - @unlink(warehouse_path('thumbs', $hash)); + @unlink(warehouse_path(Image::IMAGE_DIR, $hash)); + @unlink(warehouse_path(Image::THUMBNAIL_DIR, $hash)); $database->execute("NOTIFY shm_image_bans, '{$hash}';"); } } diff --git a/ext/transcode/main.php b/ext/transcode/main.php index aa471d0a..ba7e0947 100644 --- a/ext/transcode/main.php +++ b/ext/transcode/main.php @@ -308,7 +308,7 @@ class TranscodeImage extends Extension private function transcode_and_replace_image(Image $image_obj, String $target_format) { $target_format = $this->clean_format($target_format); - $original_file = warehouse_path("images", $image_obj->hash); + $original_file = warehouse_path(Image::IMAGE_DIR, $image_obj->hash); $tmp_filename = $this->transcode_image($original_file, $image_obj->ext, $target_format); @@ -321,7 +321,7 @@ class TranscodeImage extends Extension $new_image->ext = $this->determine_ext($target_format); /* Move the new image into the main storage location */ - $target = warehouse_path("images", $new_image->hash); + $target = warehouse_path(Image::IMAGE_DIR, $new_image->hash); if (!@copy($tmp_filename, $target)) { throw new ImageTranscodeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); }