diff --git a/src/common/compress.ts b/src/common/compress.ts index afdc997..25b4d64 100755 --- a/src/common/compress.ts +++ b/src/common/compress.ts @@ -53,7 +53,7 @@ function archiveZip(zipPath: string, files: string[], fileCode: string = "utf-8" }); } -function archiveUnZip(sourceZip: string, destDir: string, fileCode: string = "utf-8") { +function archiveUnZip(sourceZip: string, destDir: string, fileCode: string = "utf-8"): Promise { return new Promise(async (resolve, reject) => { const zip = new StreamZip.async({ file: sourceZip, nameEncoding: fileCode }); if (!fs.existsSync(destDir)) fs.mkdirsSync(destDir); @@ -76,7 +76,7 @@ export async function compress(sourceZip: string, files: string[], fileCode?: st return await archiveZip(sourceZip, files, fileCode); } -export async function decompress(zipPath: string, dest: string, fileCode?: string) { +export async function decompress(zipPath: string, dest: string, fileCode?: string): Promise { // if (system === "linux" && haveLinuxUnzip()) return await linuxUnzip(zipPath, dest); // return await nodeDecompress(zipPath, dest, fileCode); if (canGolangProcess()) return await golangProcessUnzip(zipPath, dest, fileCode); diff --git a/src/i18n/language/zh_cn.json b/src/i18n/language/zh_cn.json index 89c7001..9303e30 100755 --- a/src/i18n/language/zh_cn.json +++ b/src/i18n/language/zh_cn.json @@ -240,5 +240,9 @@ "version": { "versionDetectErr": "版本检查失败" + }, + + "quick_install": { + "unzipError": "解压文件失败" } } diff --git a/src/service/async_task_service/index.ts b/src/service/async_task_service/index.ts index 5b5828e..6815e47 100644 --- a/src/service/async_task_service/index.ts +++ b/src/service/async_task_service/index.ts @@ -1,4 +1,5 @@ import EventEmitter from "events"; +import logger from "../log"; export interface IAsyncTaskJSON { [key: string]: any; @@ -23,11 +24,11 @@ export class TaskCenter { } public static onTaskStopped(t: IAsyncTask) { - console.log("Task:", t.taskId, "Stopped"); + logger.info("Async Task:", t.taskId, "Stopped."); } public static onTaskFailure(t: IAsyncTask) { - console.log("Task:", t.taskId, "Failed"); + logger.info("Async Task:", t.taskId, "Failed."); } public static getTask(taskId: string) { diff --git a/src/service/async_task_service/quick_install.ts b/src/service/async_task_service/quick_install.ts index 87b1b89..7a91d3e 100644 --- a/src/service/async_task_service/quick_install.ts +++ b/src/service/async_task_service/quick_install.ts @@ -5,7 +5,7 @@ import fs from "fs-extra"; import Instance from "../../entity/instance/instance"; import InstanceSubsystem from "../system_instance"; import InstanceConfig from "../../entity/instance/Instance_config"; -import { $t } from "../../i18n"; +import { $t, i18next } from "../../i18n"; import path from "path"; import { getFileManager } from "../file_router_service"; import EventEmitter from "events"; @@ -15,7 +15,7 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask { private _status = 0; // 0=stop 1=running -1=error 2=downloading public taskId: string; private instance: Instance; - private readonly TMP_ZIP_NAME = "tmp.zip"; + private readonly TMP_ZIP_NAME = "__tmp__.zip"; private zipPath = ""; private downloadStream: fs.WriteStream = null; @@ -30,7 +30,7 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask { this.taskId = `QuickInstallTask-${this.instance.instanceUuid}-${v4()}`; } - private download() { + private download(): Promise { return new Promise(async (resolve, reject) => { this.zipPath = path.normalize(path.join(this.instance.config.cwd, this.TMP_ZIP_NAME)); const writeStream = fs.createWriteStream(this.zipPath); @@ -51,14 +51,18 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask { async start() { this._status = 1; this.emit("started"); + const fileManager = getFileManager(this.instance.instanceUuid); try { - await this.download(); - const fileManager = getFileManager(this.instance.instanceUuid); + let result = await this.download(); + result = await fileManager.promiseUnzip(this.TMP_ZIP_NAME, ".", "UTF-8"); + if (!result) throw new Error($t("quick_install.unzipError")); console.log("OK!!!!"); this.stop(); } catch (error) { console.log("Task error:", error); this.emit("failure"); + } finally { + fs.remove(fileManager.toAbsolutePath(this.TMP_ZIP_NAME), () => {}); } } @@ -97,5 +101,3 @@ export function createQuickInstallTask(targetLink: string, instanceName: string) TaskCenter.addTask(task); return task; } - -createQuickInstallTask("http://oss.duzuii.com/d/MCSManager/MCSManager_v9.6.0_win_x64.zip", "23333"); diff --git a/src/service/system_file.ts b/src/service/system_file.ts index 931cdaf..a13ef90 100755 --- a/src/service/system_file.ts +++ b/src/service/system_file.ts @@ -156,6 +156,13 @@ export default class FileManager { .catch(() => {}); } + async promiseUnzip(sourceZip: string, destDir: string, code?: string): Promise { + if (!code) code = this.fileCode; + if (!this.check(sourceZip) || !this.checkPath(destDir)) throw new Error(ERROR_MSG_01); + this.zipFileCheck(this.toAbsolutePath(sourceZip)); + return await decompress(this.toAbsolutePath(sourceZip), this.toAbsolutePath(destDir), code); + } + zip(sourceZip: string, files: string[], code?: string) { if (!code) code = this.fileCode; if (!this.checkPath(sourceZip)) throw new Error(ERROR_MSG_01);