From e0e26bd9addeb776d8b810cb380d23f551a252d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E5=93=92=E8=B5=AB=E8=90=9D?= Date: Thu, 6 Apr 2023 12:50:07 +0800 Subject: [PATCH] :hammer: Refactor: optimize auto update --- package.json | 2 +- public/i18n/en.yml | 2 +- public/i18n/zh-CN.yml | 2 +- public/i18n/zh-TW.yml | 2 +- scripts/notarize.js | 29 +++-- scripts/upload-beta.js | 87 +++++++------- scripts/upload-dist-to-r2.js | 171 +++++++++++++++------------- src/main/apis/app/system/index.ts | 13 --- src/main/lifeCycle/index.ts | 9 +- src/main/utils/updateChecker.ts | 37 +----- src/renderer/pages/PicGoSetting.vue | 1 - 11 files changed, 165 insertions(+), 190 deletions(-) diff --git a/package.json b/package.json index 662c376..bde0ec8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "piclist", - "version": "1.5.3", + "version": "1.5.2", "author": { "name": "Kuingsmile", "email": "msq@msq.pub" diff --git a/public/i18n/en.yml b/public/i18n/en.yml index bc439d0..95d50da 100644 --- a/public/i18n/en.yml +++ b/public/i18n/en.yml @@ -779,7 +779,7 @@ TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR: Custom config file parse error, please check TIPS_SHORTCUT_MODIFIED_SUCCEED: Shortcut modified successfully TIPS_SHORTCUT_MODIFIED_CONFLICT: Shortcut conflict, please reset TIPS_CUSTOM_LINK_STYLE_MODIFIED_SUCCEED: Custom link style modified successfully -TIPS_FIND_NEW_VERSION: Find new version ${v}, update many new features, do you want to download the latest version? +TIPS_FIND_NEW_VERSION: Find new version ${v}, update many new features, do you want to download the latest version in the background? UPDATE_DOWNLOADED: Update downloaded TIPS_UPDATE_DOWNLOADED: The update has been downloaded and will be installed on the next app restart. Would you like to restart now? # privacy diff --git a/public/i18n/zh-CN.yml b/public/i18n/zh-CN.yml index 5e94d47..f3533b0 100644 --- a/public/i18n/zh-CN.yml +++ b/public/i18n/zh-CN.yml @@ -780,7 +780,7 @@ TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR: 自定义文件解析出错,请检查路 TIPS_SHORTCUT_MODIFIED_SUCCEED: 快捷键已经修改成功 TIPS_SHORTCUT_MODIFIED_CONFLICT: 快捷键冲突,请重新设置 TIPS_CUSTOM_LINK_STYLE_MODIFIED_SUCCEED: 自定义链接格式已经修改成功 -TIPS_FIND_NEW_VERSION: 发现新版本${v},更新了很多功能,是否去下载最新的版本? +TIPS_FIND_NEW_VERSION: 发现新版本${v},更新了很多功能,是否后台下载最新的版本? UPDATE_DOWNLOADED: 更新已下载 TIPS_UPDATE_DOWNLOADED: 更新已下载,将在下次启动时安装,是否现在重启? diff --git a/public/i18n/zh-TW.yml b/public/i18n/zh-TW.yml index d25feb4..d378e78 100644 --- a/public/i18n/zh-TW.yml +++ b/public/i18n/zh-TW.yml @@ -778,7 +778,7 @@ TIPS_CUSTOM_CONFIG_FILE_PATH_ERROR: 自訂設定檔案解析出錯,請檢查 TIPS_SHORTCUT_MODIFIED_SUCCEED: 快捷鍵已經修改成功 TIPS_SHORTCUT_MODIFIED_CONFLICT: 快捷鍵衝突,請重新設定 TIPS_CUSTOM_LINK_STYLE_MODIFIED_SUCCEED: 自訂連結格式已經修改成功 -TIPS_FIND_NEW_VERSION: 發現新版本${v},更新了很多功能,是否去下載最新的版本? +TIPS_FIND_NEW_VERSION: 發現新版本${v},更新了很多功能,是否後台下載最新的版本? UPDATE_DOWNLOADED: 更新已下載 TIPS_UPDATE_DOWNLOADED: 更新已下載,将在下次啟動時安裝,是否立即重啟? # privacy diff --git a/scripts/notarize.js b/scripts/notarize.js index 05abf55..d6b1644 100644 --- a/scripts/notarize.js +++ b/scripts/notarize.js @@ -1,37 +1,36 @@ -"use strict"; +'use strict' require('dotenv').config() -const { notarize } = require("@electron/notarize") +const { notarize } = require('@electron/notarize') const { ELECTRON_SKIP_NOTARIZATION, XCODE_APP_LOADER_EMAIL, - XCODE_APP_LOADER_PASSWORD, + XCODE_APP_LOADER_PASSWORD } = process.env -async function main(context) { +async function main (context) { const { electronPlatformName, appOutDir } = context if ( - electronPlatformName !== "darwin" || - ELECTRON_SKIP_NOTARIZATION === "true" || + electronPlatformName !== 'darwin' || + ELECTRON_SKIP_NOTARIZATION === 'true' || !XCODE_APP_LOADER_EMAIL || !XCODE_APP_LOADER_PASSWORD ) { - console.log("Skipping Apple notarization.") - return; + console.log('Skipping Apple notarization.') + return } - console.log("Starting Apple notarization.") - const appName = context.packager.appInfo.productFilename; + console.log('Starting Apple notarization.') + const appName = context.packager.appInfo.productFilename await notarize({ - appBundleId: "com.kuingsmile.piclist", + appBundleId: 'com.kuingsmile.piclist', appPath: `${appOutDir}/${appName}.app`, appleId: XCODE_APP_LOADER_EMAIL, - appleIdPassword: XCODE_APP_LOADER_PASSWORD, + appleIdPassword: XCODE_APP_LOADER_PASSWORD }) - console.log("Finished Apple notarization.") - + console.log('Finished Apple notarization.') } -exports.default = main; \ No newline at end of file +exports.default = main diff --git a/scripts/upload-beta.js b/scripts/upload-beta.js index 8352d90..b173e05 100644 --- a/scripts/upload-beta.js +++ b/scripts/upload-beta.js @@ -14,51 +14,50 @@ const ACCOUNT_ID = process.env.R2_ACCOUNT_ID const SECRET_ID = process.env.R2_SECRET_ID const SECRET_KEY = process.env.R2_SECRET_KEY - const uploadFile = async () => { - try { - const platform = process.platform - if (configList[platform]) { - for (const [index, config] of configList[platform].entries()) { - const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}` - const distPath = path.join(__dirname, '../dist_electron') - console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`) - const fileStream = fs.createReadStream(path.join(distPath, fileName)) - const options = { - credentials: { - accessKeyId: SECRET_ID, - secretAccessKey: SECRET_KEY - }, - endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, - sslEnabled: true, - region: 'us-east-1' - } - const client = new S3Client.S3Client(options) - const parallelUploads3 = new Upload.Upload({ - client, - params: { - Bucket: BUCKET, - Key: `${FILE_PATH}${fileName}`, - Body: fileStream, - ContentType: 'application/octet-stream', - Metadata: { - description: 'uploaded by PicList' - } - } - }) - parallelUploads3.on('httpUploadProgress', (progress) => { - const progressBar = Math.round((progress.loaded / progress.total) * 100) - process.stdout.write(`\r${progressBar}% ${fileName}`) - }) - console.log('\n') - await parallelUploads3.done() - console.log(`${fileName} uploaded!`) - } - } else { - console.warn('platform not supported!', platform) + try { + const platform = process.platform + if (configList[platform]) { + for (const [index, config] of configList[platform].entries()) { + const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}` + const distPath = path.join(__dirname, '../dist_electron') + console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`) + const fileStream = fs.createReadStream(path.join(distPath, fileName)) + const options = { + credentials: { + accessKeyId: SECRET_ID, + secretAccessKey: SECRET_KEY + }, + endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, + sslEnabled: true, + region: 'us-east-1' } - } catch (err) { - console.error(err) + const client = new S3Client.S3Client(options) + const parallelUploads3 = new Upload.Upload({ + client, + params: { + Bucket: BUCKET, + Key: `${FILE_PATH}${fileName}`, + Body: fileStream, + ContentType: 'application/octet-stream', + Metadata: { + description: 'uploaded by PicList' + } + } + }) + parallelUploads3.on('httpUploadProgress', (progress) => { + const progressBar = Math.round((progress.loaded / progress.total) * 100) + process.stdout.write(`\r${progressBar}% ${fileName}`) + }) + console.log('\n') + await parallelUploads3.done() + console.log(`${fileName} uploaded!`) + } + } else { + console.warn('platform not supported!', platform) } + } catch (err) { + console.error(err) + } } -uploadFile() \ No newline at end of file +uploadFile() diff --git a/scripts/upload-dist-to-r2.js b/scripts/upload-dist-to-r2.js index 4101388..ef41a19 100644 --- a/scripts/upload-dist-to-r2.js +++ b/scripts/upload-dist-to-r2.js @@ -7,90 +7,109 @@ const pkg = require('../package.json') const configList = require('./config') const fs = require('fs') const path = require('path') +const yaml = require('js-yaml') const BUCKET = 'piclist-dl' const VERSION = pkg.version -const FILE_PATH = `latest/` +const FILE_PATH = 'latest/' const ACCOUNT_ID = process.env.R2_ACCOUNT_ID const SECRET_ID = process.env.R2_SECRET_ID const SECRET_KEY = process.env.R2_SECRET_KEY const s3 = new S3({ - endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, - accessKeyId: SECRET_ID, - secretAccessKey: SECRET_KEY, - signatureVersion: 'v4', + endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, + accessKeyId: SECRET_ID, + secretAccessKey: SECRET_KEY, + signatureVersion: 'v4' }) -const uploadFile = async () => { - try { - const platform = process.platform - if (configList[platform]) { - let versionFileHasUploaded = false - for (const [index, config] of configList[platform].entries()) { - const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}` - const distPath = path.join(__dirname, '../dist_electron') - let versionFileName = config['version-file'] - console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`) - const fileStream = fs.createReadStream(path.join(distPath, fileName)) - const options = { - credentials: { - accessKeyId: SECRET_ID, - secretAccessKey: SECRET_KEY - }, - endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, - sslEnabled: true, - region: 'us-east-1' - } - const client = new S3Client.S3Client(options) - const parallelUploads3 = new Upload.Upload({ - client, - params: { - Bucket: BUCKET, - Key: `${FILE_PATH}${fileName}`, - Body: fileStream, - ContentType: 'application/octet-stream', - Metadata: { - description: 'uploaded by PicList' - } - } - }) - parallelUploads3.on('httpUploadProgress', (progress) => { - const progressBar = Math.round((progress.loaded / progress.total) * 100) - process.stdout.write(`\r${progressBar}% ${fileName}`) - }) - console.log('\n') - await parallelUploads3.done() - console.log(`${fileName} uploaded!`) - if (!versionFileHasUploaded) { - console.log('[PicList Version File] Uploading...', versionFileName) - let versionFilePath - if (platform === 'win32') { - versionFilePath = path.join(distPath, 'latest.yml') - } else if (platform === 'darwin') { - versionFilePath = path.join(distPath, 'latest-mac.yml') - } else { - versionFilePath = path.join(distPath, 'latest-linux.yml') - } - const versionFileBuffer = fs.readFileSync(versionFilePath) - await s3.upload({ - Bucket: BUCKET, - Key: `${versionFileName}`, - Body: versionFileBuffer - }).promise() - await s3.upload({ - Bucket: BUCKET, - Key: `${FILE_PATH}${versionFileName}`, - Body: versionFileBuffer - }).promise() - versionFileHasUploaded = true - } - } - } else { - console.warn('platform not supported!', platform) - } - } catch (err) { - console.error(err) - } +const removeDupField = path => { + const file = fs.readFileSync(path, 'utf8') + const data = yaml.load(file) + const filesMap = {} + data.files.forEach(file => { + const key = file.url + file.sha512 + file.size + filesMap[key] = file + }) + data.files = Object.values(filesMap) + const newYml = yaml.dump(data, { lineWidth: -1 }) + fs.writeFileSync(path, newYml, 'utf8') } -uploadFile() \ No newline at end of file + +const uploadFile = async () => { + try { + const platform = process.platform + if (configList[platform]) { + let versionFileHasUploaded = false + for (const [index, config] of configList[platform].entries()) { + const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}` + const distPath = path.join(__dirname, '../dist_electron') + const versionFileName = config['version-file'] + console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`) + const fileStream = fs.createReadStream(path.join(distPath, fileName)) + const options = { + credentials: { + accessKeyId: SECRET_ID, + secretAccessKey: SECRET_KEY + }, + endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, + sslEnabled: true, + region: 'us-east-1' + } + const client = new S3Client.S3Client(options) + const parallelUploads3 = new Upload.Upload({ + client, + params: { + Bucket: BUCKET, + Key: `${FILE_PATH}${fileName}`, + Body: fileStream, + ContentType: 'application/octet-stream', + Metadata: { + description: 'uploaded by PicList' + } + } + }) + parallelUploads3.on('httpUploadProgress', progress => { + const progressBar = Math.round((progress.loaded / progress.total) * 100) + process.stdout.write(`\r${progressBar}% ${fileName}`) + }) + console.log('\n') + await parallelUploads3.done() + console.log(`${fileName} uploaded!`) + if (!versionFileHasUploaded) { + console.log('[PicList Version File] Uploading...', versionFileName) + let versionFilePath + if (platform === 'win32') { + versionFilePath = path.join(distPath, 'latest.yml') + } else if (platform === 'darwin') { + versionFilePath = path.join(distPath, 'latest-mac.yml') + } else { + versionFilePath = path.join(distPath, 'latest-linux.yml') + } + removeDupField(versionFilePath) + const versionFileBuffer = fs.readFileSync(versionFilePath) + await s3 + .upload({ + Bucket: BUCKET, + Key: `${versionFileName}`, + Body: versionFileBuffer + }) + .promise() + await s3 + .upload({ + Bucket: BUCKET, + Key: `${FILE_PATH}${versionFileName}`, + Body: versionFileBuffer + }) + .promise() + versionFileHasUploaded = true + } + } + } else { + console.warn('platform not supported!', platform) + } + } catch (err) { + console.error(err) + } +} +uploadFile() diff --git a/src/main/apis/app/system/index.ts b/src/main/apis/app/system/index.ts index d9c7ec9..e0069eb 100644 --- a/src/main/apis/app/system/index.ts +++ b/src/main/apis/app/system/index.ts @@ -15,7 +15,6 @@ import { IWindowList } from '#/types/enum' import pasteTemplate from '~/main/utils/pasteTemplate' import pkg from 'root/package.json' import { ensureFilePath, handleCopyUrl } from '~/main/utils/common' -import { privacyManager } from '~/main/utils/privacyManager' // import { T } from '#/i18n' import { T } from '~/main/i18n' import { isMacOSVersionGreaterThanOrEqualTo } from '~/main/utils/getMacOSVersion' @@ -50,12 +49,6 @@ export function createMenu () { } } }, - { - label: T('PRIVACY_AGREEMENT'), - click () { - privacyManager.show(false) - } - }, { label: T('RELOAD_APP'), click () { @@ -115,12 +108,6 @@ export function createContextMenu () { // @ts-ignore submenu }, - { - label: T('PRIVACY_AGREEMENT'), - click () { - privacyManager.show(false) - } - }, { label: T('RELOAD_APP'), click () { diff --git a/src/main/lifeCycle/index.ts b/src/main/lifeCycle/index.ts index ac4ee6d..3fd15c6 100644 --- a/src/main/lifeCycle/index.ts +++ b/src/main/lifeCycle/index.ts @@ -43,6 +43,7 @@ import getManageApi from '../manage/Main' import UpDownTaskQueue from '../manage/datastore/upDownTaskQueue' import { T } from '~/main/i18n' import { UpdateInfo, autoUpdater } from 'electron-updater' +import updateChecker from '../utils/updateChecker' const isDevelopment = process.env.NODE_ENV !== 'production' const handleStartUpFiles = (argv: string[], cwd: string) => { @@ -77,11 +78,14 @@ autoUpdater.on('update-available', (info: UpdateInfo) => { buttons: ['Yes', 'No'], message: T('TIPS_FIND_NEW_VERSION', { v: info.version - }) + }), + checkboxLabel: T('NO_MORE_NOTICE'), + checkboxChecked: false }).then((result) => { if (result.response === 0) { autoUpdater.downloadUpdate() } + db.set('settings.showUpdateTip', !result.checkboxChecked) }) }) @@ -151,8 +155,7 @@ class LifeCycle { } createTray() db.set('needReload', false) - // updateChecker() - autoUpdater.checkForUpdatesAndNotify() + updateChecker() // 不需要阻塞 process.nextTick(() => { shortKeyHandler.init() diff --git a/src/main/utils/updateChecker.ts b/src/main/utils/updateChecker.ts index eb7ca24..bf12147 100644 --- a/src/main/utils/updateChecker.ts +++ b/src/main/utils/updateChecker.ts @@ -1,13 +1,9 @@ -import { dialog, shell } from 'electron' import db from '~/main/apis/core/datastore' -import pkg from 'root/package.json' -import { lt } from 'semver' -import { T } from '~/main/i18n' import { getLatestVersion } from '#/utils/getLatestVersion' -const version = pkg.version +import { autoUpdater } from 'electron-updater' // const releaseUrl = 'https://api.github.com/repos/Molunerfinn/PicGo/releases' // const releaseUrlBackup = 'https://picgo-1251750343.cos.ap-chengdu.myqcloud.com' -const downloadUrl = 'https://github.com/Kuingsmile/PicList/releases/latest' +// const downloadUrl = 'https://github.com/Kuingsmile/PicList/releases/latest' const checkVersion = async () => { let showTip = db.get('settings.showUpdateTip') @@ -18,25 +14,7 @@ const checkVersion = async () => { if (showTip) { const res: string = await getLatestVersion() if (res !== '') { - const latest = res - const result = compareVersion2Update(version, latest) - if (result) { - dialog.showMessageBox({ - type: 'info', - title: T('FIND_NEW_VERSION'), - buttons: ['Yes', 'No'], - message: T('TIPS_FIND_NEW_VERSION', { - v: latest - }), - checkboxLabel: T('NO_MORE_NOTICE'), - checkboxChecked: false - }).then(res => { - if (res.response === 0) { // if selected yes - shell.openExternal(downloadUrl) - } - db.set('settings.showUpdateTip', !res.checkboxChecked) - }) - } + autoUpdater.checkForUpdatesAndNotify() } else { return false } @@ -45,13 +23,4 @@ const checkVersion = async () => { } } -// if true -> update else return false -const compareVersion2Update = (current: string, latest: string) => { - try { - return lt(current, latest) - } catch (e) { - return false - } -} - export default checkVersion diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index d6f63da..aabe1e3 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -151,7 +151,6 @@