summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-02-23 11:26:14 +0100
committerFlorian Pritz <bluewind@xinu.at>2015-02-23 11:26:14 +0100
commitea8471781ee26f79c3835e68b31353601e78d0a8 (patch)
tree0fa2552509011f8b330f044459384eaa1899d418
parented6077c460039ae1ca279c7a9d6d334caf1c2314 (diff)
upload_form: Support multiple textareas/files at the same time
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/controllers/file.php111
-rw-r--r--application/views/file/upload_form.php105
-rw-r--r--data/js/script.js39
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 @@
<?php if (isset($user_logged_in) && $user_logged_in) { ?>
-<div class="row">
- <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-upload-form">
- <?php echo form_open_multipart('file/do_paste'); ?>
+<?php echo form_open_multipart('file/do_websubmit'); ?>
+ <div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-upload-form">
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ <h3 class="panel-title">Text paste</h3>
+ </div>
+ <div class="panel-body" id="textboxes">
+ <ul class="nav nav-tabs">
+ <li class="active"><a href="#text-upload-tab-1" data-toggle="tab">Paste 1 </a></li>
+ </ul>
+ <div class="tab-content">
+ <div class="tab-pane active" id="text-upload-tab-1">
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ <input type="text" name="filename[1]" class="form-control" placeholder="Filename/title (default: stdin)">
+ </div>
+ <textarea name="content[1]" class="form-control text-upload" placeholder="Paste content"><?php
+ if (isset($textarea_content)) {
+ echo $textarea_content;
+ }
+ ?></textarea>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="row">
+ <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="panel panel-default">
<div class="panel-heading">
- <h3 class="panel-title">Text paste</h3>
+ <h3 class="panel-title">File upload</h3>
</div>
<div class="panel-body">
- <textarea name="content" class="form-control text-upload"><?php
- if (isset($textarea_content)) {
- echo $textarea_content;
- }
- ?></textarea><br>
- <button type="submit" class="btn btn-primary">Paste it!</button>
+ <div>
+ <input class="file-upload" type="file" name="file[]" multiple="multiple"><br>
+ </div>
</div>
- </form>
- </div>
- </div>
-</div>
-<div class="row">
- <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
- <?php echo form_open_multipart('file/do_upload'); ?>
- <div class="panel panel-default">
- <div class="panel-heading">
- <h3 class="panel-title">File upload</h3>
- </div>
- <div class="panel-body">
- <div>
- <input class="file-upload" type="file" name="file[]" multiple="multiple"><br>
- </div>
- <label><input type="checkbox" name="multipaste" value="1"> Create multipaste</label><br>
- <button type="submit" id="upload_button" class="btn btn-primary">Upload it!</button>
</div>
</div>
- </form>
- </div>
- <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
- <div class="panel panel-info">
- <div class="panel-heading">
- <h3 class="panel-title">Notice!</h3>
- </div>
- <div class="panel-body">
- <p>
- Uploads/pastes are <?php if ($upload_max_age > 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 <?php echo format_bytes($max_upload_size); ?>.
- You can upload a maximum of <?php echo ini_get("max_file_uploads"); ?> files at once.
- </p>
+ <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
+ <div class="panel panel-info">
+ <div class="panel-heading">
+ <h3 class="panel-title">Notice!</h3>
+ </div>
+ <div class="panel-body">
+ <p>
+ You can upload files and paste text at the same time. Empty text or file inputs will be ignored.
+ </p>
+ <p>
+ Uploads/pastes are <?php if ($upload_max_age > 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 <?php echo format_bytes($max_upload_size); ?>.
+ You can upload a maximum of <?php echo ini_get("max_file_uploads"); ?> files at once.
+ </p>
+ <button type="submit" id="upload_button" class="btn btn-primary">Upload/Paste it!</button>
+ </div>
</div>
</div>
</div>
-</div>
+</form>
<script type="text/javascript">
/* <![CDATA[ */
diff --git a/data/js/script.js b/data/js/script.js
index ee8ba9e3c..c96bdb5b7 100644
--- a/data/js/script.js
+++ b/data/js/script.js
@@ -155,7 +155,7 @@ function fixedEncodeURIComponent (str) {
document.getElementById('upload_button').innerHTML = "File(s) too big";
document.getElementById('upload_button').disabled = true;
} else {
- document.getElementById('upload_button').innerHTML = "Upload it!";
+ document.getElementById('upload_button').innerHTML = "Upload/Paste it!";
document.getElementById('upload_button').disabled = false;
}
}
@@ -179,6 +179,43 @@ function fixedEncodeURIComponent (str) {
});
+ $(document).on("input propertychange", '.text-upload', function() {
+ var need_new = true;
+
+ $('.text-upload').each(function() {
+ if (!$(this).val()) {
+ need_new = false;
+ return;
+ }
+ });
+
+ if (need_new) {
+ var i = $('#textboxes .tab-content .tab-pane').length + 1;
+ var new_tab = $('#text-upload-tab-1')
+ .clone()
+ .attr("id", "text-upload-tab-"+i)
+ .toggleClass("active", false)
+ .appendTo('#textboxes .tab-content');
+ new_tab.find("[name^=filename]").attr("name", "filename["+i+"]").val("");
+ new_tab.find("[name^=content]").attr("name", "content["+i+"]").val("");
+ $('#textboxes ul.nav')
+ .append('<li><a href="#text-upload-tab-'+i+'" data-toggle="tab">Paste '+i+' </a></li>');
+ }
+ });
+
+ $(document).on("input propertychange", '#textboxes input[name^=filename]', function() {
+ var name = $(this).val();
+ var tabId = $(this).closest("[id^=text-upload-tab-]").attr("id");
+ var id = tabId.match(/-(\d)$/)[1];
+ var tab = $('#textboxes .nav a[href="#'+tabId+'"]');
+
+ if (name != "") {
+ tab.text(name);
+ } else {
+ tab.text("Paste " + id);
+ }
+ });
+
if (typeof $.tablesorter !== 'undefined') {
// source: https://projects.archlinux.org/archweb.git/tree/sitestatic/archweb.js
$.tablesorter.addParser({