diff options
Diffstat (limited to 'interface/js')
-rw-r--r-- | interface/js/app/upload.js | 81 |
1 files changed, 60 insertions, 21 deletions
diff --git a/interface/js/app/upload.js b/interface/js/app/upload.js index d85196296..d5283cdb1 100644 --- a/interface/js/app/upload.js +++ b/interface/js/app/upload.js @@ -32,23 +32,8 @@ define(["jquery", "app/common", "app/libft"], let filesIdx = null; let scanTextHeaders = {}; - function cleanTextUpload(source) { - $("#" + source + "TextSource").val(""); - } - - function uploadText(data, source, headers) { - let url = null; - if (source === "spam") { - url = "learnspam"; - } else if (source === "ham") { - url = "learnham"; - } else if (source === "fuzzyadd") { - url = "fuzzyadd"; - } else if (source === "fuzzydel") { - url = "fuzzydel"; - } else if (source === "scan") { - url = "checkv2"; - } + function uploadText(data, url, headers, method = "POST") { + const deferred = new $.Deferred(); function server() { if (common.getSelector("selSrv") === "All SERVERS" && @@ -64,21 +49,24 @@ define(["jquery", "app/common", "app/libft"], params: { processData: false, }, - method: "POST", + method: method, headers: headers, success: function (json, jqXHR) { - cleanTextUpload(source); common.alertMessage("alert-success", "Data successfully uploaded"); if (jqXHR.status !== 200) { common.alertMessage("alert-info", jqXHR.statusText); } + deferred.resolve(); }, + complete: () => deferred.resolve(), server: server() }); + + return deferred.promise(); } function enable_disable_scan_btn(disable) { - $("#scan button:not(#cleanScanHistory, #scanOptionsToggle, .ft-columns-btn)") + $("#scan button:not(#cleanScanHistory, #deleteHashesBtn, #scanOptionsToggle, .ft-columns-btn)") .prop("disabled", (disable || $.trim($("textarea").val()).length === 0)); } @@ -240,7 +228,7 @@ define(["jquery", "app/common", "app/libft"], const source = $(this).data("upload"); const data = $("#scanMsgSource").val(); if ($.trim(data).length > 0) { - if (source === "scan") { + if (source === "checkv2") { getScanTextHeaders(); scanText(data); } else if (source === "compute-fuzzy") { @@ -265,6 +253,57 @@ define(["jquery", "app/common", "app/libft"], return false; }); + + function setDelhashButtonsDisabled(disabled = true) { + ["#deleteHashesBtn", "#clearHashesBtn"].forEach((s) => $(s).prop("disabled", disabled)); + } + + /** + * Parse a textarea (or any input) value into an array of non-empty tokens. + * Splits on commas, semicolons or any whitespace (space, tab, newline). + * + * @param {string} selector - jQuery selector for the input element. + * @returns {string[]} - Trimmed, non-empty tokens. + */ + function parseHashes(selector) { + return $(selector).val() + .split(/[,\s;]+/) + .map((t) => t.trim()) + .filter((t) => t.length > 0); + } + + $("#fuzzyDelList").on("input", () => { + const hasTokens = parseHashes("#fuzzyDelList").length > 0; + setDelhashButtonsDisabled(!hasTokens); + }); + + $("#deleteHashesBtn").on("click", () => { + $("#fuzzyDelList").prop("disabled", true); + setDelhashButtonsDisabled(); + $("#deleteHashesBtn").find(".btn-label").text("Deleting…"); + + const hashes = parseHashes("#fuzzyDelList"); + const promises = hashes.map((h) => { + const headers = { + flag: $("#fuzzyFlagText").val(), + Hash: h + }; + return uploadText(null, "fuzzydelhash", headers, "GET"); + }); + + $.when.apply($, promises).always(() => { + $("#fuzzyDelList").prop("disabled", false); + setDelhashButtonsDisabled(false); + $("#deleteHashesBtn").find(".btn-label").text("Delete hashes"); + }); + }); + + $("#clearHashesBtn").on("click", () => { + $("#fuzzyDelList").val("").focus(); + setDelhashButtonsDisabled(); + }); + + function fileInputHandler(obj) { ({files} = obj); filesIdx = 0; |