From a788fe55713e7c44068ee2dd8377b98037d9375f Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 11:38:03 +0100 Subject: api: implement file/delete Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 4 +-- application/controllers/file.php | 51 +++------------------------ application/service/files.php | 62 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 49 deletions(-) (limited to 'application') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 3aafd4732..0035d0a02 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -64,8 +64,8 @@ class file extends \controllers\api\api_controller { public function delete() { $this->muser->require_access("apikey"); - - // TODO: implement + $ids = $this->input->post("ids"); + return \service\files::delete($ids); } } # vim: set noet: diff --git a/application/controllers/file.php b/application/controllers/file.php index 63f6a71b5..1b45c1ba3 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -683,55 +683,12 @@ class File extends MY_Controller { $this->muser->require_access("apikey"); $ids = $this->input->post("ids"); - $userid = $this->muser->get_userid(); - $errors = array(); - $deleted = array(); - $deleted_count = 0; - $total_count = 0; - - if (!$ids || !is_array($ids)) { - show_error("No IDs specified"); - } - foreach ($ids as $id) { - $total_count++; - $next = false; - - foreach (array($this->mfile, $this->mmultipaste) as $model) { - if ($model->id_exists($id)) { - if ($model->get_owner($id) !== $userid) { - $errors[] = array( - "id" => $id, - "reason" => "wrong owner", - ); - continue; - } - if ($model->delete_id($id)) { - $deleted[] = $id; - $deleted_count++; - $next = true; - } else { - $errors[] = array( - "id" => $id, - "reason" => "unknown error", - ); - } - } - } - - if ($next) { - continue; - } - - $errors[] = array( - "id" => $id, - "reason" => "doesn't exist", - ); - } + $ret = \service\files::delete($ids); - $this->data["errors"] = $errors; - $this->data["deleted_count"] = $deleted_count; - $this->data["total_count"] = $total_count; + $this->data["errors"] = $ret["errors"]; + $this->data["deleted_count"] = $ret["deleted_count"]; + $this->data["total_count"] = $ret["total_count"]; $this->load->view('header', $this->data); $this->load->view($this->var->view_dir.'/deleted', $this->data); diff --git a/application/service/files.php b/application/service/files.php index c270500ef..3fa73c5ca 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -134,4 +134,66 @@ class files { } } } + + // TODO: streamline this interface to be somewhat atomic in regards to + // wrong owner/unknown ids (verify first and throw exception) + static public function delete($ids) + { + $CI =& get_instance(); + + $userid = $CI->muser->get_userid(); + $errors = array(); + $deleted = array(); + $deleted_count = 0; + $total_count = 0; + + if (!$ids || !is_array($ids)) { + throw new \exceptions\UserInputException("file/delete/no-ids", "No IDs specified"); + } + + foreach ($ids as $id) { + $total_count++; + $next = false; + + foreach (array($CI->mfile, $CI->mmultipaste) as $model) { + if ($model->id_exists($id)) { + if ($model->get_owner($id) !== $userid) { + $errors[$id] = array( + "id" => $id, + "reason" => "wrong owner", + ); + continue; + } + if ($model->delete_id($id)) { + $deleted[$id] = array( + "id" => $id, + ); + $deleted_count++; + $next = true; + } else { + $errors[$id] = array( + "id" => $id, + "reason" => "unknown error", + ); + } + } + } + + if ($next) { + continue; + } + + $errors[$id] = array( + "id" => $id, + "reason" => "doesn't exist", + ); + } + + return array( + "errors" => $errors, + "deleted" => $deleted, + "total_count" => $total_count, + "deleted_count" => $deleted_count, + ); + } } -- cgit v1.2.3-24-g4f1b