Browse Source

完成前端页面下载升级日志的按钮功能(前端+后端)

niujiuru 2 days ago
parent
commit
6b98a12fa2

+ 35 - 0
web-ui.service/page1_handler.go

@@ -638,3 +638,38 @@ func serviceVersionSwitchHandler(w http.ResponseWriter, r *http.Request) {
 
 	json.NewEncoder(w).Encode(VersionResp{Status: "ok"})
 }
+
+func downloadUpgradeLog(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodGet {
+		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+		return
+	}
+
+	filename := fmt.Sprintf("upgrade.log.%s.tar.gz", time.Now().Format("20060102150405"))
+	tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
+
+	defer os.Remove(tmpFile)
+
+	cmd := exec.Command(
+		"tar",
+		"--warning=no-file-changed",
+		"-czf",
+		tmpFile,
+		"-C",
+		"/opt/yfkj/upgrade.service/log",
+		".",
+	)
+
+	out, err := cmd.CombinedOutput()
+	if err != nil {
+		if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
+			http.Error(w, string(out), http.StatusInternalServerError)
+			return
+		}
+	}
+
+	w.Header().Set("Content-Type", "application/gzip")
+	w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
+
+	http.ServeFile(w, r, tmpFile)
+}

+ 1 - 1
web-ui.service/web/pages/page1.html

@@ -207,7 +207,7 @@
           <button class="btn" style="margin-right:32px;" onclick="switchAutoUpgrade(event, 'disable --now')">
             关闭
           </button>
-          <button class="btn" onclick="downloadUpgradeLogFile()">
+          <button class="btn" onclick="downloadUpgradeLogFile(event)">
             下载升级日志
           </button>
         </div>

+ 51 - 0
web-ui.service/web/static/js/page1.js

@@ -291,6 +291,57 @@ function switchAutoUpgrade(event, action) {
   });
 }
 
+function downloadUpgradeLogFile(event) {
+  const btn = event.currentTarget;
+  btn.disabled = true;
+
+  fetch("/api/upgrade/logfile")
+  .then(async r => {
+    if (!r.ok) {
+      throw new Error("HTTP " + r.status);
+    }
+
+    const disposition = r.headers.get("Content-Disposition");
+    let filename = "upgrade.log.tar.gz";
+    if (disposition) {
+      const match = disposition.match(/filename="?([^";]+)"?/);
+      if (match) {
+        filename = match[1];
+      }
+    }
+
+    return {
+      blob: await r.blob(),
+      filename
+    };
+  })
+  .then(({ blob, filename }) => {
+    const url = URL.createObjectURL(blob);
+
+    const a = document.createElement("a");
+    a.href = url;
+    a.download = filename;
+
+    document.body.appendChild(a);
+    a.click();
+    a.remove();
+
+    setTimeout(() => {
+      URL.revokeObjectURL(url);
+    }, 1000);
+
+    alert("下载成功");
+  })
+  .catch(e => {
+    console.error("download failed:", e);
+    alert("下载失败: " + e.message);
+  })
+  .finally(() => {
+    btn.disabled = false;
+    btn.textContent = "下载";
+  });
+}
+
 function startPublicAccess(event) {
   if (!confirm("确定开启公网访问吗?")) {
     return;

+ 5 - 0
web-ui.service/web_route.go

@@ -66,6 +66,11 @@ var routes = []Route{
 	},
 
 	{
+		"/api/upgrade/logfile",
+		downloadUpgradeLog,
+	},
+
+	{
 		"/api/network/interfaces",
 		netInterfaces,
 	},