From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.tschlatscher@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 0C6BC98FD
 for <pve-devel@lists.proxmox.com>; Fri,  1 Apr 2022 16:09:16 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 0AA3027C0D
 for <pve-devel@lists.proxmox.com>; Fri,  1 Apr 2022 16:09:16 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 16ABB27BFA
 for <pve-devel@lists.proxmox.com>; Fri,  1 Apr 2022 16:09:15 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DFE14459D6
 for <pve-devel@lists.proxmox.com>; Fri,  1 Apr 2022 16:09:14 +0200 (CEST)
From: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri,  1 Apr 2022 16:07:57 +0200
Message-Id: <20220401140758.1997754-1-d.tschlatscher@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.180 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pve-devel] [PATCH widget-toolkit] fix #3971: Download and copy
 button in the TaskViewer
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 01 Apr 2022 14:09:16 -0000

The taskviewer now has 2 more buttons which implement
functionality for downloading the current tasklog as a file
or copying it to the clipboard. The code for saving the log
to a file was taken from the pve System Report class and
moved to its own function in the Utils file.

Tested on Firefox 91.7.0esr and Chromium Version 99

Signed-off-by: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
---
 src/Utils.js             | 17 +++++++++++
 src/window/TaskViewer.js | 65 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/src/Utils.js b/src/Utils.js
index 6a03057..3fff3da 100644
--- a/src/Utils.js
+++ b/src/Utils.js
@@ -1272,6 +1272,23 @@ utilities: {
 	    .map(val => val.charCodeAt(0)),
 	);
     },
+
+    // Convert the given string to a file and "download" it locally
+    textToFile: function(fileName, fileContent) {
+	// Internet Explorer
+	if (window.navigator.msSaveOrOpenBlob) {
+	    navigator.msSaveOrOpenBlob(new Blob([fileContent]), fileName);
+	} else {
+	    var element = document.createElement('a');
+	    element.setAttribute('href', 'data:text/plain;charset=utf-8,' +
+		encodeURIComponent(fileContent));
+	    element.setAttribute('download', fileName);
+	    element.style.display = 'none';
+	    document.body.appendChild(element);
+	    element.click();
+	    document.body.removeChild(element);
+	}
+    },
 },
 
     singleton: true,
diff --git a/src/window/TaskViewer.js b/src/window/TaskViewer.js
index 996a41b..bf4533d 100644
--- a/src/window/TaskViewer.js
+++ b/src/window/TaskViewer.js
@@ -229,13 +229,75 @@ Ext.define('Proxmox.window.TaskViewer', {
 	    border: false,
 	});
 
+	let downloadBtn = new Ext.Button({
+		text: gettext('Download'),
+	});
+
+	let copyBtn = new Ext.Button({
+		text: gettext('Copy'),
+		iconCls: 'fa fa-clipboard',
+	});
+
 	let logView = Ext.create('Proxmox.panel.LogView', {
 	    title: gettext('Output'),
-	    tbar: [stop_btn2],
+	    tbar: [stop_btn2, '->', downloadBtn, copyBtn],
 	    border: false,
 	    url: "/api2/extjs/nodes/" + task.node + "/tasks/" + encodeURIComponent(me.upid) + "/log",
 	});
 
+	const downloadLogFull = function(callback) {
+	    Proxmox.Utils.API2Request({
+		url: "/nodes/" + task.node + "/tasks/"
+		    + encodeURIComponent(me.upid) + '/log',
+		waitMsgTarget: me,
+		method: 'GET',
+		params: {
+		    'limit': logView.viewModel.data.data.total,
+		},
+		failure: function(response, opts) {
+		    callback(response.htmlStatus, null);
+		},
+		success: function(response) {
+		    let fileContent = "";
+
+		    response.result.data.forEach((line) => {
+			fileContent += `${line.t}\n`;
+		    });
+
+		    callback(null, fileContent);
+		},
+	    });
+	};
+
+	downloadBtn.handler = function() {
+	    downloadLogFull((errStatus, fileContent) => {
+		if (errStatus) {
+		    Ext.Msg.alert(gettext('Error'), errStatus);
+		} else {
+		    let record = statstore.getRecord().data;
+		    let fileName = record.user + "@" + record.node + "_" +
+			record.type + "_" + record.pid + "_" +
+			Proxmox.Utils.render_timestamp(record.starttime) +
+			"_" + record.exitstatus + ".log";
+
+		    Proxmox.Utils.textToFile(fileName, fileContent);
+		}
+	    });
+	};
+
+	copyBtn.handler = function() {
+	    downloadLogFull((errStatus, response) => {
+		if (errStatus) {
+		    Ext.Msg.alert(gettext('Error'), errStatus);
+		} else {
+		    navigator.clipboard.writeText(response)
+		    .catch((err) => {
+			Ext.Msg.alert(gettext('Error'), err);
+		    });
+		}
+	    });
+	};
+
 	me.mon(statstore, 'load', function() {
 	    let status = statgrid.getObjectValue('status');
 
@@ -248,6 +310,7 @@ Ext.define('Proxmox.window.TaskViewer', {
 
 	    stop_btn1.setDisabled(status !== 'running');
 	    stop_btn2.setDisabled(status !== 'running');
+	    downloadBtn.setDisabled(status === 'running');
 	});
 
 	statstore.startUpdate();
-- 
2.30.2