Browse Source

fix feed icon upload not working, rework form to use FormData/ajax

Andrew Dolgov 1 year ago
parent
commit
019f4578bc
2 changed files with 42 additions and 37 deletions
  1. 7 16
      classes/pref/feeds.php
  2. 35 21
      js/CommonDialogs.js

+ 7 - 16
classes/pref/feeds.php

@@ -473,6 +473,7 @@ class Pref_Feeds extends Handler_Protected {
 
 		$icon_file = $tmp_file;
 		$feed_id = clean($_REQUEST["feed_id"]);
+		$rc = 2; // failed
 
 		if (is_file($icon_file) && $feed_id) {
 			if (filesize($icon_file) < 65535) {
@@ -492,21 +493,15 @@ class Pref_Feeds extends Handler_Protected {
 
 						$rc = 0;
 					}
-				} else {
-					$rc = 2;
 				}
 			} else {
 				$rc = 1;
 			}
-		} else {
-			$rc = 2;
 		}
 
 		if (is_file($icon_file)) @unlink($icon_file);
 
-		print "<script type=\"text/javascript\">";
-		print "parent.CommonDialogs.uploadIconHandler($rc);";
-		print "</script>";
+		print $rc;
 		return;
 	}
 
@@ -766,20 +761,16 @@ class Pref_Feeds extends Handler_Protected {
 
 			/* Icon */
 
-			print "<img class='feedIcon' src=\"".Feeds::getFeedIcon($feed_id)."\">";
-
-			print "<iframe name='icon_upload_iframe'
-				style='width: 400px; height: 100px; display: none;'></iframe>";
+			print "<img class='feedIcon feed-editor-icon' src=\"".Feeds::getFeedIcon($feed_id)."\">";
 
-			print "<form style='display : block' target='icon_upload_iframe'
-			enctype='multipart/form-data' method='POST'
-			action='backend.php'>
+			print "<form onsubmit='return false;' id='feed_icon_upload_form'
+				enctype='multipart/form-data' method='POST'>
 			<label class='dijitButton'>".__("Choose file...")."
 				<input style='display: none' id='icon_file' size='10' name='icon_file' type='file'>
 			</label>
 			<input type='hidden' name='op' value='pref-feeds'>
-			<input type='hidden' name='feed_id' value=\"$feed_id\">
-			<input type='hidden' name='method' value=\"uploadicon\">
+			<input type='hidden' name='feed_id' value='$feed_id'>
+			<input type='hidden' name='method' value='uploadicon'>
 			<button dojoType='dijit.form.Button' onclick=\"return CommonDialogs.uploadFeedIcon();\"
 				type='submit'>".__('Replace')."</button>
 			<button class='alt-danger' dojoType='dijit.form.Button' onclick=\"return CommonDialogs.removeFeedIcon($feed_id);\"

+ 35 - 21
js/CommonDialogs.js

@@ -7,25 +7,6 @@ define(["dojo/_base/declare"], function (declare) {
 			const dialog = dijit.byId("infoBox");
 			if (dialog)	dialog.hide();
 		},
-		uploadIconHandler: function(rc) {
-			switch (rc) {
-				case 0:
-					Notify.info("Upload complete.");
-
-					if (App.isPrefs())
-						dijit.byId("feedTree").reload();
-					else
-						Feeds.reload();
-
-					break;
-				case 1:
-					Notify.error("Upload failed: icon is too big.");
-					break;
-				case 2:
-					Notify.error("Upload failed.");
-					break;
-			}
-		},
 		removeFeedIcon: function(id) {
 			if (confirm(__("Remove stored feed icon?"))) {
 				Notify.progress("Removing feed icon...", true);
@@ -40,6 +21,11 @@ define(["dojo/_base/declare"], function (declare) {
 					else
 						Feeds.reload();
 
+					const icon = $$(".feed-editor-icon")[0];
+
+					if (icon)
+						icon.src = icon.src.replace(/\?[0-9]+$/, "?" + new Date().getTime());
+
 				});
 			}
 
@@ -52,7 +38,35 @@ define(["dojo/_base/declare"], function (declare) {
 				alert(__("Please select an image file to upload."));
 			} else if (confirm(__("Upload new icon for this feed?"))) {
 				Notify.progress("Uploading, please wait...", true);
-				return true;
+
+				const xhr = new XMLHttpRequest();
+
+				xhr.open( 'POST', 'backend.php', true );
+				xhr.onload = function () {
+					switch (parseInt(this.responseText)) {
+						case 0:
+							Notify.info("Upload complete.");
+
+							if (App.isPrefs())
+								dijit.byId("feedTree").reload();
+							else
+								Feeds.reload();
+
+							const icon = $$(".feed-editor-icon")[0];
+
+							if (icon)
+								icon.src = icon.src.replace(/\?[0-9]+$/, "?" + new Date().getTime());
+
+							break;
+						case 1:
+							Notify.error("Upload failed: icon is too big.");
+							break;
+						case 2:
+							Notify.error("Upload failed.");
+							break;
+					}
+				};
+				xhr.send(new FormData($("feed_icon_upload_form")));
 			}
 
 			return false;
@@ -466,4 +480,4 @@ define(["dojo/_base/declare"], function (declare) {
 	};
 
 	return CommonDialogs;
-});
+});