diff options
Diffstat (limited to 'modules/server_add')
-rw-r--r-- | modules/server_add/controllers/server_add.php | 93 | ||||
-rw-r--r-- | modules/server_add/helpers/server_add_task.php | 85 | ||||
-rw-r--r-- | modules/server_add/js/server_add.js | 114 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree.html.php | 4 | ||||
-rw-r--r-- | modules/server_add/views/server_add_tree_dialog.html.php | 3 |
5 files changed, 185 insertions, 114 deletions
diff --git a/modules/server_add/controllers/server_add.php b/modules/server_add/controllers/server_add.php index 5f061cd5..10bf28d3 100644 --- a/modules/server_add/controllers/server_add.php +++ b/modules/server_add/controllers/server_add.php @@ -25,8 +25,7 @@ class Server_Add_Controller extends Controller { access::required("server_add", $item); $view = new View("server_add_tree_dialog.html"); - $view->action = url::site("server_add/add_photo/$id"); - $view->hidden = array("csrf" => access::csrf_token(), "base_url" => url::site("__ARGS__")); + $view->action = url::site("__ARGS__/{$id}__TASK_ID__?csrf=" . access::csrf_token()); $view->parents = $item->parents(); $view->album_title = $item->title; @@ -46,10 +45,15 @@ class Server_Add_Controller extends Controller { $path_valid = false; $path = $this->input->post("path"); - if (empty($paths[$path[0]])) { + foreach (array_keys($paths) as $valid_path) { + if ($path_valid = strpos($path, $valid_path) === 0) { + break; + } + } + if (empty($path_valid)) { throw new Exception("@todo BAD_PATH"); } - $path = implode("/", $this->input->post("path")); + if (!is_readable($path) || is_link($path)) { kohana::show_404(); } @@ -60,55 +64,60 @@ class Server_Add_Controller extends Controller { print $tree; } - function start() { + function start($id) { + access::verify_csrf(); + $files = array(); + foreach ($this->input->post("path") as $path) { + if (is_dir($path)) { + $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), true); + foreach ($dir as $file) { + $extension = strtolower(substr(strrchr($file->getFilename(), '.'), 1)); + if ($file->isReadable() && + in_array($extension, array("gif", "jpeg", "jpg", "png", "flv", "mp4"))) { + $files[] = $file->getPathname(); + } + } + } else { + $files[] = $path; + } + } + + $task = task::create("server_add_task::add_from_server", t("Add from server"), + array("item_id" => $id, + "next" => 0, + "paths" => $files)); batch::start(); + print json_encode(array("result" => "started", + "url" => url::site("server_add/add_photo/{$task->id}?csrf=" . + access::csrf_token()), + "task" => $task->as_array())); } - function add_photo($id) { + function add_photo($task_id) { access::verify_csrf(); - $parent = ORM::factory("item", $id); - access::required("server_add", $parent); - if (!$parent->is_album()) { - throw new Exception("@todo BAD_ALBUM"); - } + $task = task::run($task_id); - $path = $this->input->post("path"); + if ($task->done) { + switch ($task->state) { + case "success": + message::success(t("Add from server completed")); + break; - $paths = unserialize(module::get_var("server_add", "authorized_paths")); - if (empty($paths[$path[0]])) { - throw new Exception("@todo BAD_PATH"); - } - - $source_path = $path[0]; - // The first path corresponds to the source directory so we can just skip it. - for ($i = 1; $i < count($path); $i++) { - $source_path .= "/$path[$i]"; - if (is_link($source_path) || !is_readable($source_path)) { - kohana::show_404(); - } - $pathinfo = pathinfo($source_path); - set_time_limit(30); - if (is_dir($source_path)) { - $album = ORM::factory("item") - ->where("name", $path[$i]) - ->where("parent_id", $parent->id) - ->find(); - if (!$album->loaded) { - $album = album::create($parent, $path[$i], $path[$i], null, user::active()->id); - } - $parent = $album; - } else if (in_array($pathinfo["extension"], array("flv", "mp4"))) { - $movie = movie::create($parent, $source_path, basename($source_path), - basename($source_path), null, user::active()->id); - } else { - $photo = photo::create($parent, $source_path, basename($source_path), - basename($source_path), null, user::active()->id); + case "error": + message::success(t("Add from server completed with errors")); + break; } + print json_encode(array("result" => "success", + "task" => $task->as_array())); + + } else { + print json_encode(array("result" => "in_progress", + "task" => $task->as_array())); } } - public function finish() { + public function finish($id, $task_id) { batch::stop(); print json_encode(array("result" => "success")); } diff --git a/modules/server_add/helpers/server_add_task.php b/modules/server_add/helpers/server_add_task.php new file mode 100644 index 00000000..1475a28a --- /dev/null +++ b/modules/server_add/helpers/server_add_task.php @@ -0,0 +1,85 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class server_add_task_Core { + static function available_tasks() { + // Return empty array so nothing appears in the maintenance screen + return array(); + } + + static function add_from_server($task) { + $context = unserialize($task->context); + try { + $parent = ORM::factory("item", $context["item_id"]); + access::required("server_add", $parent); + if (!$parent->is_album()) { + throw new Exception("@todo BAD_ALBUM"); + } + + $paths = unserialize(module::get_var("server_add", "authorized_paths")); + $path = $context["paths"][$context["next"]]; + + $path_valid = false; + $item_path = ""; + foreach (array_keys($paths) as $valid_path) { + if ($path_valid = strpos($path, $valid_path) === 0) { + $item_path = substr($path, strlen($valid_path)); + $item_path = explode("/", ltrim($item_path,"/")); + $source_path = $valid_path; + break; + } + } + if (empty($path_valid)) { + throw new Exception("@todo BAD_PATH"); + } + for ($i = 0; $i < count($item_path); $i++) { + $name = $item_path[$i]; + $source_path .= "/$name"; + if (is_link($source_path) || !is_readable($source_path)) { + kohana::show_404(); + } + $pathinfo = pathinfo($source_path); + set_time_limit(30); + if (is_dir($source_path)) { + $album = ORM::factory("item") + ->where("name", $name) + ->where("parent_id", $parent->id) + ->find(); + if (!$album->loaded) { + $album = album::create($parent, $name, $name, null, user::active()->id); + } + $parent = $album; + } else if (in_array($pathinfo["extension"], array("flv", "mp4"))) { + $movie = movie::create($parent, $source_path, basename($source_path), + basename($source_path), null, user::active()->id); + } else { + $photo = photo::create($parent, $source_path, basename($source_path), + basename($source_path), null, user::active()->id); + } + } + } catch(Exception $e) { + $context["errors"][$path] = $e->getMessage(); + } + $task->done = (++$context["next"]) >= count($context["paths"]); + $task->context = serialize($context); + $task->state = "success"; + $task->percent_complete = ($context["next"] / (float)count($context["paths"])) * 100; + } + +}
\ No newline at end of file diff --git a/modules/server_add/js/server_add.js b/modules/server_add/js/server_add.js index d05c6285..b5ad7336 100644 --- a/modules/server_add/js/server_add.js +++ b/modules/server_add/js/server_add.js @@ -3,7 +3,7 @@ $("#gServerAdd").ready(function() { do_add(this, event); }); $("#gProgressBar").progressbar(); - $("#gServerAdd ul").css("display", "block"); + $("#gServerAddTree ul").css("display", "block"); }); function open_close_branch(icon, event) { @@ -13,7 +13,7 @@ function open_close_branch(icon, event) { if (closed) { if (children.length == 0) { - load_children(icon, function(data, textStatus) { + load_children(parent, function(data, textStatus) { $(parent).append(data); $(icon).addClass("ui-icon-minus"); $(icon).removeClass("ui-icon-plus"); @@ -32,6 +32,13 @@ function open_close_branch(icon, event) { } } +function get_url(uri, task_id) { + var url = $("#gServerAdd form").attr("action"); + url = url.replace("__ARGS__", uri); + url = url.replace("__TASK_ID__", !task_id ? "" : "/" + task_id); + return url; +} + function checkbox_click(checkbox, event) { var parents = $(checkbox).parents("li"); var parent = parents.get(0); @@ -40,92 +47,63 @@ function checkbox_click(checkbox, event) { $("#gServerAdd form :submit").attr("disabled", checked.length == 0); } -function load_children(icon, callback) { - var csrf = $("#gServerAdd form :hidden[name='csrf']")[0].value; - var base_url = $("#gServerAdd form :hidden[name='base_url']")[0].value; - var parms = "&csrf=" + csrf; - var parents = $(icon).parents("li"); - for (var i=parents.length - 1; i >= 0; i--) { - parms += "&path[]=" + $(parents[i]).children("span").attr("ref"); - } +function load_children(parent, callback) { + var parms = "&path=" + $(parent).find(":checkbox").attr("value"); $.ajax({async: false, success: callback, data: parms, dataType: "html", type: "POST", - url: base_url.replace("__ARGS__", "server_add/children") + url: get_url("server_add/children") }); } -var current = 0; -var process_length = 0; function do_add(submit, event) { event.preventDefault(); $("#gProgressBar").progressbar("value", 0); $("#gProgressBar").css("visibility", "visible"); var check_list = $("#gServerAdd :checkbox[checked]"); - process_length = check_list.length; - current = 0; - var base_url = $("#gServerAdd form :hidden[name='base_url']")[0].value; - $.ajax({async: false, - dataType: "json", - type: "POST", - url: base_url.replace("__ARGS__", "server_add/start") - }); + + var parms = ""; $.each(check_list, function () { - process_checkbox(this); + var parent = $(this).parents("li")[0]; + // If its a file or a directory with no children + if ($(parent).hasClass("gFile") || + ($(parent).hasClass("gDirectory") && $(parent).find(".gCheckboxTree").length == 0)) { + parms += "&path[]=" + this.value; + } }); $.ajax({async: false, + data: parms, + dataType: "json", success: function(data, textStatus) { - document.location.reload(); + var task = data.task; + var url = data.url; + var done = false; + while (!done) { + $.ajax({async: false, + success: function(data, textStatus) { + $("#gProgressBar").progressbar("value", data.task.percent_complete); + done = data.task.done; + }, + dataType: "json", + type: "POST", + url: url + }); + } + $.ajax({async: false, + success: function(data, textStatus) { + document.location.reload(); + }, + dataType: "json", + type: "POST", + url: get_url("server_add/finish", task.id) + }); }, - dataType: "json", type: "POST", - url: base_url.replace("__ARGS__", "server_add/finish") + url: get_url("server_add/start") }); - return false; -} - -function process_checkbox(checkbox) { - var parents = $(checkbox).parents("li"); - var csrf = $("#gServerAdd form :hidden[name='csrf']")[0].value; - var parms = "&csrf=" + csrf; - for (var i=parents.length - 1; i > 0; i--) { - parms += "&path[]=" + $(parents[i]).children("span").attr("ref"); - } - parms += "&path[]=" + $(checkbox).val(); - - var parent = parents[0]; - if ($(parent).hasClass("gFile")) { - process_file(parents[0], parms); - } else if ($(parent).hasClass("gDirectory") && $(parents[0]).find(".gCheckboxTree").length == 0) { - // If it is a directory and retrieve the children and process them - var icon = $(parent).children("span")[0]; - load_children(icon, function(data, textStatus) { - $(parent).append(data); - $(icon).addClass("ui-icon-plus"); - checkbox_click(checkbox, null); - var boxes = $(parent).find(".gCheckboxTree :checkbox[checked]"); - process_length += boxes.length; - $.each(boxes, function () { - process_checkbox(this); - }); - }); - current++; - $("#gProgressBar").progressbar("value", current / process_length * 100); - } -} -function process_file(li_element, parms) { - $.ajax({async: false, - success: function(data, status) { - }, - data: parms, - dataType: "html", - type: "POST", - url: $("#gServerAdd form").attr("action") - }); - current++; - $("#gProgressBar").progressbar("value", current / process_length * 100); + return false; } diff --git a/modules/server_add/views/server_add_tree.html.php b/modules/server_add/views/server_add_tree.html.php index cb3a8fb7..69ff09a6 100644 --- a/modules/server_add/views/server_add_tree.html.php +++ b/modules/server_add/views/server_add_tree.html.php @@ -14,9 +14,9 @@ $("#<?= $tree_id ?>").ready(function() { <? foreach ($data as $file => $file_info): ?> <li class="<?= empty($file_info["is_dir"]) ? "gFile" : "gDirectory gCollapsed ui-icon-left" ?>"> <? if (!empty($file_info["is_dir"])): ?> - <span class="ui-icon ui-icon-plus" ref="<?= $file ?>"></span> + <span class="ui-icon ui-icon-plus"></span> <? endif ?> - <label> <?= form::checkbox("checkbox", $file) . " $file" ?> </label> + <label> <?= form::checkbox("checkbox[]", $file_info["path"]) . " $file" ?> </label> </li> <? endforeach ?> </ul> diff --git a/modules/server_add/views/server_add_tree_dialog.html.php b/modules/server_add/views/server_add_tree_dialog.html.php index 3ce1e632..6d9c28ee 100644 --- a/modules/server_add/views/server_add_tree_dialog.html.php +++ b/modules/server_add/views/server_add_tree_dialog.html.php @@ -1,6 +1,5 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <?= html::script("modules/server_add/js/server_add.js"); ?> - <div id="gServerAdd"> <h1 style="display: none;"><?= sprintf(t("Add Photos to '%s'"), $album_title) ?></h1> @@ -20,5 +19,5 @@ <?= form::submit(array("id" => "gServerAddButton", "name" => "add", "disabled" => true, "class" => "submit"), t("Add")) ?> </span> <?= form::close() ?> - <div id="gProgressBar" ></div> + <div id="gProgressBar" style="visibility:hidden;" ></div> </div> |