Browse Source

add some basic upload error reporting

Andrew Dolgov 3 years ago
parent
commit
11a2f525e3
4 changed files with 45 additions and 5 deletions
  1. 12 3
      backend.php
  2. 17 1
      index.php
  3. 12 0
      js/functions.js
  4. 4 1
      js/tt-irc.js

+ 12 - 3
backend.php

@@ -639,8 +639,15 @@
 
 	case "uploadandpost":
 		$file = $_FILES['file'];
-
-		if ($file && $file['size'] <= 3000000) {
+		$reason = "";
+
+		if (!is_writable("cache/uploads")) {
+			$reason = __("Cache not writable");
+		} else if (!$file) {
+			$reason = __("No file uploaded.");
+		} else if ($file['size'] > 3000000) {
+			$reason = __("File is too large.");
+		} else {
 			$new_file_name = 'cache/uploads/' . time() . '-' . basename($file['name']);
 
 			$result = move_uploaded_file($file['tmp_name'], $new_file_name);
@@ -659,11 +666,13 @@
 				}
 
 				return;
+			} else {
+				$reason = __("Error moving file.");
 			}
 
 		}
 
-		print json_encode(["status" => "upload_failed"]);
+		print json_encode(["status" => "UPLOAD_FAILED", "reason" => $reason]);
 
 		break;
 

+ 17 - 1
index.php

@@ -126,10 +126,26 @@
 	</div>
 </div>
 
+<div id="notifyBox" class="modal">
+	<div class="modal-dialog">
+		<div class="modal-content">
+			<div class="modal-header">
+				<button type="button" data-toggle='modal' data-target='#notifyBox' class="close">&times;</button>
+				<h4 class="modal-title"></h4>
+			</div>
+			<div class="modal-body">&nbsp;</div>
+			<div class="modal-footer">
+				<button class="btn btn-primary" data-toggle='modal' data-target='#notifyBox'>
+					<?php echo __('Close') ?></button>
+			</div>
+		</div>
+	</div>
+</div>
+
 <div id="errorBox" class="modal">
 	<div class="modal-dialog">
 		<div class="modal-content">
-			<div class="modal-header"><div class="modal-title text-danger"><?php echo __('Fatal Exception') ?></div></div>
+			<div class="modal-header"><h4 class="modal-title text-danger"><?php echo __('Fatal Exception') ?></h4></div>
 			<div class="modal-body">&nbsp;</div>
 			<div class="modal-footer">
 				<button class="btn btn-primary" onclick="window.location.reload()">

+ 12 - 0
js/functions.js

@@ -102,6 +102,18 @@ function fatal_error(code, message) {
 	}
 }
 
+function infobox_notify(title, msg) {
+
+	var m = $("#notifyBox").modal()
+
+	m.find(".modal-body").html(msg);
+	m.find(".modal-title").html(title);
+	m.show();
+
+//	$("#notifyBox").show();
+
+}
+
 function infobox_callback2(resp) {
 	$("#infoBox").modal().find(".modal-content").html(resp);
 }

+ 4 - 1
js/tt-irc.js

@@ -735,15 +735,18 @@ function handle_file_upload(files) {
 			data: fd,
 			error: function(data) {
 				hide_spinner();
+
+				infobox_notify(__("Upload failed"), __('POST request failed'));
 			},
 			success: function(data) {
 				hide_spinner();
-				console.log('success', data);
 
 				if (data.upload_url) {
 					var input = $("#input-prompt");
 
 					input.val(input.val() + " " + data.upload_url);
+				} else if (data.reason) {
+					infobox_notify(__("Upload failed"), data.reason);
 				}
 			}
 			}, 'json');