Browse Source

add basic dnd file uploads

Andrew Dolgov 3 years ago
parent
commit
ba55e576b3
4 changed files with 115 additions and 1 deletions
  1. 30 0
      backend.php
  2. 6 0
      index.php
  3. 65 0
      js/tt-irc.js
  4. 14 1
      tt-irc.css

+ 30 - 0
backend.php

@@ -637,6 +637,36 @@
 
 		break;
 
+	case "uploadandpost":
+		$file = $_FILES['file'];
+
+		if ($file && $file['size'] <= 3000000) {
+			$new_file_name = 'cache/uploads/' . time() . '-' . basename($file['name']);
+
+			$result = move_uploaded_file($file['tmp_name'], $new_file_name);
+
+			if ($result) {
+				chmod($new_file_name, 0644);
+
+				print json_encode(['upload_url' => get_self_url_prefix() . "/$new_file_name"]);
+
+				$files = glob("cache/uploads/*");
+
+				foreach ($files as $file) {
+					if (time() - filemtime($file) > 86400*7) {
+						unlink($file);
+					}
+				}
+
+				return;
+			}
+
+		}
+
+		print json_encode(["status" => "upload_failed"]);
+
+		break;
+
 	case "titleforurl":
 		if (defined('FETCH_URL_TITLES') && FETCH_URL_TITLES && class_exists("Memcached")) {
 

+ 6 - 0
index.php

@@ -98,6 +98,12 @@
 </head>
 <body class="main" user-theme="<?php echo $user_theme ?>">
 
+<div id="dnd_overlay" style="display : none">
+	<div class="inner">
+		<?php echo __("Drop files to upload and post here.") ?>
+	</div>
+</div>
+
 <div id="overlay" style="display : block">
 	<div id="overlay_inner">
 		<?php echo __("Loading, please wait...") ?>

+ 65 - 0
js/tt-irc.js

@@ -686,6 +686,70 @@ function init_second_stage(params) {
 
 }
 
+function init_dnd_upload() {
+	$(document).on('dragenter', function(e) {
+		$("#dnd_overlay").show();
+
+		e.stopPropagation();
+		e.preventDefault();
+	});
+
+	$('#dnd_overlay').on('dragleave', function(e) {
+
+		$("#dnd_overlay").hide();
+
+		e.stopPropagation();
+		e.preventDefault();
+	});
+
+	$(document).on('dragover', function (e) {
+		e.stopPropagation();
+		e.preventDefault();
+	});
+
+	$(document).on('drop', function (e) {
+		e.stopPropagation();
+		e.preventDefault();
+
+		$("#dnd_overlay").hide();
+
+		var files = e.originalEvent.dataTransfer.files;
+		handle_file_upload(files);
+
+	});
+}
+
+function handle_file_upload(files) {
+	for (var i = 0; i < files.length; i++) {
+		var fd = new FormData();
+
+		fd.append('file', files[i]);
+
+		show_spinner();
+
+		$.ajax({url: 'backend.php?op=uploadandpost',
+			type: 'POST',
+			processData: false,
+			contentType: false,
+			cache: false,
+			data: fd,
+			error: function(data) {
+				hide_spinner();
+			},
+			success: function(data) {
+				hide_spinner();
+				console.log('success', data);
+
+				if (data.upload_url) {
+					var input = $("#input-prompt");
+
+					input.val(input.val() + data.upload_url);
+				}
+			}
+			}, 'json');
+	}
+}
+
 function init() {
 	show_spinner();
 
@@ -697,6 +761,7 @@ function init() {
 
 	$.post("backend.php", {op: "init"}, function (resp) {
 		init_second_stage(resp);
+		init_dnd_upload();
 	});
 
 	window.setInterval(function() {

+ 14 - 1
tt-irc.css

@@ -404,7 +404,7 @@ form.connect-form {
 	position : absolute;
 }
 
-#overlay {
+#overlay, #dnd_overlay {
 	background : white;
 	left : 0;
 	top : 0;
@@ -414,6 +414,19 @@ form.connect-form {
 	position : absolute;
 }
 
+#dnd_overlay {
+	opacity : 0.75;
+	display : table;
+	text-align : center;
+}
+
+#dnd_overlay .inner {
+	font-weight : bold;
+	font-size : 36px;
+	display : table-cell;
+	vertical-align : middle;
+}
+
 #overlay_inner {
 	font-weight : bold;
 	margin : 10px;