From 2aea79ac356c572ef5601cb3a31b862afb9ab18d Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 24 May 2015 16:08:26 +0100 Subject: [PATCH] merge common parts of handle_archive and bulk_add --- core/imageboard.pack.php | 54 ++++++++++++++++++++++++- core/util.inc.php | 60 +++++++++++++++++++++++++++- ext/bulk_add/main.php | 78 ++++--------------------------------- ext/handle_archive/main.php | 71 ++------------------------------- ext/handle_pixel/theme.php | 3 +- 5 files changed, 125 insertions(+), 141 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index d56d561c..e5979c97 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -215,7 +215,7 @@ class Image { */ public static function count_pages($tags=array()) { assert(is_array($tags)); - global $config, $database; + global $config; return ceil(Image::count_images($tags) / $config->get_int('index_images')); } @@ -1222,6 +1222,58 @@ function move_upload_to_archive(DataUploadEvent $event) { return true; } +/** + * Add a directory full of images + * + * @param $base string + * @return string + */ +function add_dir(/*string*/ $base) { + $list = ""; + + foreach(list_files($base) as $full_path) { + $short_path = str_replace($base, "", $full_path); + $filename = basename($full_path); + + $tags = path_to_tags($short_path); + $list .= "
".html_escape("$short_path (".str_replace(" ", ", ", $tags).")... "); + try { + add_image($full_path, $filename, $tags); + $list .= "ok\n"; + } + catch(UploadException $ex) { + $list .= "failed: ".$ex->getMessage()."\n"; + } + } + + return $list; +} + +/** + * @param $tmpname + * @param $filename + * @param $tags + * @throws UploadException + */ +function add_image(/*string*/ $tmpname, /*string*/ $filename, /*string*/ $tags) { + assert(file_exists($tmpname)); + + $pathinfo = pathinfo($filename); + if(!array_key_exists('extension', $pathinfo)) { + throw new UploadException("File has no extension"); + } + $metadata = array(); + $metadata['filename'] = $pathinfo['basename']; + $metadata['extension'] = $pathinfo['extension']; + $metadata['tags'] = $tags; + $metadata['source'] = null; + $event = new DataUploadEvent($tmpname, $metadata); + send_event($event); + if($event->image_id == -1) { + throw new UploadException("File type not recognised"); + } +} + /** * Given a full size pair of dimensions, return a pair scaled down to fit * into the configured thumbnail square, with ratio intact diff --git a/core/util.inc.php b/core/util.inc.php index 04c23078..0ef92d06 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -787,7 +787,6 @@ function get_session_ip(Config $config) { * conflicts from multiple installs within one domain. */ function get_prefixed_cookie(/*string*/ $name) { - global $config; $full_name = COOKIE_PREFIX."_".$name; if(isset($_COOKIE[$full_name])) { return $_COOKIE[$full_name]; @@ -808,7 +807,6 @@ function get_prefixed_cookie(/*string*/ $name) { * @param string $path */ function set_prefixed_cookie($name, $value, $time, $path) { - global $config; $full_name = COOKIE_PREFIX."_".$name; setcookie($full_name, $value, $time, $path); } @@ -1280,6 +1278,64 @@ function full_copy($source, $target) { } } +/** + * Return a list of all the regular files in a directory and subdirectories + * + * @param string $base + * @param string $_sub_dir + * @return array file list + */ +function list_files(/*string*/ $base, $_sub_dir="") { + assert(is_dir($base)); + + $file_list = []; + + $files = array(); + $dir = opendir("$base/$_sub_dir"); + while($f = readdir($dir)) { + $files[] = $f; + } + closedir($dir); + sort($files); + + foreach($files as $filename) { + $full_path = "$base/$_sub_dir/$filename"; + + if(is_link($full_path)) { + // ignore + } + else if(is_dir($full_path)) { + if($filename == "." || $filename == "..") { + $file_list = array_merge( + $file_list, + list_files($base, "$_sub_dir/$filename") + ); + } + } + else { + $full_path = str_replace("//", "/", $full_path); + $file_list[] = $full_path; + } + } + + return $file_list; +} + + +function path_to_tags($path) { + $matches = array(); + if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", basename($path), $matches)) { + $tags = $matches[1]; + } + else { + $tags = dirname($path); + $tags = str_replace("/", " ", $tags); + $tags = str_replace("__", " ", $tags); + $tags = trim($tags); + } + return $tags; +} + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * Event API * diff --git a/ext/bulk_add/main.php b/ext/bulk_add/main.php index ea590a3f..5af9051b 100644 --- a/ext/bulk_add/main.php +++ b/ext/bulk_add/main.php @@ -21,7 +21,10 @@ class BulkAdd extends Extension { if($event->page_matches("bulk_add")) { if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) { set_time_limit(0); - $this->add_dir($_POST['dir']); + $list = add_dir($_POST['dir']); + if(strlen($list) > 0) { + $this->theme->add_status("Adding files", $list); + } $this->theme->display_upload_results($page); } } @@ -34,7 +37,10 @@ class BulkAdd extends Extension { } if($event->cmd == "bulk-add") { if(count($event->args) == 1) { - $this->add_dir($event->args[0]); + $list = add_dir($event->args[0]); + if(strlen($list) > 0) { + $this->theme->add_status("Adding files", $list); + } } } } @@ -42,73 +48,5 @@ class BulkAdd extends Extension { public function onAdminBuilding(AdminBuildingEvent $event) { $this->theme->display_admin_block(); } - - /** - * Generate the necessary DataUploadEvent for a given image and tags. - */ - private function add_image($tmpname, $filename, $tags) { - assert(file_exists($tmpname)); - - $pathinfo = pathinfo($filename); - if(!array_key_exists('extension', $pathinfo)) { - throw new UploadException("File has no extension"); - } - $metadata = array(); - $metadata['filename'] = $pathinfo['basename']; - $metadata['extension'] = $pathinfo['extension']; - $metadata['tags'] = $tags; - $metadata['source'] = null; - $event = new DataUploadEvent($tmpname, $metadata); - send_event($event); - if($event->image_id == -1) { - throw new UploadException("File type not recognised"); - } - } - - private function add_dir(/*string*/ $base, $subdir="") { - if(!is_dir($base)) { - $this->theme->add_status("Error", "$base is not a directory"); - return; - } - - $list = ""; - - foreach(glob("$base/$subdir/*") as $fullpath) { - $fullpath = str_replace("//", "/", $fullpath); - $shortpath = str_replace($base, "", $fullpath); - - if(is_link($fullpath)) { - // ignore - } - else if(is_dir($fullpath)) { - $this->add_dir($base, str_replace($base, "", $fullpath)); - } - else { - $pathinfo = pathinfo($fullpath); - $matches = array(); - if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", $pathinfo["basename"], $matches)) { - $tags = $matches[1]; - } - else { - $tags = $subdir; - $tags = str_replace("/", " ", $tags); - $tags = str_replace("__", " ", $tags); - $tags = trim($tags); - } - $list .= "
".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... "); - try{ - $this->add_image($fullpath, $pathinfo["basename"], $tags); - $list .= "ok\n"; - } - catch(Exception $ex) { - $list .= "failed:
". $ex->getMessage(); - } - } - } - - if(strlen($list) > 0) { - $this->theme->add_status("Adding $subdir", $list); - } - } } diff --git a/ext/handle_archive/main.php b/ext/handle_archive/main.php index fb423d79..6536f60b 100644 --- a/ext/handle_archive/main.php +++ b/ext/handle_archive/main.php @@ -33,7 +33,10 @@ class ArchiveFileHandler extends Extension { $cmd = str_replace('%f', $event->tmpname, $cmd); $cmd = str_replace('%d', $tmpdir, $cmd); exec($cmd); - $this->add_dir($tmpdir); + $list = add_dir($tmpdir); + if(strlen($list) > 0) { + $this->theme->add_status("Adding files", $list); + } deltree($tmpdir); $event->image_id = -2; // default -1 = upload wasn't handled } @@ -47,70 +50,4 @@ class ArchiveFileHandler extends Extension { $exts = array("zip"); return in_array(strtolower($ext), $exts); } - - // copied from bulk add extension - private function add_image($tmpname, $filename, $tags) { - assert(file_exists($tmpname)); - - try { - $pathinfo = pathinfo($filename); - if(!array_key_exists('extension', $pathinfo)) { - throw new UploadException("File has no extension"); - } - $metadata = array(); - $metadata['filename'] = $pathinfo['basename']; - $metadata['extension'] = $pathinfo['extension']; - $metadata['tags'] = $tags; - $metadata['source'] = null; - $event = new DataUploadEvent($tmpname, $metadata); - send_event($event); - } - catch(UploadException $ex) { - return $ex->getMessage(); - } - } - - // copied from bulk add extension - private function add_dir($base, $subdir="") { - $list = ""; - - $dir = opendir("$base/$subdir"); - - $files = array(); - while($f = readdir($dir)) { - $files[] = $f; - } - sort($files); - - foreach($files as $filename) { - $fullpath = "$base/$subdir/$filename"; - - if(is_link($fullpath)) { - // ignore - } - else if(is_dir($fullpath)) { - if($filename[0] != ".") { - $this->add_dir($base, "$subdir/$filename"); - } - } - else { - $tmpfile = $fullpath; - $tags = $subdir; - $tags = str_replace("/", " ", $tags); - $tags = str_replace("__", " ", $tags); - $list .= "
".html_escape("$subdir/$filename (".str_replace(" ", ",", $tags).")..."); - $error = $this->add_image($tmpfile, $filename, $tags); - if(is_null($error)) { - $list .= "ok\n"; - } - else { - $list .= "failed: $error\n"; - } - } - } - closedir($dir); - - // $this->theme->add_status("Adding $subdir", $list); - } } - diff --git a/ext/handle_pixel/theme.php b/ext/handle_pixel/theme.php index de8c0c60..6b0a159c 100644 --- a/ext/handle_pixel/theme.php +++ b/ext/handle_pixel/theme.php @@ -27,7 +27,8 @@ class PixelFileHandlerTheme extends Themelet { } } - $html = "main image"; + $html = "main image"; $page->add_block(new Block("Image", $html, "main", 10)); } }