From ea8471781ee26f79c3835e68b31353601e78d0a8 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 23 Feb 2015 11:26:14 +0100 Subject: upload_form: Support multiple textareas/files at the same time Signed-off-by: Florian Pritz --- application/controllers/file.php | 111 ++++++++++++++++++++++++--------- application/views/file/upload_form.php | 105 +++++++++++++++++-------------- data/js/script.js | 39 +++++++++++- 3 files changed, 179 insertions(+), 76 deletions(-) diff --git a/application/controllers/file.php b/application/controllers/file.php index ff112e3d4..3336cba37 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -427,10 +427,8 @@ class File extends MY_Controller { $this->load->view('footer', $this->data); } - function _show_url($ids, $lexer) + private function _prepare_claim($ids, $lexer) { - $redirect = false; - if (!$this->muser->logged_in()) { $this->muser->require_session(); // keep the upload but require the user to login @@ -442,6 +440,13 @@ class File extends MY_Controller { $this->muser->require_access("basic"); } + } + + function _show_url($ids, $lexer) + { + $redirect = false; + $this->_prepare_claim($ids, $lexer); + foreach ($ids as $id) { if ($lexer) { $this->data['urls'][] = site_url($id).'/'.$lexer; @@ -735,44 +740,94 @@ class File extends MY_Controller { )); } - // Handle pastes - // TODO: merge with do_upload and also merge the forms - // TODO: add support for multiple textareas (+ view) - function do_paste() + /** + * Handle submissions from the web form (files and textareas). + */ + public function do_websubmit() { - // stateful clients get a cookie to claim the ID later - // don't force them to log in just yet - if (!stateful_client()) { - $this->muser->require_access(); + $files = getNormalizedFILES(); + $contents = $this->input->post("content"); + $filenames = $this->input->post("filename"); + + assert(is_array($filenames)); + assert(is_array($contents)); + + $ids = array(); + $ids = array_merge($ids, $this->_handle_textarea($contents, $filenames)); + $ids = array_merge($ids, $this->_handle_files($files)); + + + if (empty($ids)) { + throw new \exceptions\UserInputException("file/websubmit/no-input", "You didn't enter any text or upload any files"); + } + + if (count($ids) > 1) { + $userid = $this->muser->get_userid(); + $limits = $this->muser->get_upload_id_limits(); + $multipaste_id = \service\files::create_multipaste($ids, $userid, $limits)["url_id"]; + + $ids[] = $multipaste_id; + $this->_prepare_claim($ids, false); + + redirect(site_url($multipaste_id)."/"); } - $content = $this->input->post("content"); - $filesize = strlen($content); - $filename = "stdin"; + $this->_show_url($ids, false); + } + + private function _handle_files($files) + { + $ids = array(); - if (!$content) { - throw new \exceptions\UserInputException("file/do_paste/empty-input", "Nothing was pasted, content is empty."); + if (!empty($files)) { + $limits = $this->muser->get_upload_id_limits(); + service\files::verify_uploaded_files($files); + + foreach ($files as $key => $file) { + $id = $this->mfile->new_id($limits[0], $limits[1]); + service\files::add_uploaded_file($id, $file["tmp_name"], $file["name"]); + $ids[] = $id; + } } - if ($filesize > $this->config->item('upload_max_size')) { - throw new \exceptions\RequestTooBigException("file/do_paste/request-too-big", "Error while uploading: File too big"); + return $ids; + } + + private function _handle_textarea($contents, $filenames) + { + $ids = array(); + + foreach ($contents as $key => $content) { + $filesize = strlen($content); + + if ($filesize == 0) { + unset($contents[$key]); + } + + if ($filesize > $this->config->item('upload_max_size')) { + throw new \exceptions\RequestTooBigException("file/websubmit/request-too-big", "Error while uploading: Paste too big"); + } } - // FIXME: this duplicates service\files::add_file (kind of) $limits = $this->muser->get_upload_id_limits(); - $id = $this->mfile->new_id($limits[0], $limits[1]); - $hash = md5($content); + foreach ($contents as $key => $content) { + $filename = "stdin"; + if (isset($filenames[$key]) && $filenames[$key] != "") { + $filename = $filenames[$key]; + } - $folder = $this->mfile->folder($hash); - file_exists($folder) || mkdir ($folder); - $file = $this->mfile->file($hash); + $id = $this->mfile->new_id($limits[0], $limits[1]); + service\files::add_file_data($id, $content, $filename); + $ids[] = $id; + } - file_put_contents($file, $content); - $this->mfile->add_file($hash, $id, $filename); - $this->_show_url(array($id), false); + return $ids; } - // Handles uploaded files + /** + * Handles uploaded files + * @Deprecated only used by the cli client + */ function do_upload() { // stateful clients get a cookie to claim the ID later diff --git a/application/views/file/upload_form.php b/application/views/file/upload_form.php index 4434a53cf..9330a1659 100644 --- a/application/views/file/upload_form.php +++ b/application/views/file/upload_form.php @@ -1,61 +1,72 @@ -
-
- + +
+
+
+
+

Text paste

+
+
+ +
+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
-

Text paste

+

File upload

-
- +
+
+
- -
-
-
-
-
- -
-
-

File upload

-
-
-
-
-
-
-
- -
-
-
-
-

Notice!

-
-
-

- Uploads/pastes are 0) { - echo "deleted after ".$upload_max_age." days"; - if ($small_upload_size > 0) { - echo " unless they are smaller than ".format_bytes($small_upload_size); - } - } else { - echo "stored forever"; - } ?>. Maximum upload size is . - You can upload a maximum of files at once. -

+
+
+
+

Notice!

+
+
+

+ You can upload files and paste text at the same time. Empty text or file inputs will be ignored. +

+

+ Uploads/pastes are 0) { + echo "deleted after ".$upload_max_age." days"; + if ($small_upload_size > 0) { + echo " unless they are smaller than ".format_bytes($small_upload_size); + } + } else { + echo "stored forever"; + } ?>. Maximum upload size is . + You can upload a maximum of files at once. +

+ +
-
+