From 5816cbcad0e9c4cda4dc10b730a5a1ea2c4e419a Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 12:11:28 +0100 Subject: api: implement file/create_multipaste Signed-off-by: Florian Pritz --- application/controllers/api/v1/file.php | 10 ++++ application/controllers/file.php | 49 ++------------------ .../exceptions/FileUploadVerifyException.php | 2 +- application/exceptions/VerifyException.php | 23 ++++++++++ application/service/files.php | 53 ++++++++++++++++++++++ 5 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 application/exceptions/VerifyException.php (limited to 'application') diff --git a/application/controllers/api/v1/file.php b/application/controllers/api/v1/file.php index 0035d0a02..fc5565416 100644 --- a/application/controllers/api/v1/file.php +++ b/application/controllers/api/v1/file.php @@ -67,5 +67,15 @@ class file extends \controllers\api\api_controller { $ids = $this->input->post("ids"); return \service\files::delete($ids); } + + public function create_multipaste() + { + $this->muser->require_access("apikey"); + $ids = $this->input->post("ids"); + $userid = $this->muser->get_userid(); + $limits = $this->muser->get_upload_id_limits(); + + return \service\files::create_multipaste($ids, $userid, $limits); + } } # vim: set noet: diff --git a/application/controllers/file.php b/application/controllers/file.php index 1b45c1ba3..c60831cba 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -700,55 +700,12 @@ class File extends MY_Controller { $this->muser->require_access("apikey"); $ids = $this->input->post("ids"); - $errors = array(); - - if (!$ids || !is_array($ids)) { - show_error("No IDs specified"); - } - - if (count(array_unique($ids)) != count($ids)) { - show_error("Duplicate IDs are not supported"); - } - - foreach ($ids as $id) { - if (!$this->mfile->id_exists($id)) { - $errors[] = array( - "id" => $id, - "reason" => "doesn't exist", - ); - } - - $filedata = $this->mfile->get_filedata($id); - if ($filedata["user"] != $this->muser->get_userid()) { - $errors[] = array( - "id" => $id, - "reason" => "not owned by you", - ); - } - } - - if (!empty($errors)) { - $errorstring = ""; - foreach ($errors as $error) { - $errorstring .= $error["id"]." ".$error["reason"]."
\n"; - } - show_error($errorstring); - } - + $userid = $this->muser->get_userid(); $limits = $this->muser->get_upload_id_limits(); - $url_id = $this->mmultipaste->new_id($limits[0], $limits[1]); - - $multipaste_id = $this->mmultipaste->get_multipaste_id($url_id); - assert($multipaste_id !== false); - foreach ($ids as $id) { - $this->db->insert("multipaste_file_map", array( - "file_url_id" => $id, - "multipaste_id" => $multipaste_id, - )); - } + $ret = \service\files::create_multipaste($ids, $userid, $limits); - return $this->_show_url(array($url_id), false); + return $this->_show_url(array($ret["url_id"]), false); } function delete() diff --git a/application/exceptions/FileUploadVerifyException.php b/application/exceptions/FileUploadVerifyException.php index d091c1eab..5125e4822 100644 --- a/application/exceptions/FileUploadVerifyException.php +++ b/application/exceptions/FileUploadVerifyException.php @@ -6,7 +6,7 @@ */ namespace exceptions; -class FileUploadVerifyException extends UserInputException { +class FileUploadVerifyException extends VerifyException { public function __toString() { $ret = $this->getMessage()."\n"; diff --git a/application/exceptions/VerifyException.php b/application/exceptions/VerifyException.php new file mode 100644 index 000000000..0e9d8b93a --- /dev/null +++ b/application/exceptions/VerifyException.php @@ -0,0 +1,23 @@ +getMessage()."\n"; + $data = $this->get_data(); + $errors = array(); + + foreach ($data as $error) { + $errors[] = sprintf("%s: %s", $error["id"], $error["reason"]); + } + + $ret .= implode("\n", $errors); + return $ret; + } +} diff --git a/application/service/files.php b/application/service/files.php index 3fa73c5ca..b4b7759e0 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -196,4 +196,57 @@ class files { "deleted_count" => $deleted_count, ); } + + static public function create_multipaste($ids, $userid, $limits) + { + $CI =& get_instance(); + + if (!$ids || !is_array($ids)) { + throw new \exceptions\UserInputException("file/create_multipaste/no-ids", "No IDs specified"); + } + + if (count(array_unique($ids)) != count($ids)) { + throw new \exceptions\UserInputException("file/create_multipaste/duplicate_id", "Duplicate IDs are not supported"); + } + + $errors = array(); + + foreach ($ids as $id) { + if (!$CI->mfile->id_exists($id)) { + $errors[$id] = array( + "id" => $id, + "reason" => "doesn't exist", + ); + continue; + } + + $filedata = $CI->mfile->get_filedata($id); + if ($filedata["user"] != $userid) { + $errors[$id] = array( + "id" => $id, + "reason" => "not owned by you", + ); + } + } + + if (!empty($errors)) { + throw new \exceptions\VerifyException("file/create_multipaste/verify-failed", "Failed to verify ID(s)", $errors); + } + + $url_id = $CI->mmultipaste->new_id($limits[0], $limits[1]); + + $multipaste_id = $CI->mmultipaste->get_multipaste_id($url_id); + assert($multipaste_id !== false); + + foreach ($ids as $id) { + $CI->db->insert("multipaste_file_map", array( + "file_url_id" => $id, + "multipaste_id" => $multipaste_id, + )); + } + + return array( + "url_id" => $url_id, + ); + } } -- cgit v1.2.3-24-g4f1b