merge common parts of handle_archive and bulk_add
This commit is contained in:
parent
9a28f0f51a
commit
2aea79ac35
5 changed files with 125 additions and 141 deletions
|
@ -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 .= "<br>".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
|
||||
|
|
|
@ -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 *
|
||||
|
|
|
@ -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 .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
|
||||
try{
|
||||
$this->add_image($fullpath, $pathinfo["basename"], $tags);
|
||||
$list .= "ok\n";
|
||||
}
|
||||
catch(Exception $ex) {
|
||||
$list .= "failed:<br>". $ex->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(strlen($list) > 0) {
|
||||
$this->theme->add_status("Adding $subdir", $list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 .= "<br>".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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,8 @@ class PixelFileHandlerTheme extends Themelet {
|
|||
}
|
||||
}
|
||||
|
||||
$html = "<img alt='main image' class='shm-main-image' id='main_image' src='$u_ilink' data-width='{$image->width}' data-height='{$image->height}'>";
|
||||
$html = "<img alt='main image' class='shm-main-image' id='main_image' src='$u_ilink' ".
|
||||
"data-width='{$image->width}' data-height='{$image->height}'>";
|
||||
$page->add_block(new Block("Image", $html, "main", 10));
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue