From b8960735599a53584cac26fa9002bd697dbed98a Mon Sep 17 00:00:00 2001 From: _ChingC Date: Mon, 22 May 2023 18:49:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=8D=A2url=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/main/componentManager/installer.ts | 48 ++++++++----------- .../main/componentManager/utils/update.ts | 13 ++++- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/main/componentManager/installer.ts b/packages/main/componentManager/installer.ts index 058a6820..9732cad9 100644 --- a/packages/main/componentManager/installer.ts +++ b/packages/main/componentManager/installer.ts @@ -27,32 +27,21 @@ export default abstract class InstallerBase implements Installer { } async install() { - const update = await this.checkUpdate() - switch (update.msg) { - case 'failedAccessLatest': - this.notifier.onException() - return - case 'alreadyLatest': - logger.info(`[Component Installer | ${this.componentType}] No update available`) - this.notifier.onCompleted() - return - case 'haveUpdate': { - const dm = new DownloadManager() - const url = update.update.url - const urlMatches = - /^https:\/\/(.+)\/MaaAssistantArknights\/MaaAssistantArknights\/releases\/download\/(.+)\/(.+)$/.exec( - url - ) - if (!urlMatches) { - logger.error(`[Component Installer | ${this.componentType}] Invalid update url: ${url}`) + try { + const info = await this.checkUpdate() + switch (info.msg) { + case 'failedAccessLatest': this.notifier.onException() return - } - const [, host, version, filename] = urlMatches + case 'alreadyLatest': + logger.info(`[Component Installer | ${this.componentType}] No update available`) + this.notifier.onCompleted() + return + case 'haveUpdate': { + const dm = new DownloadManager() + const { url, postUpgrade } = info.update - dm.download( - `https://s3.maa-org.net:25240/maa-release/MaaAssistantArknights/MaaAssistantArknights/releases/download/${filename}`, - { + dm.download(url, { handleDownloadUpdate: task => { this.notifier.onProgress(0.99 * (task.progress.percent ?? 0)) }, @@ -68,7 +57,7 @@ export default abstract class InstallerBase implements Installer { extractFile(task.savePath, path.join(getAppBaseDir(), this.componentDir)) .then(() => { this.status = 'done' - update.update.postUpgrade() // 更新版本信息 + postUpgrade() // 更新版本信息 this.notifier.onCompleted() }) .catch(() => { @@ -82,10 +71,15 @@ export default abstract class InstallerBase implements Installer { this.notifier.onException() }, } - ).then(() => { - this.status = 'downloading' - }) + ).then(() => { + this.status = 'downloading' + }) + } } + } catch (error) { + logger.error(`[Component Installer | ${this.componentType}] Failed to install: ${error}`) + this.status = 'exception' + this.notifier.onException() } } } diff --git a/packages/main/componentManager/utils/update.ts b/packages/main/componentManager/utils/update.ts index 3e3731ef..44341b3f 100644 --- a/packages/main/componentManager/utils/update.ts +++ b/packages/main/componentManager/utils/update.ts @@ -87,7 +87,7 @@ export function createCheckUpdate( } else { logger.warn( `[Component Installer | ${component}] ` + - 'Unable to acquire OTA update asset, attempting to obtain full update asset' + 'Unable to acquire OTA update asset, attempting to obtain full update asset' ) } } @@ -106,10 +106,19 @@ export function createCheckUpdate( fs.writeFileSync(infoPath.latestFile, item.name, 'utf-8') + const url = item.browser_download_url + const urlMatches = + /^https:\/\/(.+)\/MaaAssistantArknights\/MaaAssistantArknights\/releases\/download\/(.+)\/(.+)$/.exec( + url + ) + if (!urlMatches) { + throw new Error(`Invalid update url: ${url}`) + } + const [, host, version, filename] = urlMatches return { msg: 'haveUpdate', update: { - url: item.browser_download_url, + url: `https://s3.maa-org.net:25240/maa-release/MaaAssistantArknights/MaaAssistantArknights/releases/download/${filename}`, version: latestVersion, releaseDate: published_at, postUpgrade: () => {