Changed "images" and "thumbs" usages to constants

This commit is contained in:
Matthew Barbour 2019-06-15 11:18:52 -05:00 committed by matthew
parent 4ade0090cc
commit 37fe743f65
15 changed files with 40 additions and 33 deletions

View File

@ -182,7 +182,7 @@ abstract class DataHandlerExtension extends Extension
// even more hax.. // even more hax..
$event->metadata['tags'] = $existing->get_tag_list(); $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)) { if (is_null($image)) {
throw new UploadException("Data handler failed to create image object from data"); throw new UploadException("Data handler failed to create image object from data");
@ -192,7 +192,7 @@ abstract class DataHandlerExtension extends Extension
send_event($ire); send_event($ire);
$event->image_id = $image_id; $event->image_id = $image_id;
} else { } 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)) { if (is_null($image)) {
throw new UploadException("Data handler failed to create image object from data"); throw new UploadException("Data handler failed to create image object from data");
} }
@ -224,7 +224,7 @@ abstract class DataHandlerExtension extends Extension
if ($event->force) { if ($event->force) {
$result = $this->create_thumb($event->hash, $event->type); $result = $this->create_thumb($event->hash, $event->type);
} else { } else {
$outname = warehouse_path("thumbs", $event->hash); $outname = warehouse_path(Image::THUMBNAIL_DIR, $event->hash);
if (file_exists($outname)) { if (file_exists($outname)) {
return; return;
} }

View File

@ -10,6 +10,10 @@
*/ */
class Image class Image
{ {
public const DATA_DIR = "data";
public const IMAGE_DIR = "images";
public const THUMBNAIL_DIR = "thumbs";
private static $tag_n = 0; // temp hack private static $tag_n = 0; // temp hack
public static $order_sql = null; // this feels ugly public static $order_sql = null; // this feels ugly
@ -502,7 +506,7 @@ class Image
*/ */
public function get_image_filename(): string 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 public function get_thumb_filename(): string
{ {
return warehouse_path("thumbs", $this->hash); return warehouse_path(self::THUMBNAIL_DIR, $this->hash);
} }
/** /**

View File

@ -12,7 +12,7 @@
*/ */
function move_upload_to_archive(DataUploadEvent $event): void 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)) { if (!@copy($event->tmpname, $target)) {
$errors = error_get_last(); $errors = error_get_last();
throw new UploadException( throw new UploadException(
@ -171,8 +171,8 @@ function create_thumbnail_convert($hash, $input_type = ""): bool
{ {
global $config; global $config;
$inname = warehouse_path("images", $hash); $inname = warehouse_path(Image::IMAGE_DIR, $hash);
$outname = warehouse_path("thumbs", $hash); $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
$q = $config->get_int("thumb_quality"); $q = $config->get_int("thumb_quality");
$convert = $config->get_string("thumb_convert_path"); $convert = $config->get_string("thumb_convert_path");
@ -236,8 +236,8 @@ function create_thumbnail_ffmpeg($hash): bool
return false; return false;
} }
$inname = warehouse_path("images", $hash); $inname = warehouse_path(Image::IMAGE_DIR, $hash);
$outname = warehouse_path("thumbs", $hash); $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
$orig_size = video_size($inname); $orig_size = video_size($inname);
$scaled_size = get_thumbnail_size($orig_size[0], $orig_size[1], true); $scaled_size = get_thumbnail_size($orig_size[0], $orig_size[1], true);

View File

@ -163,10 +163,13 @@ function warehouse_path(string $base, string $hash, bool $create=true): string
{ {
$ab = substr($hash, 0, 2); $ab = substr($hash, 0, 2);
$cd = substr($hash, 2, 2); $cd = substr($hash, 2, 2);
$pa = Image::DATA_DIR.'/'.$base.'/';
if (WH_SPLITS == 2) { if (WH_SPLITS == 2) {
$pa = 'data/'.$base.'/'.$ab.'/'.$cd.'/'.$hash; $pa .= $ab.'/'.$cd.'/'.$hash;
} else { } else {
$pa = 'data/'.$base.'/'.$ab.'/'.$hash; $pa .= $ab.'/'.$hash;
} }
if ($create && !file_exists(dirname($pa))) { if ($create && !file_exists(dirname($pa))) {
mkdir(dirname($pa), 0755, true); mkdir(dirname($pa), 0755, true);

View File

@ -237,7 +237,7 @@ class AdminPage extends Extension
$zip = new ZipArchive; $zip = new ZipArchive;
if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === true) { if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === true) {
foreach ($images as $img) { 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->addFile($img_loc, $img["hash"].".".$img["ext"]);
} }
$zip->close(); $zip->close();

View File

@ -81,7 +81,7 @@ class BulkAddCSV extends Extension
send_event($ratingevent); send_event($ratingevent);
} }
if (file_exists($thumbfile)) { if (file_exists($thumbfile)) {
copy($thumbfile, warehouse_path("thumbs", $event->hash)); copy($thumbfile, warehouse_path(Image::THUMBNAIL_DIR, $event->hash));
} }
} }
} }

View File

@ -13,7 +13,7 @@ class FlashFileHandler extends DataHandlerExtension
global $config; global $config;
if (!create_thumbnail_ffmpeg($hash)) { 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; return true;
} }

View File

@ -9,7 +9,7 @@ class MP3FileHandler extends DataHandlerExtension
{ {
protected function create_thumb(string $hash, string $type): bool 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; return true;
} }

View File

@ -58,8 +58,8 @@ class PixelFileHandler extends DataHandlerExtension
{ {
global $config; global $config;
$inname = warehouse_path("images", $hash); $inname = warehouse_path(Image::IMAGE_DIR, $hash);
$outname = warehouse_path("thumbs", $hash); $outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
$ok = false; $ok = false;

View File

@ -19,10 +19,10 @@ class SVGFileHandler extends DataHandlerExtension
$sanitizer->removeRemoteReferences(true); $sanitizer->removeRemoteReferences(true);
$dirtySVG = file_get_contents($event->tmpname); $dirtySVG = file_get_contents($event->tmpname);
$cleanSVG = $sanitizer->sanitize($dirtySVG); $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)); 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)) { if (is_null($image)) {
throw new UploadException("SVG handler failed to create image object from data"); 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 protected function create_thumb(string $hash, string $type): bool
{ {
if (!create_thumbnail_convert($hash)) { 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; return true;
} }
@ -61,7 +61,7 @@ class SVGFileHandler extends DataHandlerExtension
$sanitizer = new Sanitizer(); $sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(true); $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); $cleanSVG = $sanitizer->sanitize($dirtySVG);
$page->set_data($cleanSVG); $page->set_data($cleanSVG);
} }

View File

@ -133,7 +133,7 @@ class RegenThumb extends Extension
$i = 0; $i = 0;
foreach ($images as $image) { foreach ($images as $image) {
if (!$force) { if (!$force) {
$path = warehouse_path("thumbs", $image["hash"], false); $path = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"], false);
if (file_exists($path)) { if (file_exists($path)) {
continue; continue;
} }
@ -157,7 +157,7 @@ class RegenThumb extends Extension
$i = 0; $i = 0;
foreach ($images as $image) { foreach ($images as $image) {
$outname = warehouse_path("thumbs", $image["hash"]); $outname = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"]);
if (file_exists($outname)) { if (file_exists($outname)) {
unlink($outname); unlink($outname);
$i++; $i++;

View File

@ -79,7 +79,7 @@ class ResizeImage extends Extension
} }
$isanigif = 0; $isanigif = 0;
if ($image_obj->ext == "gif") { 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'))) { if (($fh = @fopen($image_filename, 'rb'))) {
//check if gif is animated (via http://www.php.net/manual/en/function.imagecreatefromgif.php#104473) //check if gif is animated (via http://www.php.net/manual/en/function.imagecreatefromgif.php#104473)
while (!feof($fh) && $isanigif < 2) { while (!feof($fh) && $isanigif < 2) {
@ -167,7 +167,7 @@ class ResizeImage extends Extension
} }
$hash = $image_obj->hash; $hash = $image_obj->hash;
$image_filename = warehouse_path("images", $hash); $image_filename = warehouse_path(Image::IMAGE_DIR, $hash);
$info = getimagesize($image_filename); $info = getimagesize($image_filename);
if (($image_obj->width != $info[0]) || ($image_obj->height != $info[1])) { 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; $new_image->ext = $image_obj->ext;
/* Move the new image into the main storage location */ /* 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)) { if (!@copy($tmp_filename, $target)) {
throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); throw new ImageResizeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)");
} }

View File

@ -120,7 +120,7 @@ class RotateImage extends Extension
throw new ImageRotateException("Image does not have a hash associated with it."); 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) { if (file_exists($image_filename)==false) {
throw new ImageRotateException("$image_filename does not exist."); throw new ImageRotateException("$image_filename does not exist.");
} }
@ -212,7 +212,7 @@ class RotateImage extends Extension
$new_image->ext = $image_obj->ext; $new_image->ext = $image_obj->ext;
/* Move the new image into the main storage location */ /* 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)) { if (!@copy($tmp_filename, $target)) {
throw new ImageRotateException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); throw new ImageRotateException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)");
} }

View File

@ -116,8 +116,8 @@ class Rule34 extends Extension
continue; continue;
} }
log_info("admin", "Cleaning {$hash}"); log_info("admin", "Cleaning {$hash}");
@unlink(warehouse_path('images', $hash)); @unlink(warehouse_path(Image::IMAGE_DIR, $hash));
@unlink(warehouse_path('thumbs', $hash)); @unlink(warehouse_path(Image::THUMBNAIL_DIR, $hash));
$database->execute("NOTIFY shm_image_bans, '{$hash}';"); $database->execute("NOTIFY shm_image_bans, '{$hash}';");
} }
} }

View File

@ -308,7 +308,7 @@ class TranscodeImage extends Extension
private function transcode_and_replace_image(Image $image_obj, String $target_format) private function transcode_and_replace_image(Image $image_obj, String $target_format)
{ {
$target_format = $this->clean_format($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); $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); $new_image->ext = $this->determine_ext($target_format);
/* Move the new image into the main storage location */ /* 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)) { if (!@copy($tmp_filename, $target)) {
throw new ImageTranscodeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)"); throw new ImageTranscodeException("Failed to copy new image file from temporary location ({$tmp_filename}) to archive ($target)");
} }